10. smalltalk and closures programmierung 2, ss 2007 adrian kuhn, phd candidate software composition...

45
10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Upload: gyles-willis

Post on 17-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

10. Smalltalk and Closures

Programmierung 2, SS 2007

Adrian Kuhn, PhD candidate

Software Composition Group

University of Bern

Page 2: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Beyond Java

Page 3: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Beyond is the future

Page 4: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Beyond Java 6.0 is Java 7.0

Page 5: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Beyond, also, is the past

Page 6: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Beyond Java is Smalltalk

Developed in the 70ies

Smalltalk-80 since 1980

Page 7: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

For example, Closures

Planned for Java 7.0 in 2008

Featured in Smalltalk since 1980

Page 8: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Get winner I

public boolean hasWinner() { for (Player each : players) { if (each.isWinner()) { return true; } } return false;}

Page 9: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Get winner II

public boolean hasWinner() { return players.contains({ Player each | each.isWinner() });}

Page 10: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Get winner III

hasWinner ^players contains: [ :each | each isWinner ]

Page 11: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Handler I

Jbutton button = new JButton(“Save”);button.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { this.saveAndClose(); } });

Page 12: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Handler II

Jbutton button = new JButton(“Save”);button.addActionListener({ ActionEvent e | this.saveAndClose() });

Page 13: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Handler III

button = Button new: ‘Save’.Button addActionListener: [ :event | self saveAndClose ].

Page 14: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Closure

• A code block as first-class object

• Developed in the 60ies

• First implemented in Scheme, ie LISP

• Smalltalk-80, Ruby, Python, etc…

• In discussion for Java 7

Page 15: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Smalltalk

• An object oriented language

• Everything is an object– including primitive values

• Everything is a message send– including the control flow

Page 16: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Syntax

How to format it?

Page 17: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Smalltalk Syntax IAlbum album = new Album();album.play();album.playTrack(1);album.playTrack(1,5);

Album = Album new. album play.album playTrack: 1.album playFromTrack: 1 to: 5.

Page 18: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Smalltak Syntax IIalbum.setName(“Fat of the Land”);String str = album.getName();char ch = str.charAt(0); //starts at 0assert ch == ‘F’;

album name: ‘Fat of the Land’.str := album name.ch := str at: 1. “starts at 1, not 0!”[ ch == $F ] assert.

Page 19: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Semantics

What does it mean?

Page 20: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Smalltak Semantics// Semantics of = is assignment// Semantics of “…” defines a string literal

variable = “Foobar”; assert variable == null; //blahblah

“Semantics of = is comparison”“Semantics of “…” defines a comment”

variable := ‘Foobar’.[ variable = nil ] assert. “blahblah”

Page 21: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Closure, more examples

Page 22: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Closure syntaxdiff := [ :a :b | Transcript show: a. Transcript show: b. (a - b) abs “closure return the result “of the last expression.” ].

x := 23;y := 42;Z := diff value: x value: y.

Page 23: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Sort I

List<String> list = /* some code here */;Collections.sort(list, new Comparable<String>() { public int compare(String a, String b) { return o1.length() - o2.length();

}); }

Page 24: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Sort II

List<String> list = /* some code here */;Collections.sort(list, { String a, String b | return o1.length() - o2.length() });

Page 25: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Sort III

list = “some code here”. List sort: [ :a :b | a size <= b size ].

Page 26: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Iterate collectionfor (Object each : list) { System.out.println(each);}

list.do({ Object each | System.out.println(each) });

list do: [ :each | Transcript show: each ].

Page 27: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Sequence Diagram:Client a :Arrayb :Closure

b value: a1

a do: b

b value: a2

b value: a3

b value: an

Page 28: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Times repeatfor (Integer n = 0; n < 10; n++) { System.out.println(“foo”);}

10.timesRepeat({ System.out.println(“foo”) });

10 timesRepeat: [ Transcript show: ‘foo’ ].

Page 29: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

For loopfor (Integer n = 0; n < 10; n++) { System.out.println(n+1);}

0.loop(10,{ int each | System.out.println(each+1) });

1 to: 10 do: [ :each | Transcript show: each ].

Page 30: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Branchingif (game.hasWinner()) { System.out.prinrtln(game.getWinner());}

game.hasWinner().ifTrue({ System.out.println(game.getWinner()) });

game hasWinner ifTrue: [ Transcript show: game winner ].

Page 31: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Class Diagram

TrueifTrue: aClosure ^aClosure value

FalseifTrue: aClosure ^nil

BooleanifTrue: aClosure “abstract”

Page 32: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

For loopint sum = 1;for (int each : numbers) { sum += each;}

numbers.map(0,{ int sum, int each | sum + each });

numbers inject: 0 into: [ :sum :each | sum + each ].

Page 33: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Closures and Collections

Collection >> do:

Collection >> allSatisfy:Collection >> anySatisfy:Collection >> contains:Collection >> inject:into: Collection >> detect:Collection >> select:Collection >> reject:

ArrayedCollection >> sort:

Page 34: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Closures and Flow Control

Boolean >> ifTrue:Boolean >> ifFalse:Boolean >> ifTrue:ifFalse:

Integer >> timesRepeat:Integer >> to:do:

BlockClosure >> whileTrue:BlockClosure >> whileFalse:

Page 35: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

More Smalltalk

Page 36: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Smalltalk’s family tree

Page 37: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Family tree

Objects, Garbage Collection, Byte code, etc…

Bit Blitting

Overlapping windows

Model-View-Controller

Design Patterns

Refactoring Browser

Unit Testing

Collection framework

Extreme programming

JIT compilation

1st Apple Prototype

Eclipse IDE

Java, Self, JavaScript

1st wiki community

Page 38: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

And some name dropping

• Alan Kay• Dan Ingalls• Adele Goldberg• Ted Kaehler• Scott Wallance• John Brandt• Don Roberts

• Kent Beck• Gilad Bracha• Ward Cunningham• Erich Gamma• Ron Jeffries• Martin Fowler• Ralph Johnson

Page 39: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Pure Object-Orientation

• Pure object oriented language

• Everything is an object– including primitive values

• Everything is a message send– including the control flow

Page 40: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Persistent Object Memory

• Smalltalk is language and environment

• Everything inside the image– persistent objects– fully reflective system– incremental compilation– “hot debugging”

Page 41: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Virtual machine

• Compiled code, ie byte code• Source code• Objects• Classes• IDE• Application

*.im file, the image

Page 42: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Squeak

Open-source Smalltalk

www.squeak.org

Page 43: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Squeak ScreenshotQuickTime™ and a

TIFF (Uncompressed) decompressorare needed to see this picture.

Page 44: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

Hands-on demo

Postoffice exercise in Smalltalk

Page 45: 10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern

More see P2 wiki

smallwiki.unibe.ch/p22007smallwiki