无论项目的顺序如何,都可以比较Clojure中的两个向量 [英] Compare two vectors in clojure no matter the order of the items

查看:29
本文介绍了无论项目的顺序如何,都可以比较Clojure中的两个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个向量,并找出它们具有的项目是否相同,而与项目的顺序无关。

I want to compare two vectors and find out if the items they have are the same no matter the order the items are in.

所以。.

现在处于Clojure中:

right now in clojure:

(= [1 2 3] [3 2 1]) ;=> false

我要:

(other_fun [1 2 3] [3 2 1]) ;=> true

(other_fun [1 2 3 4] [3 2 1]) ;=> false

我找不到像Java中的containsAll一样

I could not find a containsAll like in java

推荐答案

如果您不关心重复项,则可以从两个向量创建集合并进行比较:

If you don't care about duplicates, you could create sets from both vectors and compare these:

(= (set [1 2 3]) (set [3 2 1])) ;=> true

作为函数:

(defn set= [& vectors] (apply = (map set vectors)))

这篇关于无论项目的顺序如何,都可以比较Clojure中的两个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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