such.shorthand
Explicit functions for what could be done easily - but less clearly - by composing clojure.core functions. Anti-shibboleths such as using not-empty?
instead of seq
.
any?
(any? predlike coll)
any?
provides shorthand for “containment” queries that otherwise require different functions. Behavior depends on the type of predlike
.
A function:
true
iffpredlike
returns a truthy value for any value incoll
.(any? even? [1 2 3]) => true ; works best with boolean-valued functions (any? inc [1 2 3]) => true ; a silly example to demo truthiness. (any? identity [nil false]) => false ; also silly
A collection:
true
iffpredlike
contains any element ofcoll
.(any? #{1 3} [5 4 1]) => true (any? [1 3] [5 4 1]) => true
When
predlike
is a map, it checks key/value pairs:(any? {:a 1} {:a 1}) => true (any? {:a 1} {:a 2}) => false (any? {:a 2, :b 1} {:b 1, :c 3}) => true
A keyword:
true
iffpredlike
is a key incoll
, which must be a map.(any? :a {:a 1, :b 2}) => true ; equivalent to: (contains? {:a 1, :b 2} :a) => true
find-first
(find-first pred coll)
Returns the first item of coll
where (pred item)
returns a truthy value, nil
otherwise. coll
is evaluated lazily.
(find-first even? [1 2 3]) => 2
You can apply find-first
to a map, even though which element matches “first” is undefined. Note that the item passed to pred
will be a key-value pair:
(find-first #(even? (second %)) {:a 2, :b 22, :c 222}) => [:c 222]
fourth
(fourth coll)
Returns the fourth element of coll
. Returns nil
if there are fewer than four elements.
not-empty?
(not-empty? value)
Returns true
if value
has any values, false
otherwise. value
must be a collection, a String, a native Java array, or something that implements the Iterable
interface.
prog1
macro
(prog1 retform & body)
The retform
is evaluated, followed by the body
. The value of the form is returned, so the point of body
should be to have side-effects.
(defn pop! [k]
(prog1 (top k)
(alter! k clojure.core/pop)))
The name is a homage to older Lisps.
third
(third coll)
Returns the third element of coll
. Returns nil
if there are fewer than three elements.