[Ecls-list] map iterator

Juan Jose Garcia Ripoll lisp at arrakis.es
Wed Nov 9 01:29:57 UTC 2005


On Wed, 2005-11-09 at 19:35 +1100, Dean O'Connor wrote:
> I am trying to implement the functional equivalent an C++ iterator to an 
> STL map class, in Lisp.

I think this is your problem: you try to import C++ iteration ideas. In
Lisp one would write down a map function that does the iteration and
calls another function for each of the pairs, like in (bogus untested
code follows)

(defun map-over-hash (table function)
   (with-hash-table-iterator (next-elt table)
      (loop     
         (multiple-value-bind (more? key value) (next-elt)
           (unless more? (return))
	   (funcall function key value)))))

Now you can code your function in C++ or in embedded C++ in lisp, or
however you want. Notice that if you want further flexibility you can
convert the previous iteration function into a method that dispatches on
the container

(defmethod generic-map ((container hash-table) function)
   ...)

Similarly one defines generic versions of FIND, FIND-IF, POSITION, etc,
and then one has all algorithms.

Only caveat is that your FUNCTION might need to be a closure, if you
want to keep state from iteration to iteration. But that is pretty easy
if you code it in lisp instead of C++.

In general, even though ECL is designed to be embedded in C/C++
programs, programming is far easier when you code most of the logic in
lisp and only some interfaces in embedded C/C++.

Regards,

Juanjo





More information about the ecl-devel mailing list