Logo

Programming-Idioms

History of Idiom 7 > diff from v73 to v74

Edit summary for version 74 by finalfantasia:
[Clojure] doseq might be more idiomatic

Version 73

2019-09-26, 18:03:29

Version 74

2019-09-26, 18:36:35

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