small lambda talk

Post on 02-Jul-2015

311 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Quickie talk to be held at Devoxx 2014

TRANSCRIPT

@MSkarsaune#DV14 #SmallLambdaTalk

Small Lambda TalkMartin.Skarsaune@kantega.no

Kantega – Norwegian software craftsmanship since 2003

高馬丁

@MSkarsaune#DV14 #SmallLambdaTalk

Small lambda talk

• Java 8 introduced lambda expressions

• focus on functional programming

• Also roots in object oriented programming: Smalltalk

@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

@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

@MSkarsaune#DV14 #SmallLambdaTalk

Ingredient 1: Everything is an

object!• No primitive types

• nil is an object

• No void methods

@MSkarsaune#DV14 #SmallLambdaTalk

Ingredient 2: Messages do

everything!• Three types of messages

• Unary

• Binary

• Keyword

@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

@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

@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

@MSkarsaune#DV14 #SmallLambdaTalk

OO Conditional Processing

Boolean

True

true

False

false

true false

@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

@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:

@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]

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

@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

@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

@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;

@MSkarsaune#DV14 #SmallLambdaTalk

Small confession

• Presentation a bit theoretical

• The compiler will often optimize with dedicated

bytecodes

• But the underlying functionality is still there

@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

@MSkarsaune#DV14 #SmallLambdaTalk

Thank you for your time!

response

^self satisfied

ifTrue: [self clap]

ifFalse: [self boo]

More in 10 min …

top related