Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a counter in for

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?

like image 843
murtaza52 Avatar asked Jun 19 '12 12:06

murtaza52


1 Answers

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])
like image 146
Simeon Visser Avatar answered Oct 18 '22 19:10

Simeon Visser