carroll@cs.uiuc.edu (Alan M. Carroll) (05/15/91)
CommonLisp question: I have a hash table with a set of entries. What I need to do is execute a function over all entries and get back a list of the results. In other words, I want to do a (map 'list #'func table) on the hash table. Currently, I do something like: (let ((results nil)) (maphash #'(lambda (s o) (push (func o) results)) table) results ) However, this seems kludgy to me. Is there a cleaner way? Thanks! -- Alan M. Carroll <-- Another casualty of applied metaphysics Epoch Development Team Urbana Il. "I hate shopping with the reality-impaired" - Susan
barmar@think.com (Barry Margolin) (05/15/91)
In article <1991May15.153918.23823@m.cs.uiuc.edu> carroll@cs.uiuc.edu (Alan M. Carroll) writes: >(let ((results nil)) > (maphash #'(lambda (s o) (push (func o) results)) table) > results) >However, this seems kludgy to me. Is there a cleaner way? Thanks! Nope, that's a good way. If you have an ANSI/CLtL2 LOOP implementation you can also use (loop for o being the hash-values of table collect o) However, this isn't currently as portable as the MAPHASH version, since not all CL implementation implement all the new LOOP features. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar
ccampbel@bullwinkle.dsd.es.com (Colin Campbell) (05/16/91)
In article <1991May15.153918.23823@m.cs.uiuc.edu>, carroll@cs.uiuc.edu (Alan M. Carroll) writes: > ... Currently, I do something like: > > (let ((results nil)) > (maphash #'(lambda (s o) (push (func o) results)) table) > results > ) > > However, this seems kludgy to me. Is there a cleaner way? Thanks! > Yes. If you want new functionality, IMPLEMENT it. (defun maphash-values (hash-table function) "Calls FUNCTION on each object in HASH-TABLE, returns a list of results" (let ((results nil)) (maphash #'(lambda (s o) (push (funcall s o) results)) table))) Now just use maphash-values (or whatever you call it). -- Colin Campbell Evans & Sutherland Salt Lake City, UT 84108 (801) 582-5847 ccampbel@dsd.es.com