1 of 21

Core Transformation Functions

Ken Q Pu, Faculty of Science, Ontario Tech University

2 of 21

Comprehensive Guide

Ken Q Pu, Faculty of Science, Ontario Tech University

3 of 21

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

4 of 21

"Adding" to sequence

Ken Q Pu, Faculty of Science, Ontario Tech University

5 of 21

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

6 of 21

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

7 of 21

"Deleting" from sequence

Ken Q Pu, Faculty of Science, Ontario Tech University

8 of 21

Collections

(rest <collection>)

⇒ <collection>

A

B

C

D

B

C

D

Ken Q Pu, Faculty of Science, Ontario Tech University

9 of 21

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

10 of 21

"Adding" to hashmap

Ken Q Pu, Faculty of Science, Ontario Tech University

11 of 21

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

12 of 21

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

13 of 21

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

14 of 21

"Delete" from hashmap

Ken Q Pu, Faculty of Science, Ontario Tech University

15 of 21

Hashmaps

(dissoc <hashmap> <key1> <key2> ...)

⇒ <hashmap>

Removes one or more keys from the input hashmap

Ken Q Pu, Faculty of Science, Ontario Tech University

16 of 21

"Update" hashmap

Ken Q Pu, Faculty of Science, Ontario Tech University

17 of 21

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

18 of 21

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

19 of 21

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

20 of 21

Vectors

Vector is

  • an iterable sequence like lists
  • an indexed structure like hashmaps

(conj <vector> <element>)

(assoc <vector> <index> <element>)

(update <vector> <index> <update-fn>)

Ken Q Pu, Faculty of Science, Ontario Tech University

21 of 21

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