Not Everything Is An Object

Post on 19-May-2015

2.174 views 0 download

Tags:

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

Not Everything is an Object

By @garyshort

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.

The Road to Here

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

Where we are Now

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

What OOP/D is Good For

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

What is it Not Good For?

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

The Solution

What Does it Mean to be Functional?

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

Clojure

• A Lisp• Dynamic• Functional– Impure

• “Lockless” Concurrency• Macros

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

Introduction to Clojure

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

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.

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.

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.

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...

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.

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))

Tail Recursion

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

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

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

Questions?

• gary@garyshort.org• www.garyshort.org• @garyshort