简体   繁体   中英

Filter a set in Clojure clojure.set/select vs. clojure.core/filter

I would like to filter a set, something like:

(filter-set even? #{1 2 3 4 5})
; => #{2 4}

If I use clojure.core/filter I get a seq which is not a set:

(filter even? #{1 2 3 4 5})
; => (2 4)

So the best I came with is:

(set (filter even? #{1 2 3 4 5}))

But I don't like it, doesn't look optimal to go from set to list back to set. What would be the Clojurian way for this ?

UPDATE

I did the following to compare @A.Webb and @Beyamor approaches. Interestingly, both have almost identical performance, but clojure.set/select is slightly better.

(defn set-bench []
  (let [big-set (set (take 1000000 (iterate (fn [x] (int (rand 1000000000))) 1)))]
    (time (set (filter even? big-set))) ; "Elapsed time: 422.989 msecs"
    (time (clojure.set/select even? big-set))) ; "Elapsed time: 345.287 msecs"
    nil) ; don't break my REPL !

clojure.set is a handy API for common set operations.

In this case, clojure.set/select is a set-specific filter. It works by dissociating elements which don't meet the predicate from the given set.

(require 'clojure.set)

(clojure.set/select even? #{1 2 3 4 5})
; => #{2 4} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM