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

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

问题描述

假设我有一个接受坐标 x 和 y 的函数.

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

对于 x,我有一个值序列,比如 [1 2 3],对于 y,我有另一个值序列,比如 [4 5 6].

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

是否有这样的功能?

推荐答案

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天全站免登陆