Core Library: Collections Flashcards

1
Q

What does this construct do? What are its arities and efficiencies?

count

A

(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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does this construct do? What are its arities and efficiencies?

empty

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does this construct do? What are its arities and efficiencies?

not-empty

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this construct do? What are its arities and efficiencies?

into

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does this construct do? What are its arities and efficiencies?

conj

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

contains?

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

distinct?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

empty?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

every?

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

not-every?

A

(not-every? pred coll)

Arity 2: Returns false if (pred x) is logical true for every x in
coll, else true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

some

A

(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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

not-any?

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

sequential?

A

(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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

associative?

A

(associative? coll)

Returns true if coll implements Associative

Maps and vectors are examples of associatives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

sorted?

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

counted?

A

(counted? coll)

Returns true if coll implements count in constant time

Examples include vectors, lists, maps, and sets.

17
Q

reversible?

A

(reversible? coll)

Returns true if coll implements Reversible

Examples includ vectors, sorted maps, and sorted sets.

Lists, maps, and sets are NOT reversible.

18
Q

seqable?

A

(seqable? x)

Return true if the seq function is supported for x

nil is seqable.
Anything that implements CharSequence is seqable.
Anything that implements Seqable is seqable (Clojure data structures/lazy seqs).
Anything that implements Iterable is seqable.
Arrays are seqable.

Some stuff that isn’t seqable: terminal values like characters, booleans, numbers, and functions.

19
Q

coll?

A

(coll? x)

Returns true if x implements IPersistentCollection

Examples: Maps, sets, vectors, lists, string seqs, etc.
Non-examples: Numbers, strings, booleans, nil.

20
Q

seq?

A

(seq? x)

Return true if x implements ISeq

Examples include actual seqs, lazy seqs, and lists.
Non-examples: Vectors, terminals, maps, and sets.

21
Q

vector?

A

(vector? x)

Return true if x implements IPersistentVector

Be careful while traversing data, map entries are vectors!

22
Q

list?

A

(list? x)

Returns true if x implements IPersistentList

;; not all lists are lists
(cons 1 '(2 3))
;; => (1 2 3)
(= '(1 2 3) (cons 1 '(2 3)))
;; => true
(list? (cons 1 '(2 3)))
;; => false
23
Q

map?

A

(map? x)

Return true if x implements IPersistentMap

Note that Clojure records are also maps.

24
Q

set?

A

(set? x)

Returns true if x implements IPersistentSet

Pretty self explanatory.