0% found this document useful (0 votes)
41 views

Intro To Clojure Part 2

The document discusses working with Clojure code including using a read-eval-print loop, text editor and running code at the terminal. It also discusses Clojure project structure and using rest arguments, apply, sets and baking simulation commands.

Uploaded by

Ajay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Intro To Clojure Part 2

The document discusses working with Clojure code including using a read-eval-print loop, text editor and running code at the terminal. It also discusses Clojure project structure and using rest arguments, apply, sets and baking simulation commands.

Uploaded by

Ajay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 244

LispCast

Introduction to Clojure
Part 2

Friday, June 7, 13 1
generously sponsored by

JAM
lambdajam.com

Friday, June 7, 13 2
generously sponsored by

juxt.pro

Friday, June 7, 13 3
Friday, June 7, 13 4
X5’s second day
at baking school

Friday, June 7, 13 4
X5’s second day
at baking school
Managing ingredients

Friday, June 7, 13 4
X5’s second day
at baking school
Managing ingredients
Filling daily orders

Friday, June 7, 13 4
Friday, June 7, 13 5
working with Clojure code

Friday, June 7, 13 5
working with Clojure code

read-eval-print loop

Friday, June 7, 13 5
working with Clojure code

read-eval-print loop

text editor

Friday, June 7, 13 5
working with Clojure code

read-eval-print loop

text editor

running at the terminal

Friday, June 7, 13 5
Friday, June 7, 13 6
clojureintro/
project.clj
README.md
test/
src/
clojureintro/

core.clj

Friday, June 7, 13 6
clojureintro/
project.clj
README.md
test/
src/
clojureintro/

core.clj

Friday, June 7, 13 6
clojureintro/
project.clj
README.md
test/
src/
clojureintro/

core.clj

Friday, June 7, 13 6
clojureintro/
project.clj
README.md
test/
src/
clojureintro/

core.clj

Friday, June 7, 13 6
clojureintro/
project.clj
README.md
test/
src/
clojureintro/

core.clj

Friday, June 7, 13 6
Friday, June 7, 13 7
rest argument

Friday, June 7, 13 7
rest argument

(defn my-function [a b c]
(println (+ a b) c))

Friday, June 7, 13 7
rest argument
3 arguments

(defn my-function [a b c]
(println (+ a b) c))

Friday, June 7, 13 7
rest argument
3 or more arguments

(defn my-function [a b c & rs]


(println (+ a b) c)
(println rs))

Friday, June 7, 13 7
rest argument
any number of aguments

(defn my-function [& rs]


(println rs))

Friday, June 7, 13 7
Friday, June 7, 13 8
apply

Friday, June 7, 13 8
apply

(apply println [1 2 3])

Friday, June 7, 13 8
apply
function

(apply println [1 2 3])

Friday, June 7, 13 8
apply
function arguments (0 or more)

(apply println [1 2 3])

Friday, June 7, 13 8
apply
function arguments (0 or more)

(apply println [1 2 3])

(println 1 2 3)

Friday, June 7, 13 8
Friday, June 7, 13 9
warm-up

Write a function error that takes any number


of arguments, prints them using println, and
returns :error.

Friday, June 7, 13 9
Friday, June 7, 13 10
warm-up

Refactor all of the error conditions in


the program to use the new error
function. Hint: search for :error to
find the instances.

Friday, June 7, 13 10
Friday, June 7, 13 11
a day at the bakery

Friday, June 7, 13 11
a day at the bakery
get orders for the day

Friday, June 7, 13 11
a day at the bakery
get orders for the day

fetch needed ingredients

Friday, June 7, 13 11
a day at the bakery
get orders for the day

fetch needed ingredients

bake orders

Friday, June 7, 13 11
a day at the bakery
get orders for the day

fetch needed ingredients

bake orders

give receipts to the delivery bot

Friday, June 7, 13 11
Friday, June 7, 13 12
locations

Friday, June 7, 13 12
locations
:prep-area for baking

Friday, June 7, 13 12
locations
:prep-area for baking

:pantry for dry storage

Friday, June 7, 13 12
locations
:prep-area for baking

:pantry for dry storage

:fridge for cold storage

Friday, June 7, 13 12
locations
:prep-area for baking

:pantry for dry storage

:fridge for cold storage

example
(go-to :prep-area)

Friday, June 7, 13 12
Friday, June 7, 13 13
commands

Friday, June 7, 13 13
commands

go-to

Friday, June 7, 13 13
commands

go-to

load-up

Friday, June 7, 13 13
commands

go-to

load-up

unload

Friday, June 7, 13 13
commands

go-to

load-up

unload

bakery-help

Friday, June 7, 13 13
Friday, June 7, 13 14
sets #{ }

Friday, June 7, 13 14
sets #{ }
order does not matter

Friday, June 7, 13 14
sets #{ }
order does not matter
no duplicates

Friday, June 7, 13 14
sets #{ }
order does not matter
no duplicates
fast containment check

Friday, June 7, 13 14
sets #{ }
order does not matter
no duplicates
fast containment check
contains?

Friday, June 7, 13 14
sets #{ }
order does not matter
no duplicates
fast containment check
contains?
put any value in it

Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}

Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}

(contains? #{1 2 3 “Hello!”} “Hello!”)

Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}

(contains? #{1 2 3 “Hello!”} “Hello!”)


=> true

Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}

(contains? #{1 2 3 “Hello!”} “Hello!”)


=> true
(contains? #{1 2 3 “Hello!”} 10)

Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}

(contains? #{1 2 3 “Hello!”} “Hello!”)


=> true
(contains? #{1 2 3 “Hello!”} 10)
=> false

Friday, June 7, 13 14
Friday, June 7, 13 15
exercise 1

Write fridge-ingredients
and from-fridge? using this
new idiom.

Friday, June 7, 13 15
Friday, June 7, 13 16
exercise 2

Refactor scooped?, squeezed?, and


simple? to use this new idiom.

Friday, June 7, 13 16
Friday, June 7, 13 17
exercise 3
Write a function fetch-ingredient which
takes the name of an ingredient and an
amount and fetches that amount of the
ingredient. Also make it accept just the
ingredient, with a default amount of 1.

Friday, June 7, 13 17
exercise 3
Write a function fetch-ingredient which
takes the name of an ingredient and an
amount and fetches that amount of the
ingredient. Also make it accept just the
ingredient, with a default amount of 1.

examples
(fetch-ingredient :milk 10)
(fetch-ingredient :flour 14)
(fetch-ingredient :sugar)
Friday, June 7, 13 17
Friday, June 7, 13 18
maps { }

Friday, June 7, 13 18
maps { }
key, value pairs

Friday, June 7, 13 18
maps { }
key, value pairs
order does not matter

Friday, June 7, 13 18
maps { }
key, value pairs
order does not matter
fast lookup by key

Friday, June 7, 13 18
maps { }
key, value pairs
order does not matter
fast lookup by key
get

Friday, June 7, 13 18
maps { }
key, value pairs
order does not matter
fast lookup by key
get
contains?

Friday, June 7, 13 18
maps { }
key, value pairs
order does not matter
fast lookup by key
get
contains?
keys and values can be any type

Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})

Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})

(get ingredients :flour)

Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})

(get ingredients :flour) => 3

Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})

(get ingredients :flour) => 3


(contains? ingredients :milk)

Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})

(get ingredients :flour) => 3


(contains? ingredients :milk) => false

Friday, June 7, 13 18
Friday, June 7, 13 19
IFn

Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)

Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)

Fn -- for Clojure’s functions (like in defn)

Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)

Fn -- for Clojure’s functions (like in defn)

one method: invoke (called internally)

Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)

Fn -- for Clojure’s functions (like in defn)

one method: invoke (called internally)

functions, keywords, maps, and sets

Friday, June 7, 13 19
Friday, June 7, 13 20
seq

Friday, June 7, 13 20
seq
short for sequenence

two methods: first and rest

Friday, June 7, 13 20
seq
short for sequenence

two methods: first and rest

all Clojure and Java collections and Strings

Friday, June 7, 13 20
Friday, June 7, 13 21
doseq
iteration over items in a sequence

Friday, June 7, 13 21
doseq
iteration over items in a sequence

(doseq [ingredient fridge-ingredients]


(println “I like” ingredient))

Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable

(doseq [ingredient fridge-ingredients]


(println “I like” ingredient))

Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable seq

(doseq [ingredient fridge-ingredients]


(println “I like” ingredient))

Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable seq

(doseq [ingredient fridge-ingredients]


(println “I like” ingredient))

body

Friday, June 7, 13 21
Friday, June 7, 13 22
a day at the bakery
get morning orders

Friday, June 7, 13 22
a day at the bakery
get morning orders

fetch necessary ingredients

Friday, June 7, 13 22
a day at the bakery
get morning orders

fetch necessary ingredients

bake the items

Friday, June 7, 13 22
a day at the bakery
get morning orders

fetch necessary ingredients

bake the items

give a receipt to delivery bot

Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders

fetch necessary ingredients

bake the items

give a receipt to delivery bot

Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders

fetch necessary ingredients fetch...

bake the items

give a receipt to delivery bot

Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders

fetch necessary ingredients fetch...

bake the items bake...

give a receipt to delivery bot

Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders

fetch necessary ingredients fetch...

bake the items bake...

give a receipt to delivery bot delivery

Friday, June 7, 13 22
Friday, June 7, 13 23
orders

Friday, June 7, 13 23
orders

{:orderid 123
:address “323 Robot Ln”
:items {:cake 14
:cookies 12}}

Friday, June 7, 13 23
Friday, June 7, 13 24
receipts

Friday, June 7, 13 24
receipts
{:orderid 123
:address “323 Robot Ln”
:rackids [:cooling-rack-324
:cooling-rack-325
:cooling-rack-326]}

Friday, June 7, 13 24
Friday, June 7, 13 25
exercise 4
Write a function day-at-the-bakery
which will represent what X5 does at
the bakery.

Friday, June 7, 13 25
exercise 4
Write a function day-at-the-bakery
which will represent what X5 does at
the bakery.

Requirements:

Friday, June 7, 13 25
exercise 4
Write a function day-at-the-bakery
which will represent what X5 does at
the bakery.

Requirements:

•Get the orders

Friday, June 7, 13 25
exercise 4
Write a function day-at-the-bakery
which will represent what X5 does at
the bakery.

Requirements:

•Get the orders


•Bake items

Friday, June 7, 13 25
exercise 4
Write a function day-at-the-bakery
which will represent what X5 does at
the bakery.

Requirements:

•Get the orders


•Bake items
•Send receipts to the delivery bot
Friday, June 7, 13 25
Friday, June 7, 13 26
exercise 5
Name some things that can be improved in this
function. They can include ways to make it
more efficient (saving battery life), less
annoying to the delivery bot, or better code.
Name one way to fix each problem.

Friday, June 7, 13 26
Friday, June 7, 13 27
answers

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.


X5 should deliver one receipt per order.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.


X5 should deliver one receipt per order.

We repeat the code for cakes and cookies.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.


X5 should deliver one receipt per order.

We repeat the code for cakes and cookies.


We should factor out the common code.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.


X5 should deliver one receipt per order.

We repeat the code for cakes and cookies.


We should factor out the common code.

We are calling fetch-list on an inline map.

Friday, June 7, 13 27
answers
X5 will make many trips to the pantry and fridge.
X5 should make one trip before baking.

X5 will deliver a receipt for every item.


X5 should deliver one receipt per order.

We repeat the code for cakes and cookies.


We should factor out the common code.

We are calling fetch-list on an inline map.


We should extract the map and name it.
Friday, June 7, 13 27
Friday, June 7, 13 28
functional programming

Friday, June 7, 13 28
functional programming
Side effects

Friday, June 7, 13 28
functional programming
Side effects Pure functions

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world”

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world”


may be different each time

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world”


may be different each time
fetching ingredients

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world” “in you head”


may be different each time
fetching ingredients

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world” “in you head”


may be different each time make list of ingredients
fetching ingredients

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world” “in you head”


may be different each time make list of ingredients
fetching ingredients same answer every time

Friday, June 7, 13 28
functional programming
Side effects Pure functions

“change the world” “in you head”


may be different each time make list of ingredients
fetching ingredients same answer every time
referential transparency

Friday, June 7, 13 28
combining two lists of ingredients

List 1 List 2

5 eggs 2 cups of sugar

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour 5 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour 5 cups of flour

Friday, June 7, 13 29
combining two lists of ingredients

List 1 + List 2 = List 3

5 eggs 2 cups of sugar 6 eggs

2 butters 1 egg 2 butters

3 cups of flour 2 cups of flour 5 cups of flour

2 cups of sugar

Friday, June 7, 13 29
Friday, June 7, 13 30
merge-with

Friday, June 7, 13 30
merge-with

(merge-with + a b)

Friday, June 7, 13 30
merge-with
function

(merge-with + a b)

Friday, June 7, 13 30
merge-with
function two maps

(merge-with + a b)

Friday, June 7, 13 30
Friday, June 7, 13 31
exercise 6

Write a function add-ingredients which takes


two ingredient lists and adds them together using
merge-with.

Friday, June 7, 13 31
multiplying a list by a number

List 1 x 10

5 eggs

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters 10

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters 10 20 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters 10 20 butters

3 cups of flour

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters 10 20 butters

3 cups of flour 10

Friday, June 7, 13 32
multiplying a list by a number

List 1 x 10 = List 2

5 eggs 10 50 eggs

2 butters 10 20 butters

3 cups of flour 10 30 cups of flour

Friday, June 7, 13 32
Friday, June 7, 13 33
into

Friday, June 7, 13 33
into

(into {} [[:x 1] [:y 2] [:z 19]])


=> {:x 1 :y 2 :z 19}

Friday, June 7, 13 33
into
collection to add to

(into {} [[:x 1] [:y 2] [:z 19]])


=> {:x 1 :y 2 :z 19}

Friday, June 7, 13 33
into
collection to add to seq of items

(into {} [[:x 1] [:y 2] [:z 19]])


=> {:x 1 :y 2 :z 19}

Friday, June 7, 13 33
Friday, June 7, 13 34
REPL exploration
What happens if a key appears twice when
using into?

Friday, June 7, 13 34
REPL exploration
What happens if a key appears twice when
using into?

Try to guess what these expressions return


and then try them out in a REPL.

Friday, June 7, 13 34
REPL exploration
What happens if a key appears twice when
using into?

Try to guess what these expressions return


and then try them out in a REPL.

(into {} [[:x 1] [:x 2] [:x 5]])

(into {:x 5} [[:x 1] [:y 7]])

Friday, June 7, 13 34
Friday, June 7, 13 35
for

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2))

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x))

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x)) => (2 2 2)

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x)) => (2 2 2)

(for [[k v] {:x 1 :y 2 :z 3}]


[k (* 2 v)])

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x)) => (2 2 2)

(for [[k v] {:x 1 :y 2 :z 3}]


[k (* 2 v)]) => ([:x 2] [:y 4] [:z 6])

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x)) => (2 2 2)

(for [[k v] {:x 1 :y 2 :z 3}]


[k (* 2 v)]) => ([:x 2] [:y 4] [:z 6])
(into {}
(for [[k v] {:x 1 :y 2 :z 3}]
[k (* 2 v)]))

Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 6)

(for [x {:x 1 :y 2 :z 3}]


(count x)) => (2 2 2)

(for [[k v] {:x 1 :y 2 :z 3}]


[k (* 2 v)]) => ([:x 2] [:y 4] [:z 6])
(into {}
(for [[k v] {:x 1 :y 2 :z 3}]
[k (* 2 v)])) => {:x 2 :y 4 :z 6}

Friday, June 7, 13 35
Friday, June 7, 13 36
exercise 7
Write a function multiply-ingredients
that takes a quantity and an ingredient list and
returns a new ingredient list with all the
amounts multiplied by the quantity. Use into
and for.

Friday, June 7, 13 36
exercise 7
Write a function multiply-ingredients
that takes a quantity and an ingredient list and
returns a new ingredient list with all the
amounts multiplied by the quantity. Use into
and for.
example

(multiply-ingredients 4 {:egg 2})


=> {:egg 8}

Friday, June 7, 13 36
Friday, June 7, 13 37
exercise 8
Write a function order->ingredients which
takes an order and returns an ingredient list for
everything needed in that order. You should
use add-ingredients and multiply-
ingredients.

example
(order->ingredients
{:orderid 123
:address “43 Cyber Dr”
:items {:cake 10 :cookies 1}})
=> {:egg 21 :flour 21 :sugar 11 :milk 10 :butter 1}

Friday, June 7, 13 37
Friday, June 7, 13 38
map

Friday, June 7, 13 38
map

(map solve-problem problem-list)

Friday, June 7, 13 38
map
function

(map solve-problem problem-list)

Friday, June 7, 13 38
map
function sequence

(map solve-problem problem-list)

Friday, June 7, 13 38
map
function sequence

(map solve-problem problem-list)

(map order->ingredients orders)

Friday, June 7, 13 38
Friday, June 7, 13 39
reduce

Friday, June 7, 13 39
reduce

1 4 5 90 23 2 32

Friday, June 7, 13 39
reduce

1 + 4 + 5 + 90 + 23 + 2 + 32

Friday, June 7, 13 39
reduce

((((((1 + 4) + 5) + 90) + 23) + 2) + 32)

Friday, June 7, 13 39
reduce

(+ (+ (+ (+ (+ (+ 1 4) 5) 90) 23) 2) 32)

Friday, June 7, 13 39
reduce

(+ (+ (+ (+ (+ (+ 1 4) 5) 90) 23) 2) 32)

(reduce + [1 4 5 3 90 23 2 32])

Friday, June 7, 13 39
Friday, June 7, 13 40
exercise 9

Write a function orders->ingredients that


builds a total ingredients list from a list of
orders. You should use map and reduce.

Friday, June 7, 13 40
Friday, June 7, 13 41
let

Friday, June 7, 13 41
let

(let [x 1
y 2
z (+ x y)]
(println z))

Friday, June 7, 13 41
let

(let [x 1 name
y 2
z (+ x y)]
(println z))

Friday, June 7, 13 41
let
name
(let [x 1 value
y 2
z (+ x y)]
(println z))

Friday, June 7, 13 41
let
name
(let [x 1
name value
y 2
z (+ x y)]
(println z))

Friday, June 7, 13 41
let
name
(let [x 1
name value
y 2 value
z (+ x y)]
(println z))

Friday, June 7, 13 41
let
name
(let [x 1
name value
y 2 value
name
z (+ x y)]
(println z))

Friday, June 7, 13 41
let
name
(let [x 1
name value
y 2 value
name
z (+ x y)] value
(println z))

Friday, June 7, 13 41
let
name
(let [x 1
name value
y 2 value
name
z (+ x y)] value
(println z)) body

Friday, June 7, 13 41
Friday, June 7, 13 42
for

Friday, June 7, 13 42
for

(def list-of-lists [[1 2 3] [4 5 6] [7 8] [9 10] []])

Friday, June 7, 13 42
for

(def list-of-lists [[1 2 3] [4 5 6] [7 8] [9 10] []])

(for [l list-of-lists
x l]
x)

Friday, June 7, 13 42
for

(def list-of-lists [[1 2 3] [4 5 6] [7 8] [9 10] []])

(for [l list-of-lists
x l]
x) => (1 2 3 4 5 6 7 8 9 10)

Friday, June 7, 13 42
for

(def list-of-lists [[1 2 3] [4 5 6] [7 8] [9 10] []])

(for [l list-of-lists
x l]
x) => (1 2 3 4 5 6 7 8 9 10)

(for [x [:a :b :c]


y [1 2 3]]
[x y])

Friday, June 7, 13 42
Friday, June 7, 13 43
range

Friday, June 7, 13 43
range

(range 3) => (0 1 2)

Friday, June 7, 13 43
range

(range 3) => (0 1 2)

(range 10) => (0 1 2 3 4 5 6 7 8 9)

Friday, June 7, 13 43
range

(range 3) => (0 1 2)

(range 10) => (0 1 2 3 4 5 6 7 8 9)

(for [[item times] {“X” 20 “O” 12}


i (range times)]
item)

Friday, June 7, 13 43
Friday, June 7, 13 44
exercise 10

Rewrite day-at-the-bakery to make


X5 take a single trip to the fridge and
pantry, and to deliver one receipt per
order (instead of per item).

Friday, June 7, 13 44
Friday, June 7, 13 45
create a project

Friday, June 7, 13 45
create a project

refactor

Friday, June 7, 13 45
create a project

refactor

functional programming

Friday, June 7, 13 45
create a project

refactor

functional programming

Clojure collections

Friday, June 7, 13 45
create a project

refactor

functional programming

Clojure collections

Clojure abstractions

Friday, June 7, 13 45
Friday, June 7, 13 46
Friday, June 7, 13 46

You might also like