small lambda talk

Download Small lambda talk

Post on 02-Jul-2015




0 download

Embed Size (px)


Quickie talk to be held at Devoxx 2014


  • 1. Small Lambda TalkMartin.Skarsaune@kantega.noKantega Norwegian software craftsmanship since 2003#DV14 #SmallLambdaTalk @MSkarsaune

2. Small lambda talk Java 8 introduced lambda expressions focus on functional programming Also roots in object oriented programming: Smalltalk#DV14 #SmallLambdaTalk @MSkarsaune 3. Smalltalk everything is simple Illustration, reserved words: self (=this), super, nil (=null), true, falseJava reserved words .abstract default goto package synchronizedassert do if private thisboolean double implements protected throwbreak else import public throwsbyte enum instanceof return transientcase extends int short truecatch false interface static trychar final long strictfp voidclass finally native super volatileconst float new switch whilecontinue for null#DV14 #SmallLambdaTalk @MSkarsaune 4. Smalltalk the main conceptSmalltalk's design--and existence--is due to the insight thateverything we can describe can be represented by therecursive composition of a single kind of behavioral buildingblock that hides its combination of state and process insideitself and can be dealt with only through the exchange ofmessages. (Kay 1996)+ Messages=> Can do everything!Objects#DV14 #SmallLambdaTalk @MSkarsaune 5. Ingredient 1: Everything is anobject! No primitive types nil is an object No void methods#DV14 #SmallLambdaTalk @MSkarsaune 6. Ingredient 2: Messages doeverything! Three types of messages Unary Binary Keyword#DV14 #SmallLambdaTalk @MSkarsaune 7. Messages - unary No arguments Invocation example:2 negatedEquivalent Java Syntax2.negated() Implementation in class Number:negated^0 - selfpublic Number negated() {return 0 - this;}#DV14 #SmallLambdaTalk @MSkarsaune 8. Messages - binary Exactly one argument Message selector (method name) made from specialcharacters: = , + , - , > etc. Invocationa = b Implementation in class Object:= anObject^self == anObjectEquivalent Java Syntaxa.equals(b)public boolean equals(Object obj){return this == obj;}#DV14 #SmallLambdaTalk @MSkarsaune 9. Messages - keyword One or more arguments Each argument is separated by a keyword Invocation:a at: 1 put: one Implementation:at: key put: valueself cacheat: key put: valueEquivalent Java Syntaxa.put(1, one);public Object put(Integer key, String value){this.cache().put(key,value);return this;}#DV14 #SmallLambdaTalk @MSkarsaune 10. OO Conditional ProcessingBooleanTruetrueFalsefalsetrue false#DV14 #SmallLambdaTalk @MSkarsaune 11. OO Conditional ProcessingExample Task: Use recursion to return root oftree Each node may access itsparent by sending the parentmessage#DV14 #SmallLambdaTalk @MSkarsaune 12. OO branching- blocksroot^self parent = nilifTrue: [self]ifFalse: [self parent root]Implementation in True: Implementation in False:ifTrue: trueBlock ifFalse:falseBlock^trueBlock valueifTrue: trueBlockifFalse: falseBlock^falseBlock value#DV14 #SmallLambdaTalk @MSkarsaune 13. OO branching- characteristicsroot^self parent = nilifTrue: [self]ifFalse: [self parent root] Voilla! We have made if else with only objects andmessages Boolean instance is an object Code blocks are objects Implemented as one message, one expression, one returnvalue.#DV14 #SmallLambdaTalk @MSkarsaune 14. Object oriented null - Nil check:#isNilMessage isNil can be used to ask an object if it is nil:rootObjectNode UndefinedObject^self parent isNilnil!java.util.Optional.isPresent()ifTrue: [self]ifFalse: [self parent root]isNil^falseisNil^true#DV14 #SmallLambdaTalk @MSkarsaune 15. OO null symmetric equalsrootObjectjava.util.OptionalNode UndefinedObject^self parent = nilprovides symmetric equalsnil( nil = self parent)ifTrue: [self]ifFalse: [self parent root]= anObject^self == anObject#DV14 #SmallLambdaTalk @MSkarsaune 16. Object oriented null: #ifNil:ifNotNil:Message #ifNil:ifNotNil: can be used to branch on nil:rootObjectNode UndefinedObject^self parentnilifNil: [self]ifNotNil: [:par | par root]ifNil: nilBlockifNotNil: block^block value: selfifNil: nilBlockifNotNil: block^nilBlock value#DV14 #SmallLambdaTalk @MSkarsaune 17. More OO conditional logicboolExpr1 && boolExpr2;boolExpr1 || boolExpr2;!boolExpr;True implementation:and: block^block valueboolExpr1 and: [boolExpr2].boolExpr1 or: [boolExpr2].boolExpr notFalse implementation:and: block^selfor: block^selfor: block^block valuenot^falsenot^true#DV14 #SmallLambdaTalk @MSkarsaune 18. Small confession Presentation a bit theoretical The compiler will often optimize with dedicatedbytecodes But the underlying functionality is still there#DV14 #SmallLambdaTalk @MSkarsaune 19. Learning points Anything may be expressed using objects, messagesand lambdas: Branches, loops, exception handling Common language features may be implemented withlambdas Self documenting Possible to debug#DV14 #SmallLambdaTalk @MSkarsaune 20. Thank you for your time!response^self satisfiedifTrue: [self clap]ifFalse: [self boo]More in 10 min #DV14 #SmallLambdaTalk @MSkarsaune