mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 15:48:01 +00:00
Update mapping-with-an-index.md
This commit is contained in:
@@ -9,3 +9,10 @@ can reach for `map-indexed`. The `map-indexed` function can be used like so:
|
|||||||
> (map-indexed (fn [idx item] (str idx " - " item)) foods)
|
> (map-indexed (fn [idx item] (str idx " - " item)) foods)
|
||||||
("0 - pizza" "1 - hotdog" "2 - chicken alfredo")
|
("0 - pizza" "1 - hotdog" "2 - chicken alfredo")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Alternatively, `map` can take multiple sequences, and each becomes a new argument to the mapping function. By giving an infinite sequential list of numbers starting at 0 as the first sequence, you can pretend they're indices, like so:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
> (map (fn [idx item] (str idx " - " item)) (range) foods)
|
||||||
|
("0 - pizza" "1 - hotdog" "2 - chicken alfredo")
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user