I am a newbie and was doing some exercises when I ran across something that
I don't understand.
I am trying to count the number of elements in an array less than 100.
My first attempt didn't work. The counter returns 0
(let [a (atom 0)
i (take-while (fn[x] (swap! a inc) (< x 100)) [1 2 3 4 5])]
[@a i]) => [0 (1 2 3 4 5)]
I am not getting any insight from the source
<http://clojuredocs.org/clojure_core/clojure.core/take-while>of
'take-while' too.
The strange thing is, if I set a break-point, I can see that the atom is
getting incremented.
(let [a (atom 0)
i (take-while (fn[x] (swap! a inc) *(swank.core/break) *(< x 100))
[1 2 3 4 5])]
[@a i])
whereas if I do this in a slightly more roundabout way, it works!
(let [a (atom 0)
i (reduce
(fn[l in]
(if (or (nil? (last l)) (< (last l) 100))
((fn[] (swap! a inc) (conj l in)))
l )) [] [1 2 3 4 5])]
[@a i]) => [5 [1 2 3 4 5]]
I tried reading docs, but I am unable to understand what is happening. If
anyone could please point me in the right direction, it would be great.
Thanks in advance
Vinay
--
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