doesn’t immutability
require a lot of copying?
Friday, October 11, 13
Slide 10
Slide 10 text
yes, but there’s a trick
Friday, October 11, 13
Slide 11
Slide 11 text
Persistent data structures
Friday, October 11, 13
Slide 12
Slide 12 text
Copy only what is needed
through structural
sharing
Friday, October 11, 13
Slide 13
Slide 13 text
“just” “a” “list”
Friday, October 11, 13
Slide 14
Slide 14 text
“just” “a” “list”
“i’m”
Friday, October 11, 13
Slide 15
Slide 15 text
hmm, but that’s still less
efficient than mutation,
right?
Friday, October 11, 13
Slide 16
Slide 16 text
Transients
• provide mutability where performance is critical
• enforce thread isolation
• convert from/to persistent ds in O(1)
Friday, October 11, 13
Slide 17
Slide 17 text
associative data
structurues
[], {}
Friday, October 11, 13
Slide 18
Slide 18 text
Basic operations
Friday, October 11, 13
Slide 19
Slide 19 text
get
retrieve value by key
(get {:hi :there} :hi)
;; => :there
(get [1 2 3] 1)
;; => [2]
Friday, October 11, 13
Slide 20
Slide 20 text
get-in
retrieve nested value by key-seq
(get-in {:lang {:name clojure"}}
[:lang :name])
;; => "clojure"
(get-in [[1 2] [3 4]] [0 1])
;; => 2
Friday, October 11, 13
Slide 21
Slide 21 text
(assoc {} :hi :there)
;;=> {:hi :there}
assoc
add new key value pair
(assoc [] 0 1)
;;=> [1]
Friday, October 11, 13
Slide 22
Slide 22 text
dissoc
remove key value pair (map only)
(dissoc {:hi there} :hi)
;;=> {}
Friday, October 11, 13
Slide 23
Slide 23 text
Utility functions
Friday, October 11, 13
Slide 24
Slide 24 text
keys/vals
get the keys/values
(keys {:a :b :c :d})
;;=> (:a :c)
(vals {:a :b :c :d})
;;=> (:b :d)
Friday, October 11, 13
Slide 25
Slide 25 text
merge
merge maps, overwriting values right to left
(merge {:a 1 :b 2 :c 3}
{:b 3 :c 4}
{:c 5})
;; => {:a 1, :c 5, :b 3}
Friday, October 11, 13
Slide 26
Slide 26 text
merge-with
merge maps, overwriting is managed by fn
(merge-with (fn [a b] a)
{:a 1 :b 2 :c 3}
{:b 3 :c 4}
{:c 5})
;; => {:a 1, :c 3, :b 2}
Friday, October 11, 13
Slide 27
Slide 27 text
[], ‘(), #{}, {}
sequential
data structures
Friday, October 11, 13
Slide 28
Slide 28 text
Basic operations
Friday, October 11, 13
Slide 29
Slide 29 text
cons
builds a cons cell (ie, add before first element)
(cons 1 [2 3])
;;=> (1 2 3)
(cons 1 '(2 3))
;;=> (1 2 3)
Friday, October 11, 13
Slide 30
Slide 30 text
conj
add element to a sequence
place of addition depends on sequence type
(conj [2 3] 1)
;;=> [2 3 1]
(conj ‘(2 3) 1)
;;=> (1 2 3)
Friday, October 11, 13
disj
remove element from a set
sets only!
(disj #{:a :b :c} :a)
;;=> {:b :c}
Friday, October 11, 13
Slide 33
Slide 33 text
seq
convert any collection to a seq
returns nil for nil, empty collections
Friday, October 11, 13
Slide 34
Slide 34 text
Utility functions
Friday, October 11, 13
Slide 35
Slide 35 text
partition
divide a sequence into parts
parts may overlap (optional)
(partition 2 [1 2 3 4 5])
;;=> ((1 2) (3 4))
Friday, October 11, 13
Slide 36
Slide 36 text
flatten
convert any nested seq into a flat list
(flatten [1 [2] [3 4]])
;;=> (1 2 3 4)
Friday, October 11, 13
Slide 37
Slide 37 text
frequencies
take a seq and return the number of
occurrences for each element
(frequencies [:a :v :d :v :a])
;;=> {:a 2, :v 2, :d 1}
Friday, October 11, 13
Slide 38
Slide 38 text
Sequence predicates
Friday, October 11, 13
Slide 39
Slide 39 text
every?
true if all members satisfy the predicate
(every? even? [2 4 6 8])
;;=> true
(every? even? [2 4 7 8])
;;=> false
Friday, October 11, 13
Slide 40
Slide 40 text
some
true if at least one member satisfies the predicate
not-any? as complement.
(some even? [1 4 5 7])
;;=> true
(some even? [1 3 5 7])
;;=> nil
Friday, October 11, 13
Slide 41
Slide 41 text
comprehensions
Friday, October 11, 13
Slide 42
Slide 42 text
for
• looks like iteration, but is list comprehension
• not intended for side effects
• carthesian product of multiple lists
• powerful possibilities for filtering/constraining
Friday, October 11, 13
doseq
• intended for side effects
• similar filtering/constraining possibilities as for
• always returns nil!
Friday, October 11, 13
Slide 45
Slide 45 text
example
(doseq [x (range 3) y (range 3) :while (< y x)]
(println [x y]))
;; [1 0]
;; [2 0]
;; [2 1]
;;=> nil
Friday, October 11, 13
Slide 46
Slide 46 text
Combinators
Friday, October 11, 13
Slide 47
Slide 47 text
consider the following
Java code
Friday, October 11, 13
Slide 48
Slide 48 text
public static List incByOne(List input){
List mapped = new ArrayList();
" for(int i:input){
" " mapped.add(i + 1);
" }
" return mapped;
}
Friday, October 11, 13
Slide 49
Slide 49 text
public static List incByOne(List input){
List mapped = new ArrayList();
" for(int i:input){
" " mapped.add(i + 1);
" }
" return mapped;
}
Boilerplate Code
Friday, October 11, 13
Slide 50
Slide 50 text
public static List incByOne(List input){
List mapped = new ArrayList();
" for(int i:input){
" " mapped.add(i + 1);
" }
" return mapped;
}
Hard-coded Functionality
Friday, October 11, 13
Slide 51
Slide 51 text
some guava might help...
Friday, October 11, 13
Slide 52
Slide 52 text
Collections2.transform(input, new Function(){
public Integer apply(final Integer i){
return i + 1;
}
});
Friday, October 11, 13
Slide 53
Slide 53 text
much better, but...
Friday, October 11, 13
Slide 54
Slide 54 text
Collections2.transform(input, new Function(){
public Integer apply(final Integer i){
return i + 1;
}
});
Still too much Boilerplate
Friday, October 11, 13
Slide 55
Slide 55 text
enter: clojure
Friday, October 11, 13
Slide 56
Slide 56 text
(map inc input)
Friday, October 11, 13
Slide 57
Slide 57 text
map
f [a,b,c,...]
[f(a),f(b),f(c),...]
Friday, October 11, 13