且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Clojure中的顺序奇怪

更新时间:2022-12-17 15:43:10

正如ymonad所说,Clojure集是随机排序的:

As ymonad said, Clojure sets are "randomly" ordered:

> (println #{ 1 2 3 4 5 } )
#{1 4 3 2 5}

文字集语法#{1 2 3 4 5} 只是(哈希集...)$ c $的缩写c>。您可以得到如下所示的排序集:

The literal set syntax #{1 2 3 4 5} is just short for (hash-set ...). You can get a sorted set as shown:

> (hash-set 1 2 3 4 5 6)
#{1 4 6 3 2 5}
> (sorted-set 1 2 3 4 5 6)
#{1 2 3 4 5 6}
> (into (sorted-set) #{1 2 3 4 5 6} )
#{1 2 3 4 5 6}

在上一个示例中,我们使用 in $ c>将常规集中的元素添加到空的 sorted-set

In the last example, we use into to add elements from the regular set into an empty sorted-set