Clojure:我如何获得一个“坐标”的组合列表? [英] Clojure: How do I get a list of combinations of 'coordinates'?

查看:89
本文介绍了Clojure:我如何获得一个“坐标”的组合列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个函数,用于坐标,x和y。



对于x我有一系列值说[1 2 3]有另一个值序列说[4 5 6]。



我如何得到一个包含所有这些组合的列表?



因此,所需的结果将是:

 (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]] 

这样的东西是否有现成的函数?

解决方案

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

...或...

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


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?

解决方案

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

这篇关于Clojure:我如何获得一个“坐标”的组合列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆