You have probably noticed the excitement regarding functional programming and the new lambda expression in Java 8. But did you know that this also brings Java back to its object-oriented roots?
The Smalltalk programming language shows how virtually any task can be achieved through good old object orientation. Everything is built with the basic ingredients of objects, messages and blocks (lambda).
Smalltalk uses lambda expressions for conditional processing, iteration and exception handling. We will look at some fascinating examples, and discuss if and how to apply them in Java.
We Java programmers now find ourselves with a brand new lambda tool in our toolbox. Lets take the time to learn from the past about the power of object orientation and lambdas.
2. Small lambda talk
• Java 8 introduced lambda expressions
• focus on functional programming
• Also roots in object oriented programming: Smalltalk
3. 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
5. Ingredient 2: Messages do everything!
• Three types of messages
• Unary
• Binary
• Keyword
6. 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
7. 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
8. 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
10. OO Conditional Processing Example
• Task:
• Use recursion to return root of
tree
• Each node may access its
parent by sending the parent
message
11. 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:
12. 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]
13. 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;
14. Learning points
• Anything may be expressed using objects, messages
and lambdas:
• Branches, loops, exception handling ….
• Common language features may be implemented with
lambdas
• Source code acts as documentation
• Possible to debug
• Many ways to arrange program flow
• Polymorphy
• Behavioural design patterns
Workshop 13:3
0 Room #2
15. Thank you for your time!
response
^self satisfied
ifTrue: [self clap]
ifFalse: [self boo]
Notas del editor
Hi everyone.
My name is Martin Skarsaune, and I am a software developer in Kantega here in Norway.
I guess most of you come to this conference to pick up the latest and greatest trends and cool stuff.
Now however we will look at some good old stuff. And when I say old I mean that this stuff existed even before I was born.
But I still believe that we can learn something from the past.
If you have been going to these kind of conferences the last couple of years, you have probably noticed the massive influence of functional programming.
Now the title of this talk “Small Lambda Talk” is no coincidence. We will look at lambdas, but with emphasis on the first and last word combined, Smalltalk.
So I though we should look at at lambdas from an object oriented angle. I had the pleasure of working on a Smalltalk application at CSC. And I believe we as Java developers have a lot to learn from it.
By the way, do we have any grumpy old Smalltalkers in the audience? Has anyone here done or seen any Smalltalk? Do we have anyone who have never heard about the Smalltalk programming language?
So we need to learn a little bit of Smalltalk. But, don’t worry, it’s all very simple. As an illustration, this is the list of reserved words. By just looking at the screen, you have probably figured it out. And as Java developers, you’re used to a lot more than that….
In a Smalltalk, every tiny piece of the application is an object. What does that mean?
There are no primitive types
Even nil is an object, so all references point to an object.
There are no void methods, so whatever you do produces a result, which must be an object.
So we have objects everywhere, which is nice. But how do we make our application do anything? Well, we remember objects may have state and behaviour, right?
So we send the them messages. That is Smalltalk lingo for calling methods. There are three kind of messages, due to slightly different syntax, but conceptually and at runtime the are identical.
The simplest type of message is the unary message. The unary message is comprised of only an identifier. We say we send the message negated to 2. I have added some Java-like syntax beside to make it look more familiar.
And the declaration is identical, as the language is dynamically typed and there are no method modifiers the signature also only consists of an identifier. In this case we see that the method is implemented by a single statement. The caret at the beginning means return, or in Smalltalk terminology we say we answer 0 minus myself.
The second kind of message is the binary message. Here the message selector is made from one or more special characters, such as arithmetic operators. Here is an example. We say we send the message equals to a with argument b. If we look at the implementation we see that the default implementation of equals is, as in Java to make an identity comparison.
The third kind of message is the keyword message. A keyword message has one or more arguments. Each argument is separated by a keyword. This gives very good overview of the role of each argument.
The main lesson is that anything can be expressed with objects and messages. I have shown some very simple examples, but the same also applies to things such as loops and exception handling, admittedly with some compiler and vm support.
As the required components are so easily accessible, Smalltalk libraries and your own code typically provides a wide range of specialized iteration and testing constructs.
For instance if you keep on making the same tests in your code, you might instead create a reusable lambda based test. Then if the underlying conditions changes at some point in time, you can easily track down all code that depends on it.
Or if I for instance if I wanted to offer norwegian message names instead of English, for my kids, the Smalltalk way makes it very easy to extend the language In this way.
Also if you are working on a domain specific language or an interpreter or something like that, you might want to look at the Smalltalk class libraries for some really elegant solutions to divide an conquer low level functionality.
In Brian Goetz futures talk yesterday , there was some discussion on where language enhancements should go, vm, language and compiler or libraries. You will see that the Smalltalk way is the extremely geared towards libraries offloading the language and vm from low level issues.
And with that I would like to thank you for your patience.