Back
select (clj)
(source)function
(select pred xset)
Returns a set of the elements for which pred is true
Examples
clojure
(ns clojure.test-clojure.clojure-set
(:use clojure.test)
(:require [clojure.set :as set]))
(deftest test-select
(are [x y] (= x y)
(set/select integer? #{}) #{}
(set/select integer? #{1 2}) #{1 2}
(set/select integer? #{1 2 :a :b :c}) #{1 2}
(set/select integer? #{:a :b :c}) #{}) )
clojure
(ns clojure.test-clojure.metadata
(:use clojure.test
[clojure.test-helper :only (eval-in-temp-ns)])
(:require [clojure.set :as set]))
(deftest interaction-of-def-with-metadata
(testing "initial def sets metadata"
(let [v (eval-in-temp-ns
(def ^{:a 1} foo 0)
#'foo)]
(is (= 1 (-> v meta :a)))))
(testing "const vars preserve metadata"
(let [[v1 v2] (eval-in-temp-ns
(def ^:const foo ^:foo [])
(def ^:const bar ^:foo [:bar])
[(meta foo) (meta bar)])]
(is (= {:foo true} v1))
(is (= {:foo true} v2))))
#_(testing "subsequent declare doesn't overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:b 2} bar 0)
(declare bar)
#'bar)]
(is (= 2 (-> v meta :b))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:c 3} bar 0)
(defn declare-bar []
(declare bar))
(declare-bar)
#'bar)]
(is (= 3 (-> v meta :c))))))
(testing "subsequent def with init-expr *does* overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:d 4} quux 0)
(def quux 1)
#'quux)]
(is (nil? (-> v meta :d))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:e 5} quux 0)
(defn def-quux []
(def quux 1))
(def-quux)
#'quux)]
(is (nil? (-> v meta :e))))))
(testing "IllegalArgumentException should not be thrown"
(testing "when defining var whose value is calculated with a primitive fn."
(testing "This case fails without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (inc (foo 10))))))
(testing "This case should pass even without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (foo (inc 10)))))))))
(deftest fns-preserve-metadata-on-maps
(let [xm {:a 1 :b -7}
x (with-meta {:foo 1 :bar 2} xm)
ym {:c "foo"}
y (with-meta {:baz 4 :guh x} ym)]
(is (= xm (meta (:guh y))))
(is (= xm (meta (reduce #(assoc %1 %2 (inc %2)) x (range 1000)))))
(is (= xm (meta (-> x (dissoc :foo) (dissoc :bar)))))
(let [z (assoc-in y [:guh :la] 18)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(let [z (update-in y [:guh :bar] inc)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(is (= xm (meta (get-in y [:guh]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (merge x y))))
(is (= ym (meta (merge y x))))
(is (= xm (meta (merge-with + x y))))
(is (= ym (meta (merge-with + y x))))
(is (= xm (meta (select-keys x [:bar]))))
(is (= xm (meta (set/rename-keys x {:foo :new-foo}))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; TBD: rseq, subseq, and rsubseq returns seqs. If it is even
;; possible to put metadata on a seq, does it make sense that the
;; seqs returned by these functions should have the same metadata
;; as the sorted collection on which they are called?
))
(deftest fns-preserve-metadata-on-vectors
(let [xm {:a 1 :b -7}
x (with-meta [1 2 3] xm)
ym {:c "foo"}
y (with-meta [4 x 6] ym)]
(is (= xm (meta (y 1))))
(is (= xm (meta (assoc x 1 "one"))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (pop (pop (pop x))))))
(let [z (assoc-in y [1 2] 18)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(let [z (update-in y [1 2] inc)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(is (= xm (meta (get-in y [1]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (replace {2 "two"} x))))
(is (= [1 "two" 3] (replace {2 "two"} x)))
;; TBD: Currently subvec drops metadata. Should it preserve it?
;;(is (= xm (meta (subvec x 2 3))))
;; TBD: rseq returns a seq. If it is even possible to put
;; metadata on a seq, does it make sense that the seqs returned by
;; these functions should have the same metadata as the sorted
;; collection on which they are called?
))
(deftest fns-preserve-metadata-on-sets
;; TBD: Do tests independently for set, hash-set, and sorted-set,
;; perhaps with a loop here.
(let [xm {:a 1 :b -7}
x (with-meta #{1 2 3} xm)
ym {:c "foo"}
y (with-meta #{4 x 6} ym)]
(is (= xm (meta (y #{3 2 1}))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (-> x (disj 1) (disj 2) (disj 3)))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (set/select even? x))))
(let [cow1m {:what "betsy cow"}
cow1 (with-meta {:name "betsy" :id 33} cow1m)
cow2m {:what "panda cow"}
cow2 (with-meta {:name "panda" :id 34} cow2m)
cowsm {:what "all the cows"}
cows (with-meta #{cow1 cow2} cowsm)
cow-names (set/project cows [:name])
renamed (set/rename cows {:id :number})]
(is (= cowsm (meta cow-names)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) cow-names)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) cow-names)))))
(is (= cowsm (meta renamed)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) renamed)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) renamed))))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; union: Currently returns the metadata of the largest input set.
;; This is an artifact of union's current implementation. I doubt
;; any explicit design decision was made to do so. Like join,
;; there doesn't seem to be much reason to prefer the metadata of
;; one input set over another, if at least two input sets are
;; given, but perhaps defining it to always return a set with the
;; metadata of the first input set would be reasonable?
;; intersection: Returns metadata of the smallest input set.
;; Otherwise similar to union.
;; difference: Seems to always return a set with metadata of first
;; input set. Seems reasonable. Not sure we want to add a test
;; for it, if it is an accident of the current implementation.
;; join, index, map-invert: Currently always returns a value with
;; no metadata. This seems reasonable.
))
clojure
(ns clojure.test-clojure.multimethods
(:use clojure.test [clojure.test-helper :only (with-var-roots)])
(:require [clojure.set :as set]))
(defn hierarchy-tags
"Return all tags in a derivation hierarchy"
[h]
(set/select
#(instance? clojure.lang.Named %)
(reduce into #{} (map keys (vals h)))))
(defn tag-descendants
"Set of descedants which are tags (i.e. Named)."
[& args]
(set/select
#(instance? clojure.lang.Named %)
(or (apply descendants args) #{})))
metabase/metabase
(ns metabase-enterprise.sandbox.api.user
"Endpoint(s)for setting user attributes."
(:require
[clojure.set :as set]
[compojure.core :refer [GET PUT]]
[metabase.api.common :as api]
[metabase.models.user :refer [User]]
[metabase.util.i18n :refer [deferred-tru]]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
;; TODO - not sure we need this endpoint now that we're just letting you edit from the regular `PUT /api/user/:id
;; endpoint
(api/defendpoint PUT "/:id/attributes"
"Update the `login_attributes` for a User."
[id :as {{:keys [login_attributes]} :body}]
{id ms/PositiveInt
login_attributes [:maybe UserAttributes]}
(api/check-404 (t2/select-one User :id id))
(pos? (t2/update! User id {:login_attributes login_attributes})))
(api/defendpoint GET "/attributes"
"Fetch a list of possible keys for User `login_attributes`. This just looks at keys that have already been set for
existing Users and returns those. "
[]
(->>
;; look at the `login_attributes` for the first 1000 users that have them set. Then make a set of the keys
(for [login-attributes (t2/select-fn-set :login_attributes User :login_attributes [:not= nil] {:limit 1000})
:when (seq login-attributes)]
(set (keys login-attributes)))
;; combine all the sets of attribute keys into a single set
(reduce set/union #{})))
borkdude/speculative
(ns speculative.set
(:require [clojure.set :as set]
[clojure.spec.alpha :as s]
[speculative.specs :as ss]))
(s/fdef set/select
:args (s/cat :pred ::ss/predicate
:xset ::nilable-set)
:ret ::nilable-set)
district0x/district0x-network-token
(ns contribution.subs
(:require
[cljs-time.core :as t]
[cljs-web3.core :as web3]
[contribution.constants :as constants]
[district0x.utils :as u]
[goog.string :as gstring]
[goog.string.format]
[medley.core :as medley]
[re-frame.core :refer [reg-sub]]
[clojure.set :as set]
[clojure.string :as string]))
(reg-sub
:contribution/configuration
(fn [db]
(merge
(select-keys db [:contribution/stopped? :contribution/founder1 :contribution/founder2
:contribution/early-sponsor :contribution/wallet :contribution/advisers
:contribution/max-gas-price])
{:contribution-address (get-in db [:smart-contracts :contribution :address])
:dnt-token-address (get-in db [:smart-contracts :dnt-token :address])})))
stuarthalloway/exploring-clojure
(ns exploring.persistent-data-structures
(:require
[clojure.repl :refer :all]
[clojure.set :as set]))
(select-keys m [:a :d])
NoahTheDuke/coc-clojure
(ns build-commands
(:require
[babashka.fs :as fs]
[babashka.process :as p]
[cheshire.core :refer [generate-string parse-string]]
[clojure.set :as set]
[clojure.string :as str]))
(def commands-for-package
(->> (concat commands-json aliases)
(map #(-> (set/rename-keys % {:description :title})
(assoc :command (str "lsp-clojure-" (:command %)))
(select-keys [:command :title])))
(sort-by :command)))