small lambda talk

20
@MSkarsaune #DV14 #SmallLambdaTalk Small Lambda Talk [email protected] Kantega Norwegian software craftsmanship since 2003

Upload: martin-skarsaune

Post on 02-Jul-2015

311 views

Category:

Software


1 download

DESCRIPTION

Quickie talk to be held at Devoxx 2014

TRANSCRIPT

Page 1: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Small Lambda [email protected]

Kantega – Norwegian software craftsmanship since 2003

高馬丁

Page 2: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Small lambda talk

• Java 8 introduced lambda expressions

• focus on functional programming

• Also roots in object oriented programming: Smalltalk

Page 3: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Smalltalk – everything is simple

• Illustration, reserved words:

• self (=this), super, nil (=null), true, false

Java reserved words ….abstract default goto package synchronized

assert do if private this

boolean double implements protected throw

break else import public throws

byte enum instanceof return transient

case extends int short true

catch false interface static try

char final long strictfp void

class finally native super volatile

const float new switch while

continue for null

Page 4: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

“Smalltalk's design--and existence--is due to the insight that

everything we can describe can be represented by the

recursive composition of a single kind of behavioral building

block that hides its combination of state and process inside

itself and can be dealt with only through the exchange of

messages.” (Kay 1996)

• Objects+ Messages=> Can do everything!

Smalltalk – the main concept

Page 5: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Ingredient 1: Everything is an

object!• No primitive types

• nil is an object

• No void methods

Page 6: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Ingredient 2: Messages do

everything!• Three types of messages

• Unary

• Binary

• Keyword

Page 7: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Messages - unary

• No arguments

• Invocation example:

• Implementation in class Number:

2 negated

negated

^0 - self

public Number negated() {

return 0 - this;

}

2.negated()

Equivalent “Java” Syntax

Page 8: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Messages - binary

• Exactly one argument

• Message selector (method name) made from special

characters: = , + , - , > etc.

• Invocation

• Implementation in class Object:

a = b

= anObject

^self == anObject

public boolean equals(Object obj){

return this == obj;

}

a.equals(b)

Equivalent “Java” Syntax

Page 9: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Messages - keyword

• One or more arguments

• Each argument is separated by a keyword

• Invocation:

• Implementation:

a at: 1 put: ‘one’

at: key put: value

self cache

at: key put: value

public Object put(Integer key, String value){

this.cache()

.put(key,value);

return this;

}

a.put(1, “one”);

Equivalent “Java” Syntax

Page 10: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

OO Conditional Processing

Boolean

True

true

False

false

true false

Page 11: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

OO Conditional Processing

Example• Task:

• Use recursion to return root of

tree

• Each node may access its

parent by sending the parent

message

Page 12: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

OO branching- blocks

root

^self parent = nil

ifTrue: [self]

ifFalse: [self parent root]

ifTrue: trueBlock ifFalse:

falseBlock

^trueBlock value

ifTrue: trueBlock

ifFalse: falseBlock

^falseBlock value

Implementation in True: Implementation in False:

Page 13: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

OO branching- characteristics

• Voilla! We have made if else with only objects and

messages

• Boolean instance is an object

• Code blocks are objects

• Implemented as one message, one expression, one return

value.

root

^self parent = nil

ifTrue: [self]

ifFalse: [self parent root]

Page 14: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Object oriented null - Nil check:

#isNilMessage isNil can be used to ask an object if it is nil:

root

^self parent isNil

ifTrue: [self]

ifFalse: [self parent root]

Object

Node UndefinedObject

nil

isNil

^false

isNil

^true

!java.util.Optional<T>.isPresent()

Page 15: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

OO null – symmetric equals

root

^self parent = nil

ifTrue: [self]

ifFalse: [self parent root]

Object

Node UndefinedObject

nil

= anObject

^self == anObject

(<==> nil = self parent)

java.util.Optional

provides symmetric equals

Page 16: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Object oriented null: #ifNil:ifNotNil:

Message #ifNil:ifNotNil: can be used to branch on nil:

root

^self parent

ifNil: [self]

ifNotNil: [:par | par root]

Object

Node UndefinedObject

nil

ifNil: nilBlock

ifNotNil: block

^block value: self

ifNil: nilBlock

ifNotNil: block

^nilBlock value

Page 17: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

More OO conditional logic

and: block

^block value

True implementation:

boolExpr1 and: [boolExpr2].

boolExpr1 or: [boolExpr2].

boolExpr not

and: block

^self

False implementation:

or: block

^self

or: block

^block value

not

^false

not

^true

boolExpr1 && boolExpr2;

boolExpr1 || boolExpr2;

!boolExpr;

Page 18: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Small confession

• Presentation a bit theoretical

• The compiler will often optimize with dedicated

bytecodes

• But the underlying functionality is still there

Page 19: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Learning points

• Anything may be expressed using objects, messages

and lambdas:

• Branches, loops, exception handling

• Common language features may be implemented with

lambdas

• Self documenting

• Possible to debug

Page 20: Small lambda talk

@MSkarsaune#DV14 #SmallLambdaTalk

Thank you for your time!

response

^self satisfied

ifTrue: [self clap]

ifFalse: [self boo]

More in 10 min …