avl-rb-clojure-intro

Table of Contents

1 Intro to Clojure    slide

Toby Crawley
Asheville.rb
March 27, 2012

1.1 What is Clojure?    slide

  • A Lisp
    • Homoiconic - Code as data
    • Functional
      • Not Object Oriented
      • built around immutable data structures
  • Runs on the JVM
    • Embraces Java
  • ClojureCLR, ClojureScript, clojure-py are alt. impls.

1.1.1 5 years old    notes

  • 1958
  • s-expressions (symbolic)

1.2 Parts of Clojure    slide

  • very little syntax

1.2.1 Truthiness    slide

  • nil and false are false
  • true and everything else is true, including empty lists
  • same as ruby

1.2.2 Primitives - Strings    slide

  • Just java strings
"I'm a string"
1.2.2.1 No string interp    notes

1.2.3 Primitives - Characters    slide

  • Just java characters
\a \b \x  
1.2.3.1 but w/o single quotes    notes

1.2.4 Primitives - Numbers    slide

42   ;; long
42N  ;; clojure.lang.BigInt
4.2  ;; double
4.2M ;; java.math.BigDecimal
22/7 ;; ratio

1.2.5 Primitives - Symbols    slide

  • names of things
str
println
clojure.string/split

1.2.6 Primitives - Keywords    slide

  • identifiers, offer fast equality
  • often used as map keys
  • are functions of maps
:foo
:what-is-this-thing-in-my-hair?
:some.namespace/bar

(:key {:key "value"}) ;; => "value"

1.2.7 Collections - Lists    slide

  • implemented as a linked list
  • have special eval semantics
(1 2 3 4)

(str "a" "b" "c")
1.2.7.1 head is fast    notes

1.2.8 Collections - Vectors    slide

  • an array
  • generally used unless list semantics or performance needed
  • functions of index
[1 2 3 4]

([1 2 3 4] 1) ;; => 2
1.2.8.1 tail is fast    notes

1.2.9 Collections - Maps    slide

  • analogous to a Ruby Hash
  • functions of keys
{:a :b, :c :d}

({:key "value"} :key) ;; => "value"

1.2.10 Collections - Sets    slide

  • functions of their members
#{1 2 3 4}

(#{"yes" "no"} "yes")   ;; => "yes"
(#{"yes" "no"} "maybe") ;; => nil

1.3 Immutability    slide

(def x {:a :b})
(assoc x :c :d) ;; => {:a :b, :c :d}
x               ;; => {:a :b}

(def y [1 2 3])
(conj y 4)      ;; => [1 2 3 4]
y               ;; => [1 2 3]

1.3.1 def creates a 'var'    notes

  • internal structure is shared
  • makes concurrency easier

1.4 Lazy Sequences    slide

  • values in the sequence aren't realized until needed
  • allows working with large/infinite datasets
(take 10 (range)) ;; => (0 1 2 3 4 5 6 7 8 9)

1.4.1 range is infinite    notes

  • take itself returns a lazy sequence

1.5 Function Definition    slide

  • named
    (defn ham-is-good [a b]
      (println (if (or (= a "ham")
                       (= b "ham"))
                 "Ham goes good with anything!"
                 (str a " and " b "? Grody!"))))
    

1.5.1 this is just data    notes

1.6 Function Definition    slide

  • anonymous - lambdas
    (fn [v]
      (println "I got" v))
    
    #(println "I got" %)
    

1.6.1 compare to ruby blocks    notes

1.7 Reference Types    slide

  • allow for controlled mutability
  • can only be manipulated via special functions
  • mutation occurs within a transaction
  • must be explicitly dereferenced
(def an-atom (atom 0))

(swap! an-atom inc)

@an-atom ;; => 1

1.7.1 STM & MVCC    notes

  • atoms - single changing value
  • agents - asynchronous update of a single value
  • refs - mutliple coordinated changing values; explicit (dosync)

1.8 Let & Desctructuring    slide

  • bind values in a closure
    (let [full-name (some-name-producing-function)
          [first-name last-name] 
            (split full-name #" ")]
      (println full-name "splits into" 
               first-name last-name))
    

1.8.1 values only bound inside let    notes

  • introduces destructuring
  • aka structural binding

1.9 Let & Destructuring    slide

(let [point [123 456]
      [x-coord y-coord] point]
  (defn draw-something
    [height width 
     {:keys [color opacity] :as options}]
    (draw-square x-coord y-coord height 
                 width color opacity)
    (something-else-that-wants-options options)))

1.10 REPL    slide

  • the Read-Eval-Print Loop

1.10.1 think irb    notes

1.11 Demo!    slide

1.12 Pros/Cons    slide

  • pros:
    • write code with less errors
    • bend your brain
    • access to all Java libraries
    • safe(r) concurrency
  • cons:
    • have to learn Java as well
    • young ecosystem

1.13 Who uses Clojure?    slide

  • Twitter
  • Heroku
  • Datomic
  • World Singles
  • Sonian
  • Runa
  • Me! (therefore Red Hat)

1.14 Resources    slide

Date: 2012-03-27 19:32:18 EDT

Author: Tobias Crawley

Org version 7.7 with Emacs version 24

Validate XHTML 1.0