Hi,
As a clojure beginner I'm practicing my understanding of the language
but at some point the use of lazy-seq get blurry.
Imagine that I want to write a lazy function to extract a slice of a
collection something like this :
(defn slice [coll low high]
(drop (dec low) (take high coll)))
... but "hand-made" using lazy-seq and with the constraint to check
the boundaries (<= 1 low high (count coll))
Of course I could write the check "in lazy-seq" but each call counts
the collection.
I could try to use arity to keep collection length, but I don't want
to expose this to the user.
I could use and outer helper function, but what if I want it all in
one function ?
(defn slice [coll low high]
(when (<= 1 low high (count coll))
(let [step (fn step [coll low high]
(lazy-seq
(cond
(> low 1)
(step (rest coll) (dec low) (dec high))
(and (= low 1) (>= high 1))
(cons (first coll) (step (rest coll) low (dec
high)))
:else nil)))]
(step coll low high))))
Unexpectedly, this is not lazy, it seems like I'm hanging onto my
head.
Is lazy-seq creating some kind of closure with my collection in it ?
How about lazy-seq, tail call and closure clearing ?
regards,
--
kawas
--
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