On Jun 24, 2009, at 11:24, Volker wrote:
> I'm rather new to clojure and still fighting with the
> basics. Just new I wondering why (type (doall x)) is still
> clojure.lang.LazySeq? Probably easy but I get no clue, could
> anyone help?
A lazy seq is a data structure whose elements are built on demand
*and then stored*. It remains a lazy seq forever. Perhaps you thought
that a lazy seq is just an intermediate data type that becomes a list
once all elements are there? This is not possible, because an object
cannot change its type. Or perhaps you expected doall to convert a
lazy seq into a list? That's not what it does, it returns exactly the
same object that is passed as its argument, it just forces evaluation
of all elements.
> user> (def x (range 0 (* 2.0 Math/PI) 0.1))
> #'user/x
> user> (ns user
> (:require [clojure.contrib.generic.math-functions :as gmath]))
> nil
> user> (gmath/sin (doall x))
> ; Evaluation aborted.
> No method in multimethod 'sin' for dispatch value: class
> clojure.lang.LazySeq
That wouldn't work even if doall converted x to another collection
data type. If you want to apply sin to sequences, you have to provide
a method for applying sin to sequences - which is of course perfectly
well possible:
(defmethod gmath/sin clojure.lang.ISeq
[coll]
(map gmath/sin coll))
That should work (I didn't test it), but I would probably prefer to see
(map gmath/sin (doall x))
written out explicitly.
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---