سؤال

I'm trying to combine a couple of LazySeqs into one collection of maps.

("a" "b" "c" ...)
("x" "y" "z" ...)

into

({:key1 "a" :key2 "x"} {:key1 "b" :key2 "y"} ...)

It is guaranteed that the LazySeqs are of the same length. What would be the functional way to do this?

هل كانت مفيدة؟

المحلول

map will work on multiple seqs, accepting one argument at a time from each:

(def a ["a" "b" "c"])
(def b ["x" "y" "z"])

(map (fn [x y] {:key1 x :key2 y}) a b)  ; a and b could be lazy...
;=> ({:key1 "a", :key2 "x"} {:key1 "b", :key2 "y"} {:key1 "c", :key2 "z"})
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top