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

Munich Clojure Workshop - 06 - Destructuring

Munich Clojure Workshop - 06 - Destructuring

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. (def my-collection [1 2 [3 4] 5]) (first my-collection) ;=>

    1 (nth my-collection 2) ;=> [3 4] Friday, October 11, 13
  2. It takes a function(+) and some arguments(1) and returns a

    closure with the arguments pre-set. But if we want multiple elements, this gets ugly fast: (let [a (nth my-collection 0) b (nth my-collection 1) c (nth my-collection 2) d (nth my-collection 3)] (println a c)) Friday, October 11, 13
  3. How many of you know Python? a, b, c, d

    = my_collection Friday, October 11, 13
  4. Hey, we can do this too! (let [[a b c

    d] my-collection] (println a c)) Friday, October 11, 13
  5. But wait, it's not only vectors! (def my-map {:lang "Clojure"

    :platform "JVM" :version "1.5.1"}) (let [{language :lang} my-map] (println language)) Friday, October 11, 13
  6. What else? • You can use this language in binding

    forms • So, not only let but also fn/defn • It works recursively • More hands-on stuff in the exercises Friday, October 11, 13