Distributing list items to variables in clojure

…衆ロ難τιáo~ 提交于 2019-12-24 14:29:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!