Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pixie/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,11 @@ returns true"
(defn rem [num div]
(-rem num div))

(defn rand-int
{:doc "random integer between 0 (inclusive) and n (exclusive)"}
[n]
(rem (rand) n))

(defn =
{:doc "Returns true if all the arguments are equivalent. Otherwise, returns false. Uses
-eq to perform equality checks."
Expand Down Expand Up @@ -946,6 +951,16 @@ If further arguments are passed, invokes the method named by symbol, passing the
(fn [v]
(transduce ordered-hash-reducing-fn v)))

(defn select-keys
{:doc "Produces a map with only the values in m contained in key-seq"}
[m key-seq]
(with-meta
(transduce
(comp (filter (fn [k] (contains? m k)))
(map (fn [k] [k (get m k)])))
conj {} key-seq)
(meta m)))

(defn keys
{:doc "If called with no arguments returns a transducer that will extract the key from each map entry. If passed
a collection, will assume that it is a hashmap and return a vector of all keys from the collection."
Expand Down Expand Up @@ -1411,6 +1426,10 @@ The new value is thus `(apply f current-value-of-atom args)`."
(defn nil? [x]
(identical? x nil))

(defn some? [x]
{:doc "true if x is not nil"}
(not (nil? x)))

(defn fnil [f else]
(fn [x & args]
(apply f (if (nil? x) else x) args)))
Expand Down
14 changes: 14 additions & 0 deletions tests/pixie/tests/test-stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@
(t/assert= (set (vals v)) #{1 2 3})
(t/assert= (transduce (vals) conj! v) (vals v))))

(t/deftest test-select-keys
(let [m ^{:k :v} {:a 1 :b 2}]
(t/assert= (select-keys m [:a :b]) m)
(t/assert= :v
(-> (select-keys m [:a])
meta
:k))
(t/assert= (select-keys m [:a :not-found]) {:a 1})
(t/assert= (select-keys m nil) {})
(t/assert= (select-keys {} [:a]) {})))

(t/deftest test-empty
(t/assert= (empty '(1 2 3)) '())
Expand Down Expand Up @@ -273,6 +283,10 @@
(t/assert= (every? even? []) true)
(t/assert= (every? odd? []) true))

(t/deftest test-rand-int
(let [vs (repeatedly 10 #(rand-int 4))]
(t/assert (every? #(and (>= % 0) (< % 4)) vs))))

(t/deftest test-some
(t/assert= (some even? [2 4 6 8]) true)
(t/assert= (some odd? [2 4 6 8]) false)
Expand Down