the curious clo jureist - jfokus · why all the ()’s? lisp is a homoiconic language. lisp...

75
NEAL FORD ThoughtWorks ® [email protected] 2002 Summit Boulevard, Atlanta, GA 30319 nealford.com thoughtworks.com memeagora.blogspot.com @neal4d director / software architect meme wrangler The Curious Clojureist

Upload: others

Post on 12-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

NEAL FORD

ThoughtWorks®

[email protected] 2002 Summit Boulevard, Atlanta, GA 30319 nealford.com thoughtworks.com memeagora.blogspot.com @neal4d

director / software architect meme wrangler

The Curious Clojureist

Page 2: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what is this talk about?

clojure’s 4 elevators: 1. java interop 2. lisp 3. functional 4. state & concurrency

the ecosystem

the coolness

Page 3: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what is clojure?

Clojure is a dynamic, strongly typed, functional, high-

performance implementation of a lisp on the JVM.

dynamic stronglytyped functional high-

performancelisp

Page 4: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isn’t lisp the one with all the ()’s?

yes

Page 5: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

why all the ()’s?

Lisp is a homoiconic language.

Lisp programs consist of lisp data structures.

all kinds of useful!

Page 6: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

ecosystem

Page 7: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

clojure.org/

Page 8: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

code.google.com/p/counterclockwise/

Page 9: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

github.com/technomancy/emacs-starter-kit

Page 10: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

github.com/technomancy/leiningen

Page 11: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

compojure(web framework)

github.com/weavejester/compojure

clj-record(pseudo-port of ActiveRecord)

github.com/duelinmarkers/clj-record

github.com/LauJensen/clojureql

clojureql(relational algebra

-> SQL)

clojurescript(clojure on JavaScript)

github.com/clojure/clojurescript

Page 12: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

what does clojure code look like?

Page 13: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

data types?type example java equivalentstring "foo" String

character \f Character

regex #"fo*" Pattern

a. p. integer 42 Int/Long/BigInteger

double 3.14159 Double

a.p. double 3.14159M BigDecimal

boolean true Boolean

nil nil null

symbol foo, + N/A

keyword :foo, ::foo N/A

Page 14: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

data literals?

type properties example

list singly-linked,insert at front (1 2 3)

vector indexed,insert at rear [1 2 3]

map key/value {:a 100 :b 90}

set key #{:a :b}

Page 15: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

function calls?

(println "Hello World")

fn call argsemantics:

structure: stringsymbol

list

Page 16: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

function definition?

(defn greet "Returns a friendly greeting" [your-name] (str "Hello, " your-name))

define a fn fn name

docstring

arguments

fn body

Page 17: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

homoiconicity?

symbol symbolstring

vector

listit’s all data

(defn greet "Returns a friendly greeting" [your-name] (str "Hello, " your-name))

Page 18: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

function meta-data?

(defn ^String greet "Returns a friendly greeting" [your-name] (str "Hello, " your-name))

prefix with ^ class name orarbitrary map

Page 19: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

what is the java interop story with

clojure?

Page 20: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

interop?

syntax extensions to reach all of Java

fast

compiles to bytecode

call Clojure from Java

Page 21: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

construction?

new Widget("foo")

(Widget. "red")

Page 22: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

static members?

Math.PI

Math/PI

Page 23: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

instance members?

rnd.nextInt()

(.nextInt rnd)

Page 24: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

chained access?

person.getAddress().getZipCode()

(.. person getAddress getZipCode)

Page 25: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

() count?

8

6

Page 26: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how would you implement an interface?

(reify Runnable (run [] (println "Hello")))

interface

method bodies

add moreinterfaces

here

Page 27: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what would a typical method look like in

clojure?

isBlank()

Page 28: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

Page 29: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

— types

Page 30: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

— class

Page 31: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

+ higher-order function

Page 32: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

— corner cases

Page 33: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

isBlank()

lispify!

Page 34: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

when does verbosity == obscurity?

Page 35: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s so special about lisp?

feature industry norm

coolkids clojure

conditionals ✔ ✔ ✔

variables ✔ ✔ ✔

garbage collection ✔ ✔ ✔

recursion ✔ ✔ ✔

function type ✔ ✔

symbol type ✔ ✔

whole language available ✔ ✔

everything’s an expression ✔ ✔

homoiconicity ✔http://www.paulgraham.com/diff.html

Page 36: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what are special forms?

the syntactic scaffolding of your language

importsscopesprotectionmeta-datacontrol flowanything using a keyword

Page 37: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how are special forms outside lisp different?

μ limited to specific use

μ look different

μ may have special semantics unavailable to you

μ hamper reuse

Page 38: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s special about lisp’s special forms?

λ look just like everything else

λ may have special semantics available to you

λ can be augmented with macros

Page 39: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

all forms are created equal?

form syntax example

function list (println "hello")

operator list (+ 1 2)

method call list (.trim " hello ")

import list (require 'mylib)

metadata list (with-meta obj m)

control flow list (when valid? (proceed))

scope list (dosync (alter ...))

Page 40: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

why is this important?

special forms are easier to understand individually…

…but create combinatorialcomplexity in aggregate

to deal with complexity, you categorize

& you end up

Page 41: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s the alternative to patterns in lisp?

m a c r o s

Page 42: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how can macros reduce repetition?

(.add frame panel)(.pack frame)(.setVisible frame true)

(doto frame (.add panel) (.pack) (.setVisible true)

doto returns framenow we have an expression

say it only once

Page 43: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

Many of the features of Clojure are implemented with

macros. How can you tell?

you can’t!

Page 44: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

an example of a simple macro?

(defmacro when [test & body] (list 'if test (cons 'do body)))

(when x (println "x is true")))

(if x (do (println "x is true")))

macroexpansion

it’s all data

Page 45: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what are some types of macros?

type examples

control flow when when-not and or

vars defn defmacro defmulti

java interop .. doto deftype proxy

rearranging -> ->> -?>

scopes dosync time with-open

“special form” fn lazy-seq let

Page 46: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

how does “functional” make my

life better?

Page 47: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how does function change the structure of my code?

indexOfAny

Page 48: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

indexOfAny

StringUtils.indexOfAny(null, *) = -1StringUtils.indexOfAny("", *) = -1StringUtils.indexOfAny(*, null) = -1StringUtils.indexOfAny(*, []) = -1StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3StringUtils.indexOfAny("aba", ['z']) = -1

Page 49: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.indexOfAny

Page 50: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.indexOfAny

— simplify corner cases

Page 51: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.indexOfAny

— type decls

Page 52: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.indexOfAny

+ when clause

Page 53: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.indexOfAny

+ comprehension

Page 54: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

indexOfAny

lispify

Page 55: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

which version is simpler?imperative functional

functions 1 1

classes 1 0

internal exit points 2 0

variables 3 0

branches 4 0

boolean ops 1 0

function calls* 6 3

total 18 4

Page 56: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

which is more general?

; idxs of heads in stream of coin flips(index-filter #{:h} [:t :t :h :t :h :t :t :t :h :h]) -> (2 4 8 9)

; Fibonaccis pass 1000 at n=17(first (index-filter #(> % 1000) (fibo)))-> 17

Page 57: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A. imperative functional

searches strings searches any sequence

matches characters matches any predicate

returns first match returns lazy seq of all matches

which is more general?

Page 58: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

what’s clojure’s

unique take on state &

concurrency?

Page 59: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.“change” by function application

immutable

full fidelity old versions

maintain performance guarantees

what about persistent data structures?

Page 60: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how would that work with a linked list?

rabbitternnewt

“my” list“your” list

Page 61: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

…and more complex data structures?

“your” trie

“my”trie

bit-partitioned tries

log2 n: too slow

Page 62: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how many tries to make it fast enough?

... ... ... ... ... ...

... ...

... ......

... ......

32-way tries

log32 n: fast enough!

Page 63: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s wrong with variables?!?

variable

???

variable

???

?identity

state

time

Page 64: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s clojure’s view of identity

identity value

identity value

value

explicit semantic

state

Page 65: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

compare identity, state, & time?

term meaning

value immutable data in a persistent data structure

identity series of causally related values over time

state identity at a point in time

timerelative: before/simultaneous/after ordering of causal values

Page 66: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what is clojure’s epochal time model?

v1

F

v2

F

v3

F

v4

States (immutable values)

Observers/perception/

memoryIdentity (succession of

states)

Process events (pure functions)

Page 67: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.snapshot always available

writes never impede readers

no user locking, no deadlocks

what’s the “unified update model”?

(change-state ref fn [args*])

gets current state of ref

return becomes next state of ref

Page 68: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

software transactional

memory

Page 69: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how does STM work?

F

v1 v2 v3 v4

v1 v2 v3 v4

v1 v2 v3 v4

v1 v2 v3 v4

F

F

F

F

F

F

F F F

F

F

transactions

Page 70: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

what’s the syntax for STM?

(def messages (ref []))

identity

initial value

Page 71: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how do you read a value?

(deref messages)=> []

@messages=> []

Page 72: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how do you alter a message?

r

oldval

newval

(alter r update-fn & args)

(apply update-fn oldval args)

Page 73: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.

how do you update a message?

(defn add-message [msg] (dosync (alter messages conj msg)))

apply an...

scope a transaction

...update fn

Page 74: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

Q.

A.seamless java interop

highly expressive language

advanced concurrency

functional

what’s cool about clojure?

Page 75: The Curious Clo jureist - Jfokus · why all the ()’s? Lisp is a homoiconic language. Lisp programs consist of lisp data structures. all kinds of useful! ecosystem. ... clojurescript

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License.

http://creativecommons.org/licenses/by-sa/3.0/us/

?’splease fill out the session evaluations

NEAL FORD

ThoughtWorks®

[email protected] 2002 Summit Boulevard, Atlanta, GA 30319 nealford.com thoughtworks.com memeagora.blogspot.com @neal4d

director / software architect meme wrangler