mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Mapping With An Index as a clojure til.
This commit is contained in:
11
clojure/mapping-with-an-index.md
Normal file
11
clojure/mapping-with-an-index.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# 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:
|
||||
|
||||
```clojure
|
||||
> (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")
|
||||
```
|
||||
Reference in New Issue
Block a user