I would like to iterate over a collection and at the same time also maintain a counter ex
(for [x (range 10) y (inc 0)] [x y] )
I would like 'y' to represent the counter, so for every element the output is ( [0 0] [ 1 1] [2 2]...). How do I do that?
You can use indexed
from clojure.contrib.seq
. Example:
(indexed '(a b c d)) => ([0 a] [1 b] [2 c] [3 d])
You can also use map-indexed
. Example:
(map-indexed vector "foobar") => ([0 \f] [1 \o] [2 \o] [3 \b] [4 \a] [5 \r])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With