Core Transformation Functions
Ken Q Pu, Faculty of Science, Ontario Tech University
Comprehensive Guide
Ken Q Pu, Faculty of Science, Ontario Tech University
Collections
(count <sequence>)
⇒ number
1
2
3
4
4
Clojure supports infinite streams as sequences, so make sure that <sequence> is finite to ensure termination.
Ken Q Pu, Faculty of Science, Ontario Tech University
"Adding" to sequence
Ken Q Pu, Faculty of Science, Ontario Tech University
Collections
(cons <element> <collection>)
⇒ <collection>
A
B
C
D
A
B
C
D
Always returns a list
( )
Ken Q Pu, Faculty of Science, Ontario Tech University
Collections
(conj <collection> <element>)
⇒ <collection>
A
B
C
D
A
B
C
D
Add the <element> into <collection> in the most efficient way.
[ ]
[ ]
A
B
C
D
A
B
C
D
( )
( )
Ken Q Pu, Faculty of Science, Ontario Tech University
"Deleting" from sequence
Ken Q Pu, Faculty of Science, Ontario Tech University
Collections
(rest <collection>)
⇒ <collection>
A
B
C
D
B
C
D
Ken Q Pu, Faculty of Science, Ontario Tech University
Collections
(pop <collection>)
⇒ <collection>
A
B
C
D
B
C
D
A
B
C
D
A
B
C
Removes an element in the most efficient way possible.
For vectors, remove the last element.
For lists, remove the first element.
Ken Q Pu, Faculty of Science, Ontario Tech University
"Adding" to hashmap
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(assoc <hashmap> <key> <value> <key> <value>)
⇒ <hashmap>
(assoc {:name "Ken" :course "CSCI 3055U"}
:name "Ken Pu"
:role "instructor")
=>
{:name "Ken Pu"
:course "CSCI 3055U"
:role "instructor"}
Inserts key value pairs into the input hashmap. If the key already exists, the previous value is overwritten by the new value.
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(assoc-in <hashmap> [ k1 k2 ... ] <value>)
⇒ <hashmap>
(def person {:name "Albert Einstein"
:address {:city "Princeton"
:state "New Jersey"}})
(assoc-in person
[:address :country] "United States")
=>
{:name "Albert Einstein"
:address {:city "Princeton"
:state "New Jersey"
:country "United State"}}
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(assoc-in <hashmap> [ k1 k2 ... ] <value>)
⇒ <hashmap>
(def person {:name "Albert Einstein"
:address {:city "Princeton"
:state "New Jersey"}})
(assoc-in person
[:address :country] "United States")
(assoc person
:address (assoc (person :address)
:country "United States"))
assoc-in is not strictly required as it can be replaced by nested assoc forms.
But the latter is much more verbose.
Ken Q Pu, Faculty of Science, Ontario Tech University
"Delete" from hashmap
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(dissoc <hashmap> <key1> <key2> ...)
⇒ <hashmap>
Removes one or more keys from the input hashmap
Ken Q Pu, Faculty of Science, Ontario Tech University
"Update" hashmap
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(update <hashmap> <key> <fn> <args...>)
⇒ <hashmap>
update is a functional way of changing a value in a hashmap.
{:name " programming "
:code "csci 3055u"}
"Programming"
Update function
(fn [x]
(capitalize (trim x)))
(update course-info
:name (fn [x] (capitalize (trim x))))
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(update <hashmap> <key> <fn> <args...>)
⇒ <hashmap>
(def after-tax-income [income tax-rate]
(- income (* income tax-rate)))
(def before-income-cheque
{:payable-to "Ken Pu"
:amount 1000 })
(update before-income-cheque
:amount after-tax-income 0.25 )
{:payable-to "Ken Pu"
:amount (after-tax-income 1000 0.25)}
Ken Q Pu, Faculty of Science, Ontario Tech University
Hashmaps
(update-in <hashmap> [k1 k2 ...] <fn> <args...>)
⇒ <hashmap>
This is the natural generalization to update nested hashmaps.
Ken Q Pu, Faculty of Science, Ontario Tech University
Vectors
Vector is
(conj <vector> <element>)
(assoc <vector> <index> <element>)
(update <vector> <index> <update-fn>)
Ken Q Pu, Faculty of Science, Ontario Tech University
Example of hybrid structure
(def course-outline
{:name "CSCI 3055U"
:topics [ {:name "Lambda Calculus"
:weeks 2}
{:name "Clojure"
:weeks 5}
{:name "Kotlin"
:weeks 3} ]})
(update-in course-outline
[:topics 1 :weeks]
dec)
(update-in course-outline
[:topics 2 :weeks]
inc)
(let [a (update-in course-outline
[:topics 1 :weeks]
dec)]
(update-in a
[:topics 2 :weeks]
inc))
We will introduce threading forms to build data processing pipelines easily.
Ken Q Pu, Faculty of Science, Ontario Tech University