Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure - map-indexed is it possible to start with an index other than 0?

Tags:

clojure

Usually map-indexed function maps each list item to a respective index where the first index is 0, the second is 1 etc.

Is it possible to have the index start at another number and proceed from there?

like image 489
Yechiel Labunskiy Avatar asked Feb 23 '14 11:02

Yechiel Labunskiy


2 Answers

Easiest way is to just remember that you can pass multiple sequences to map.

(map vector [:a :b :c] (iterate inc 100))

=> ([:a 100] [:b 101] [:c 102])
like image 169
danneu Avatar answered Sep 28 '22 02:09

danneu


You simply wrap the index with another function in the receiving function

For example if we wanted to start at 1 instead of zero we would simply use inc

(map-indexed (fn [i v] (vector (inc i) v)) ["one" "two" "three"])

Will return

([1 "one"] [2 "two"] [3 "three"])
like image 29
KobbyPemson Avatar answered Sep 28 '22 02:09

KobbyPemson