Clojure集和哈希集返回无序值 [英] Clojure set and hash-set returns unordered value

查看:82
本文介绍了Clojure集和哈希集返回无序值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在玩Clojure,在那里遇到了一些棘手的事情。

I'm playing with Clojure recently and I've had encountered some tricky things there.

为什么这样做:

(set [3 3 3 3 4 "Sample String"])

返回我的REPL:

;=> #{4 3 "Sample String"}

当自然出现时,应该返回:

When it comes as natural that is should return:

;=> #{3 4 "Sample String"}

是否有任何不适?

推荐答案

Set返回集合的唯一元素。集合中的元素是无序的,这意味着不能保证元素的特定顺序(请参见 https://clojuredocs.org/ Clojure.core / set )。

Set returns unique elements of a collection. Elements in a set are unordered, meaning no particular order of elements is guaranteed (see https://clojuredocs.org/clojure.core/set).

如果您想要特定的订购,请使用排序集( https://clojuredocs.org/clojure.core/sorted-set )。

If you want a particular ordering use sorted-set (https://clojuredocs.org/clojure.core/sorted-set).

由于示例中元素的类型不同(即字符串和数字),因此必须指定一个比较器来定义顺序,因此您需要使用sorted-set-by( https://clojuredocs.org/clojure.core/sorted-set-by )。

Since you have different types of elements in your example (i.e. strings and numbers) you will have to specify a comparator to define the ordering, thus you would need to use sorted-set-by (https://clojuredocs.org/clojure.core/sorted-set-by).

这篇关于Clojure集和哈希集返回无序值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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