If I understand correctly you don't need an atom to count the elements in a
seq....all you need is 'count'
(let [s [1 2 3 4 5]
      n (count s)]
  (vector n s))

=> [5 [1 2 3 4 5]]

If you want to count only the first 100 you can first "take" as many as you
need and then count them...you can also use loop/recur if you want to get
down and dirty!

Jim




On Mon, Jul 30, 2012 at 7:30 PM, Vinay D.E <devi...@gmail.com> wrote:

> 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 clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to