问题
When I pass this function
(into []
(map #(+ %1 %2)
[1 2]
[5 6]))
I get this result: [6 8]
What should I do to get this: [6 7 7 8]
while keeping this #(+ %1 %2)
?
Seems like map
isn't the right function in this case.
回答1:
Use for
when you want a Cartesian product:
user=> (for [x [1 2] y [5 6]]
#_=> (+ x y))
(6 7 7 8)
回答2:
for
is one option as Alex answer shows. map
can also be used (with mapcat
) as shown below:
user=> (mapcat #(map (partial + %1) [5 6]) [1 2])
(6 7 7 8)
来源:https://stackoverflow.com/questions/15036460/distributing-list-items-to-variables-in-clojure