@bodil



@bodil

1 3


ten-minute-clojure

Slides for teaching Clojure basics in a hurry with Catnip

On Github bodil / ten-minute-clojure

Bodil Stokke

@bodil

Clojure?
interface Function<F,T> {
    T apply(F input);
}
class SquareFunction implements Function<Long,Long> {
    Long apply(Long number) {
        return number * number;
    }
}
(defn square [number]
  (* number number))

Java Syntax

println("Hello sailor!");

Lisp Syntax

(println "Hello sailor!")

Lists

(println "Hello sailor!")

("Rainbow Dash" "Twilight Sparkle" "Pinkie Pie")

(0 1 1 2 3 5 8 13 21 34 55)

Homoiconic

Homoiconic

Your code is a data structure Makes metaprogramming easy Macros

Data structures

;; List (linked list)
("Rainbow Dash" "Twilight Sparkle" "Pinkie Pie")
;; Vector (array list)
["Fluttershy" "Rarity" "Applejack"]
;; Map (hash map)
{"name" "Rainbow Dash", "type" "Pegasus pony"}

All the syntax you need

(println "Hello sailor!")

(println "2 + 2 = " (+ 2 2))

(defn add [number1 number2]
  (+ number1 number2))

Immutable Data Structures

No unexpected side effects guaranteed The easy way to thread safety No, they're not slow

Look ma, no mutation

(def primes [2 3 5 7 11 13])
(assoc primes 3 42)

Functions as values

(defn square [number]
  (* number number))

(def fibonacci [0 1 1 2 3 5 8 13 21 34 55])

Higher order functions

(defn plus-two [number]
  (+ 2 number))

(defn plus-something [something]
  (fn [number]
    (+ something number)))

Combinators

(defn function-plus-something [function]
  (fn [number]
    (+ something (function number))))
MULTI-CORE LIKE A BOSS

State gonna state

(def counter-state (atom 0))

(defn count-up []
  (swap! counter-state inc)
  @counter-state)

Threads so simple you could cry

(defn sum-to [n]
  (reduce + (range n)))

; (sum-to 250000000)
YOU NOW KNOW CLOJURE
So... ClojureScript?

ClojureScript

A subset of Clojure Compiles to Javascript Can use Javascript APIs NO MORE JAVASCRIPT!
Node is the new Ruby
kthxbye.

bodil.org/ten-minute-clojure

github.com/bodil/clojurescript-all-the-way-down