Not Everything Is An Object

19
Not Everything is an Object By @garyshort

description

A presentations highlighting the weaknesses in OO programming and showing how functional languages, in this case Clojure, could be the answer.

Transcript of Not Everything Is An Object

Page 1: Not Everything Is An Object

Not Everything is an Object

By @garyshort

Page 2: Not Everything Is An Object

Agenda

• The road to here• Where we are now• What OOD/P is good for• What it’s not good for• The solution• Demo• Questions.

Page 3: Not Everything Is An Object

The Road to Here

http://www.flickr.com/photos/fyngyrz/

Page 4: Not Everything Is An Object

Where we are Now

http://www.flickr.com/photos/gtarded/

Page 5: Not Everything Is An Object

What OOP/D is Good For

http://www.flickr.com/photos/gserafini/

Page 6: Not Everything Is An Object

What is it Not Good For?

http://www.flickr.com/photos/aloshbennett/

Page 7: Not Everything Is An Object

The Solution

Page 8: Not Everything Is An Object

What Does it Mean to be Functional?

• First order functions• Function like constructs• Stateless• Immutable data.

Page 9: Not Everything Is An Object

Clojure

• A Lisp• Dynamic• Functional– Impure

• “Lockless” Concurrency• Macros

• JVM• Java Interop• Fast• Persistent collections• Easy to learn.

Page 10: Not Everything Is An Object

Introduction to Clojure

• Java.Lang.Object• Arbitrary sized numbers• Ratios: 18/20• Nil is null and is treated as false.

Page 11: Not Everything Is An Object

Persistent Collections

• Immutable– Cheap to copy

• Examples– (1 2 3)• List – sequential lookup time

– [1 2 3]• Vector – logarithmic lookup time

– {“key” 1, 3 7, “foo” “bar”}• Hashmap – unordered, key value pairs.

Page 12: Not Everything Is An Object

Equality Vs Identity

• Equality– Two objects are equal• Two cylinders maybe equal if their volumes are equal

– Identity• Two pointers to the same object

– Clojure favours equality :-O.

Page 13: Not Everything Is An Object

Clojure is Impure so has Mutability

• Var– Mutable pointer to immutable data• You can’t change the data• But you can change what data the var points to

• But vars hold global data, function defs etc .• This data won’t change• So why are vars mutable?• So we can patch running software.

Page 14: Not Everything Is An Object

The Reader

• Other programming languages– Compiler• Text -> lexing and parsing -> AST

• Clojure– The Reader• Text -> lexing and parsing -> Literals (Data)• As soon as it reads a complete literal it’s passed to...

Page 15: Not Everything Is An Object

The Evaluator

• Compile Phase– Traverses the data

• Symbol evaluation– Symbols evaluate into Vars

» Symbol Dog evaluates to a var in the current namespace» Symbol MyPets/Dog evaluates to var in namespace MyPets

• List evaluation– Lists evaluate into function calls

» (+ 1 2 3)

• Process macros

– Execute phase• Effects the special forms• Calls functions.

Page 16: Not Everything Is An Object

Special Forms

• Reserved symbol– Denotes special list evaluation

• Not a function call – it’s “something else”

• Def, if, do, let, quote, var, fn, loop, recur, throw, try, ., new, set!

• If a list starts with any of those it’s evaluated in a special way particular to that form

• (if condition a b?)• (if (been_drinking) (hungover) (happy))• (if (have_beers) (drink))

Page 17: Not Everything Is An Object

Tail Recursion

http://www.flickr.com/photos/43911015@N05

Page 18: Not Everything Is An Object

I’m Bored... Show me the Code!

http://www.flickr.com/photos/peter_hasselbom/

Page 19: Not Everything Is An Object

Questions?

[email protected]• www.garyshort.org• @garyshort