Intro To Clojure Part 2
Intro To Clojure Part 2
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
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
Friday, June 7, 13 7
rest argument
any number of aguments
Friday, June 7, 13 7
Friday, June 7, 13 8
apply
Friday, June 7, 13 8
apply
Friday, June 7, 13 8
apply
function
Friday, June 7, 13 8
apply
function arguments (0 or more)
Friday, June 7, 13 8
apply
function arguments (0 or more)
(println 1 2 3)
Friday, June 7, 13 8
Friday, June 7, 13 9
warm-up
Friday, June 7, 13 9
Friday, June 7, 13 10
warm-up
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
Friday, June 7, 13 11
a day at the bakery
get orders for the day
bake orders
Friday, June 7, 13 11
a day at the bakery
get orders for the day
bake orders
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
Friday, June 7, 13 12
locations
:prep-area for baking
Friday, June 7, 13 12
locations
:prep-area for baking
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}
Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}
Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}
Friday, June 7, 13 14
sets #{ }
examples
#{1 2 3}
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
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})
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})
Friday, June 7, 13 18
maps { }
examples
(def ingredients {:flour 3
:egg 6
:sugar 24})
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)
Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)
Friday, June 7, 13 19
IFn
I -- Interface (a common Java pattern)
Friday, June 7, 13 19
Friday, June 7, 13 20
seq
Friday, June 7, 13 20
seq
short for sequenence
Friday, June 7, 13 20
seq
short for sequenence
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
Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable
Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable seq
Friday, June 7, 13 21
doseq
iteration over items in a sequence
variable seq
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
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
Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders
Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders
Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders
Friday, June 7, 13 22
a day at the bakery
get morning orders get-morning-orders
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:
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:
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.
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.
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.
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 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
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
functional programming
Side effects Pure functions
Friday, June 7, 13 28
combining two lists of ingredients
List 1 List 2
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
2 butters 1 egg
Friday, June 7, 13 29
combining two lists of ingredients
Friday, June 7, 13 29
combining two lists of ingredients
Friday, June 7, 13 29
combining two lists of ingredients
Friday, June 7, 13 29
combining two lists of ingredients
Friday, June 7, 13 29
combining two lists of ingredients
Friday, June 7, 13 29
combining two lists of ingredients
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
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
Friday, June 7, 13 32
Friday, June 7, 13 33
into
Friday, June 7, 13 33
into
Friday, June 7, 13 33
into
collection to add to
Friday, June 7, 13 33
into
collection to add to seq of items
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?
Friday, June 7, 13 34
REPL exploration
What happens if a key appears twice when
using into?
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)
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)
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)
Friday, June 7, 13 35
for
(for [x [1 2 3]]
(* x 2)) => (2 4 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
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
Friday, June 7, 13 38
map
function
Friday, June 7, 13 38
map
function sequence
Friday, June 7, 13 38
map
function sequence
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
Friday, June 7, 13 39
reduce
Friday, June 7, 13 39
reduce
(reduce + [1 4 5 3 90 23 2 32])
Friday, June 7, 13 39
Friday, June 7, 13 40
exercise 9
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
Friday, June 7, 13 42
for
(for [l list-of-lists
x l]
x)
Friday, June 7, 13 42
for
(for [l list-of-lists
x l]
x) => (1 2 3 4 5 6 7 8 9 10)
Friday, June 7, 13 42
for
(for [l list-of-lists
x l]
x) => (1 2 3 4 5 6 7 8 9 10)
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)
Friday, June 7, 13 43
range
(range 3) => (0 1 2)
Friday, June 7, 13 43
Friday, June 7, 13 44
exercise 10
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