Clojure · v1.0.1
2026-11-14 14:53:58
nil and false are logically false; 0 and "" are true(:name m) looks itself up in m| Collection | Literal | Good at | conj adds |
|---|---|---|---|
| list | '(1 2 3) |
the front | (conj '(1 2) 0) ⇒ (0 1 2) |
| vector | [1 2 3] |
indexing, the end | (conj [1 2] 3) ⇒ [1 2 3] |
| map | {:a 1 :b 2} |
lookup by key | (assoc {:a 1} :b 2) ⇒ {:a 1, :b 2} |
first, rest and cons work on every collection[key value] pairif, when, if-not, when-not, cond: choose a valueloop / recur: repetition without growing the stack->> threads a value through the last argument; -> through the firstA registrar keeps three kinds of record:
Questions to answer: transcripts, GPAs, students at risk, course averages, prerequisites, eligibility, the dean’s list.
| What | Representation | Why |
|---|---|---|
| an entity | a map with keyword keys | self-describing; keywords look themselves up |
| an identifier | a keyword :csci1060, or an integer |
evaluates to itself; cheap equality |
| a lookup table | a map from id to entity | constant-time lookup |
| a relation | a vector of maps | one row per fact; every sequence function applies |
| membership | a set | the question asked of it is “is this a member?” |
No class is defined: the library’s functions already work on every one of these.
First version
Fall sorts before Winter of the same year
Choose a representation that makes the standard functions correct.
points is a map used as a function: (points :A) is 4.0grade-letter composes two functions: :grade first, then letter:grade, credits, grade-letterpassed?(of-student 4) is a functionjuxt applies four projections to each enrollment, producing one rowgroup-by partitions the relation by a key function[[id es]] destructures each group in the parameter listat-risk is a query over another query’s resultfrequencies counts each distinct valueevery-pred joins predicates: true when all of them hold(juxt :term :student) is a composite keygpa computes a term GPA, unchangedcomp turns two lookups into one projection| Piece | Used by |
|---|---|
grade-letter |
Q1, Q5, Q9, passed?, gpa |
gpa |
Q2, Q3, Q8 |
of-student, in-course |
Q1, Q5, Q6, Q9 |
completed |
Q6, Q7 |
name-of |
Q2, Q7, Q8, Q9 |
Each query is a pipeline of small functions; most of the pieces are shared.
| Function | Does | Used in |
|---|---|---|
group-by |
partitions a collection by a key function | Q2, Q4, Q8 |
frequencies |
counts each distinct value | Q5 |
juxt |
applies several functions, collects a vector | Q1, Q8 |
comp |
composes functions, right to left | grade-letter, Q9 |
every-pred |
joins predicates with “and” | Q6, Q9 |
sort-by, min-key |
order or select by a key function | Q1, Q4 |
set/subset? |
set containment | Q6 |
defn, let, loop/recur, and the threading macrosBuilding Blocks of Clojure — the next unit in this module.