开发者

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

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 t开发者_JS百科he 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?


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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜