Clojure: How do I get a list of combinations of 'coordinates'?

孤人 提交于 2019-12-17 19:38:20

问题


Say i have a function that takes to coordinates, x and y.

For x I have a sequence of values say [1 2 3] and for y I have another sequence of values say [4 5 6].

How would I get a list with all the combinations of these?

So the desired result would be something like:

(myfn [1 2 3] [4 5 6]) => [[1 4] [1 5] [1 6] [2 4] [2 5] [2 6] [3 4] [3 5] [3 6]]

Is there an existing function for something like this?


回答1:


data> (for [x [1 2 3] y [4 5 6]] (vector x y))
([1 4] [1 5] [1 6] [2 4] [2 5] [2 6] [3 4] [3 5] [3 6])

...or...

user> (use 'clojure.contrib.combinatorics)
nil
user> (cartesian-product [1 2 3] [4 5 6])
((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))


来源:https://stackoverflow.com/questions/1646544/clojure-how-do-i-get-a-list-of-combinations-of-coordinates

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