1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00
Files
til/clojure/mapping-with-an-index.md
2015-12-12 14:17:01 -06:00

372 B

Mapping With An Index

If you're mapping over a collection and you need an index for each item, you can reach for map-indexed. The map-indexed function can be used like so:

> (def foods ["pizza" "hotdog" "chicken alfredo"])
#'cljs.user/foods
> (map-indexed (fn [idx item] (str idx " - " item)) foods)
("0 - pizza" "1 - hotdog" "2 - chicken alfredo")