Logo

Programming-Idioms

History of Idiom 7 > diff from v68 to v69

Edit summary for version 69 by finalfantasia:
[Clojure] `clojure.core/map-indexed` is more idiomated than using `clojure.core/range` to generate indices

Version 68

2019-09-26, 17:49:40

Version 69

2019-09-26, 17:55:15

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Code
(doseq [[value index] (map vector items (range))]
  (println index ": " value))
Code
(doseq [[value index] (map vector items (range))]
  (println index ": " value))

(run! (fn [[i x]] (println i ":" x))
      (map-indexed vector items))
Comments bubble
items can be any Clojure collection (vector, list, set, map). We construct a new list which contains in each element the index of each one and its original value in items. doseq is use for side-effects.
Comments bubble
items can be any Clojure collection (vector, list, set, map). We construct a new list which contains in each element the index of each one and its original value in items. doseq is use for side-effects.
Doc URL
http://clojuredocs.org/clojure.core/doseq
Doc URL
http://clojuredocs.org/clojure.core/map-indexed