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