(seq {:a 1 :b 2 :c 3})
;([:a 1] [:b 2] [:c 3])
Calling seq
on a map gives you a sequence of key-value pairs. The seq
call is usually implicit.
The pairs
- need not be in the entered order;
- are actually
MapEntry
s, which behave as pairs.
Thus
(type (first {:a 1 :b 2 :c 3}))
;clojure.lang.MapEntry
Your pseudocode
(mapcat (fn [[:key key-a][:value value-a]] (println "key: " key-a "\n value: " value-a )))
... needs several repairs:
- Supply the omitted final argument - the collection to which
map
is applied.
- Simply destructure each
MapEntry
as a pair to get at
key and value.
- Use
map
instead of mapcat
to apply the function to each pair.
It's just lucky that mapcat
works at all.
- Use
dorun
to force the sequence to evaluate and to throw it away as
it does so. The REPL does the former for you, but a running
application need not.
This gives us
(dorun
(map
(fn [[key-a value-a]] (println "key: " key-a " value: " value-a ))
{:a 1 :b 2 :c 3}))
Which prints
key: :a value: 1
key: :c value: 3
key: :b value: 2
... and returns nil
.