Core Library: Collections Flashcards
What does this construct do? What are its arities and efficiencies?
count
(count coll)
Type: Function
Airity 1: Returns the number of items in a collection. (count nil) returns 0. Also works on strings, arrays, Java collections, and maps.
Runs in constant time for all Clojure data structures except lazy sequences, which take linear time to count (unless realized).
What does this construct do? What are its arities and efficiencies?
empty
(empty coll)
Type: Function
Airity 1: Returns an empty collection of the same category as coll, or nil if the input is not supported or not a collection. Output won’t necessarily be the same JVM class as the input. Metadata is copied.
Runs in constant time.
What does this construct do? What are its arities and efficiencies?
not-empty
(not-empty coll)
Type: Function
Airity 1: If coll is empty, returns nil, else coll. Same behavior for strings. Allows if-let to be used with collections.
Runs in constant time.
What does this construct do? What are its arities and efficiencies?
into
(into)
(into to)
(into to from)
(into to xform from)
Type: Function
Airity 0: Returns an empty vector.
Airity 1: Returns its argument.
Airity 2: Returns a new coll consisting of to-coll with all of the items of
from-coll conjoined.
Airity 3: Returns a new coll consisting of to-coll with all of the items of
from-coll conjoined and transduced by xform.
Runs in constant type for arities 0-1, linear time for arities 2-3.
What does this construct do? What are its arities and efficiencies?
conj
(conj coll x)
(conj coll x & xs)
Type: Function
Airity 2: conj[oin]. Returns a new collection with the xs
‘added’. (conj nil item) returns (item). The ‘addition’ may
happen at different ‘places’ depending on the concrete type.
Airity 2+: Same thing.
Conjoining a list happens at the beginning.
Conjoining a vector happens at the end.
Conjoining multiple items is done in order.
Conjoining to maps only takes items as vectors of length exactly 2.
Conjoining maps to maps conjoins the contents.
Conjoining a set to a set results in nested sets, use clojure.set/union to merge sets, not conj.
contains?
(contains? coll key)
Type: Function
Airity 2: Returns true if key is present in the given collection, otherwise
returns false. Note that for numerically indexed collections like
vectors and Java arrays, this tests if the numeric key is within the
range of indexes. ‘contains?’ operates constant or logarithmic time;
it will not perform a linear search for a value. See also ‘some’.
contains?
is straightforward for maps. It’s likely to surprise you for other sequences because it’s about indices or keys, not contents. Although lists are sequences, they are not keyed sequences. contains?
should not be used for lists.
distinct?
Type: Function
(distinct? x)
(distinct? x y)
(distinct? x y & more)
Returns true if no two of the arguments are =
Like min
or max
and unlike distinct
the parameters are taken separately, i.e. not in a collection.
empty?
Type: Function
(empty? coll)
Arity 1: Returns true if coll has no items - same as (not (seq coll)).
Please use the idiom (seq x) rather than (not (empty? x))
Collections containing nil are not empty.
every?
(every? pred coll)
Arity 2: Returns true if (pred x) is logical true for every x in coll, else
false.
Can be a substitute for an (apply and coll) which can’t be done directly because (and) is a macro.
Returns true for empty collections regardless of criteria. As such, a better description of every? would be: Returns false if there exists a value x in coll such that (pred? x) is false, else true.
You can use every? with a set as the predicate to return true if every member of a collection is in the set or use a hash-map as the predicate with every? to return true if every member of a collection is a key within the map.
not-every?
(not-every? pred coll)
Arity 2: Returns false if (pred x) is logical true for every x in
coll, else true.
some
(some pred coll)
Arity 2: Returns the first logical true value of (pred x) for any x in coll,
else nil. One common idiom is to use a set as pred, for example
this will return :fred if :fred is in the sequence, otherwise nil:
(some #{:fred} coll)
Can be a substitute for an (apply or coll) which can’t be done directly because (or) is a macro.
Remember this function short-circuits and does not test all values.
Be careful, ‘nil’ can occasionally be returned on success.
(#{nil} nil)
Almost as troublesome is returning a false
(#{false} false)
not-any?
(not-any? pred coll)
Arity 2: Returns false if (pred x) is logical true for any x in coll,
else true.
Can use the following to create an any?
predicate:(def any? (complement not-any?))
Note that some
is like any?
but it’s not technically a predicate.
sequential?
(sequential? coll)
Returns true if coll implements Sequential
Lists, vectors, and lazy seqs are examples of sequentials.
Be careful while traversing data. Map entries are vectors and thus sequential!
associative?
(associative? coll)
Returns true if coll implements Associative
Maps and vectors are examples of associatives.
sorted?
(sorted? coll)
Returns true if coll implements Sorted
Sorted sets and sorted maps are examples of sorteds.
Note you can’t just pass in a collection that happens to be sorted. This is specifically testing if the data structure supports upkeeping a sort as it grows or shrinks.