Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Munich Clojure Workshop - 02&03 - Sequences

Munich Clojure Workshop - 02&03 - Sequences

Copyright © 2012-2014 Clojure Workshop Team: Maximilian Karasz, Jan Stępień, Marek Kubica, Oleksandr Petrov

Commercial use of complete work or parts of work is not allowed to anyone except for Clojure Workshop Team at whole or in parts.

Licensed under Creative Commons Attribution-NonCommercial 3.0: http://creativecommons.org/licenses/by-nc/3.0/

When using complete work or parts of work, explicit attribution is required.

Clojure Workshops

October 11, 2013
Tweet

More Decks by Clojure Workshops

Other Decks in Technology

Transcript

  1. Transients • provide mutability where performance is critical • enforce

    thread isolation • convert from/to persistent ds in O(1) Friday, October 11, 13
  2. get retrieve value by key (get {:hi :there} :hi) ;;

    => :there (get [1 2 3] 1) ;; => [2] Friday, October 11, 13
  3. 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
  4. (assoc {} :hi :there) ;;=> {:hi :there} assoc add new

    key value pair (assoc [] 0 1) ;;=> [1] Friday, October 11, 13
  5. keys/vals get the keys/values (keys {:a :b :c :d}) ;;=>

    (:a :c) (vals {:a :b :c :d}) ;;=> (:b :d) Friday, October 11, 13
  6. 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
  7. 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
  8. 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
  9. 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
  10. (concat [1 2] ‘(3 4)) ;;=> (1 2 3 4)

    concat concatenate sequences Friday, October 11, 13
  11. disj remove element from a set sets only! (disj #{:a

    :b :c} :a) ;;=> {:b :c} Friday, October 11, 13
  12. seq convert any collection to a seq returns nil for

    nil, empty collections Friday, October 11, 13
  13. 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
  14. flatten convert any nested seq into a flat list (flatten

    [1 [2] [3 4]]) ;;=> (1 2 3 4) Friday, October 11, 13
  15. 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
  16. 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
  17. 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
  18. 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
  19. examples (for [x (range 3) y (range 3)] [x y])

    ;;=> ([0 0] [0 1] [0 2] [1 0] ;; [1 1] [1 2] [2 0] [2 1] [2 2]) (for [x (range 3) y (range 3) :while (< y x)] [x y]) ;; => ([1 0] [2 0] [2 1]) Friday, October 11, 13
  20. doseq • intended for side effects • similar filtering/constraining possibilities

    as for • always returns nil! Friday, October 11, 13
  21. 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
  22. public static List<Integer> incByOne(List<Integer> input){ List<Integer> mapped = new ArrayList<Integer>();

    " for(int i:input){ " " mapped.add(i + 1); " } " return mapped; } Friday, October 11, 13
  23. public static List<Integer> incByOne(List<Integer> input){ List<Integer> mapped = new ArrayList<Integer>();

    " for(int i:input){ " " mapped.add(i + 1); " } " return mapped; } Boilerplate Code Friday, October 11, 13
  24. public static List<Integer> incByOne(List<Integer> input){ List<Integer> mapped = new ArrayList<Integer>();

    " for(int i:input){ " " mapped.add(i + 1); " } " return mapped; } Hard-coded Functionality Friday, October 11, 13
  25. examples (map inc [1 2 3 4]) ;;=> (2 3

    4 5) (map :id [{:id 2 :name "Maxi"} {:id 4 :name "Alex"}]) ;;=> (2 4) Friday, October 11, 13
  26. examples (mapcat list [:a :b :c] [1 2 3]) ;;=>

    (:a 1 :b 2 :c 3) (mapcat (fn [x] (repeat x x)) [1 2 3]) ;;=> (1 2 2 3 3 3) Friday, October 11, 13
  27. examples (filter even? [1 2 3 4]) ;;=> (2 4)

    (remove nil? [1 nil 3 nil]) ;;=> (1 3) Friday, October 11, 13
  28. examples (reduce + [1 2 3 4]) ;;=> 10 (reduce

    conj ‘() [1 2 3 4]) ;;=> (4 3 2 1) Friday, October 11, 13
  29. examples (reductions + [1 2 3 4]) ;;=> (1 3

    6 10) (reductions conj ‘() [1 2 3 4]) ;;=> (() (1) (2 1) (3 2 1) (4 3 2 1)) Friday, October 11, 13
  30. imperative summing public int sum(Iterable<Integer> c){ int sum = 0;

    for(int i:c){ sum += i; } return sum; } Friday, October 11, 13
  31. recursive summing (defn recursive-sum [[head & tail]] (if (empty? tail)

    (or head 0) (+ head (recursive-sum tail)))) Friday, October 11, 13
  32. where’s the problem? (defn recursive-sum [[head & tail]] (if (empty?

    tail) (or head 0) (+ head (recursive-sum tail)))) Friday, October 11, 13
  33. (defn recursive-sum [[head & tail]] (if (empty? tail) (or head

    0) (+ head (recursive-sum tail)))) where’s the problem? Friday, October 11, 13
  34. tail-recursive summing i (defn recursive-sum-tc ([[head & tail] acc] (if

    (empty? tail) (+ acc head) (recursive-sum-tc tail (+ acc head)))) ([coll] (recursive-sum-tc coll 0))) Friday, October 11, 13
  35. tail-recursive summing ii (defn recursive-sum-tc [coll] (loop [[head & tail]

    coll acc 0 ] (if (nil? head) acc (recur tail (+ acc head))))) Friday, October 11, 13