Back
sorted-set (clj)
(source)function
(sorted-set & keys)
Returns a new sorted set with supplied keys. Any equal keys are
handled as if by repeated uses of conj.
Examples
cloojure/tupelo
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public License 1.0
; (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html at
; the root of this distribution. By using this software in any fashion, you are agreeing to be
; bound by the terms of this license. You must not remove this notice, or any other, from this
; software.
(ns tst.tupelo.string
(:refer-clojure :exclude [take drop])
;---------------------------------------------------------------------------------------------------
; https://code.thheller.com/blog/shadow-cljs/2019/10/12/clojurescript-macros.html
; http://blog.fikesfarm.com/posts/2015-12-18-clojurescript-macro-tower-and-loop.html
#?(:cljs (:require-macros [tupelo.test]))
(:require
[clojure.test] ; sometimes this is required - not sure why
[clojure.core :as cc]
[tupelo.core :as t :refer [spy spyx spyxx spyx-pretty forv]]
[tupelo.chars :as char]
[tupelo.string :as str]
[tupelo.test :refer [testing is verify verify-focus
is isnt is= isnt= is-set= is-nonblank= is-nonblank-lines=
throws? throws-not?
]]
))
(verify
(testing "single string"
(is (= "" (str/clip 0 "abcdefg")))
(is (= "a" (str/clip 1 "abcdefg")))
(is (= "ab" (str/clip 2 "abcdefg")))
(is (= "abc" (str/clip 3 "abcdefg")))
(is (= "abcd" (str/clip 4 "abcdefg")))
(is (= "abcde" (str/clip 5 "abcdefg"))))
(testing "two strings"
(is (= "" (str/clip 0 "abc defg")))
(is (= "a" (str/clip 1 "abc defg")))
(is (= "ab" (str/clip 2 "abc defg")))
(is (= "abc" (str/clip 3 "abc defg")))
(is (= "abc " (str/clip 4 "abc defg")))
(is (= "abc d" (str/clip 5 "abc defg"))))
(testing "two strings & char"
(is (= "" (str/clip 0 "ab" \c "defg")))
(is (= "a" (str/clip 1 "ab" \c "defg")))
(is (= "ab" (str/clip 2 "ab" \c "defg")))
(is (= "abc" (str/clip 3 "ab" \c "defg")))
(is (= "abcd" (str/clip 4 "ab" \c "defg")))
(is (= "abcde" (str/clip 5 "ab" \c "defg"))))
(testing "two strings & digit"
(is (= "" (str/clip 0 "ab" 9 "defg")))
(is (= "a" (str/clip 1 "ab" 9 "defg")))
(is (= "ab" (str/clip 2 "ab" 9 "defg")))
(is (= "ab9" (str/clip 3 "ab" 9 "defg")))
(is (= "ab9d" (str/clip 4 "ab" 9 "defg")))
(is (= "ab9de" (str/clip 5 "ab" 9 "defg"))))
(testing "vector"
(is (= "" (str/clip 0 [1 2 3 4 5])))
(is (= "[" (str/clip 1 [1 2 3 4 5])))
(is (= "[1" (str/clip 2 [1 2 3 4 5])))
(is (= "[1 2" (str/clip 4 [1 2 3 4 5])))
(is (= "[1 2 3 4" (str/clip 8 [1 2 3 4 5])))
(is (= "[1 2 3 4 5]" (str/clip 16 [1 2 3 4 5]))))
(testing "map"
(is (= "" (str/clip 0 (sorted-map :a 1 :b 2))))
(is (= "{" (str/clip 1 (sorted-map :a 1 :b 2))))
(is (= "{:" (str/clip 2 (sorted-map :a 1 :b 2))))
(is (= "{:a " (str/clip 4 (sorted-map :a 1 :b 2))))
(is (= "{:a 1, :" (str/clip 8 (sorted-map :a 1 :b 2))))
(is (= "{:a 1, :b 2}" (str/clip 16 (sorted-map :a 1 :b 2)))))
(testing "set"
(let [tst-set (sorted-set 5 4 3 2 1)]
(is (= "" (str/clip 0 tst-set)))
(is (= "#" (str/clip 1 tst-set)))
(is (= "#{" (str/clip 2 tst-set)))
(is (= "#{1 " (str/clip 4 tst-set)))
(is (= "#{1 2 3 " (str/clip 8 tst-set)))
(is (= "#{1 2 3 4 5}" (str/clip 16 tst-set))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/set (t/All [x] [(t/Seqable x) :-> #?(:cljs (t/Set x)
:default (PersistentHashSet x))])
cc/hash-set (t/All [x] [x :* :-> #?(:cljs (t/Set x)
:default (PersistentHashSet x))])
;TODO
;cc/hash-map (t/All [x y z :..]
; (t/IFn [(t/cat z z) :.. z :-> (t/Assoc '{} z :.. z)]
; [(t/cat x y) :* :-> (t/Map x y)]))
cc/hash-map (t/All [x y] [(t/cat x y) :* :-> (t/Map x y)])
cc/array-map (t/All [x y] [(t/cat x y) :* :-> (t/Map x y)])
cc/sorted-map (t/All [x y] [(t/cat x y) :* :-> (t/SortedMap x y)])
cc/sorted-map-by (t/All [x y] [[x x :-> t/Int] (t/cat x y) :* :-> (t/SortedMap x y)])
cc/sorted-set (t/All [x] [x :* :-> (t/SortedSet x)])
;;FIXME use t/Comparator for first arg
cc/sorted-set-by (t/All [x] [[x x :-> t/Int] x :* :-> (t/SortedSet x)])
cc/list (t/All [x] [x :* :-> (#?(:clj PersistentList :cljs t/List) x)])
;cc/list* (t/All [x] [x :* (t/Seqable x) :-> (t/NilableNonEmptyASeq x)])
cc/list* (t/All [x] (t/IFn [(t/Seqable x) :-> (t/NilableNonEmptyASeq x)]
[x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]
[x x x x x x x x x x (t/Seqable x) :-> (t/NonEmptyASeq x)]))
bsless/clj-fast
(ns clj-fast.clojure.core-test
(:require [clj-fast.clojure.core :as sut]
[clojure.test :as t]))
(t/deftest test-subseq
(let [s1 (range 100)
s2 (into (sorted-set) s1)]
(doseq [i (range 100)]
(t/is (= s1 (concat (sut/subseq s2 < i) (sut/subseq s2 >= i))))
(t/is (= (reverse s1) (concat (sut/rsubseq s2 >= i) (sut/rsubseq s2 < i)))))))
dco-dev/interval-tree
(ns com.dean.interval-tree.tree.ordered-set
(:require [clojure.core.reducers :as r :refer [coll-fold]]
[clojure.set]
[com.dean.interval-tree.tree.node :as node]
[com.dean.interval-tree.tree.order :as order]
[com.dean.interval-tree.tree.protocol :as proto]
[com.dean.interval-tree.tree.root]
[com.dean.interval-tree.tree.tree :as tree])
(:import [clojure.lang RT]
[com.dean.interval_tree.tree.protocol PExtensibleSet]
[com.dean.interval_tree.tree.root INodeCollection
IBalancedCollection
IOrderedCollection]))
(time (def v (into (sorted-set) foo))) ;; 500K: "Elapsed time: 839.188189 msecs"
(time (def w (into (sorted-set) bar))) ;; 1M: "Elapsed time: 1974.798286 msecs"
(time (def s (clojure.set/intersection
(into (sorted-set) s0)
(into (sorted-set) s1)))) ;; 833K: "Elapsed time: 1589.786106 msecs"
(time (r/fold + + w)) ;; 1M: "Elapsed time: 167.916539 msecs"
(criterium.core/bench (def v (into (sorted-set) foo)))
(time (def z (into (avl/sorted-set) foo))) ;; 500K: "Elapsed time: 586.862601 msecs"
(time (def z (into (avl/sorted-set) bar))) ;; 1M: "Elapsed time: 1399.241718 msecs"
(criterium.core/bench (def z (into (avl/sorted-set) foo)))
dco-dev/interval-tree
(ns com.dean.interval-tree.ordered-set-test
(:require [clojure.core.reducers :as r]
[clojure.math.combinatorics :as combo]
[clojure.set :as set]
[clojure.test :refer :all]
[com.dean.interval-tree.core :refer :all]))
(deftest sets-of-various-size-and-element-types
(doseq [size [1 10 100 1000 10000 100000 250000 500000]
f [identity str gensym
#(java.util.Date. %)
(fn [_] (java.util.UUID/randomUUID))]]
(let [data (mapv f (shuffle (range size)))
this (ordered-set data)
that (apply sorted-set data)
afew (take 1000 data)]
(is (= that this))
(is (= that (into this afew)))
(is (= (apply disj that afew) (apply disj this afew)))
(is (= (seq this) (seq that)))
(is (= (count this) (count that)))
(is (every? #(= (nth this %) (->> that (drop %) first))
(take 10 (repeatedly #(rand-int size)))))
(is (every? #(= (this %) (that %)) afew)))))