450 likes | 476 Views
10. Smalltalk and Closures. Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern. Beyond Java. Beyond is the future. Beyond Java 6.0 is Java 7.0. Beyond, also, is the past. Beyond Java is Smalltalk. Developed in the 70ies
E N D
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
For example, Closures Planned for Java 7.0 in 2008 Featured in Smalltalk since 1980
Get winner I public boolean hasWinner() { for (Player each : players) { if (each.isWinner()) { return true; } } return false; }
Get winner II public boolean hasWinner() { return players.contains({ Player each | each.isWinner() }); }
Get winner III hasWinner ^players contains: [ :each | each isWinner ]
Handler I Jbutton button = new JButton(“Save”); button.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { this.saveAndClose(); } });
Handler II Jbutton button = new JButton(“Save”); button.addActionListener({ ActionEvent e | this.saveAndClose() });
Handler III button = Button new: ‘Save’. Button addActionListener: [ :event | self saveAndClose ].
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
Smalltalk • An object oriented language • Everything is an object • including primitive values • Everything is a message send • including the control flow
Syntax How to format it?
Smalltalk Syntax I Album 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.
Smalltak Syntax II album.setName(“Fat of the Land”); String str = album.getName(); char ch = str.charAt(0); //starts at 0 assert ch == ‘F’; album name: ‘Fat of the Land’. str := album name. ch := str at: 1. “starts at 1, not 0!” [ ch == $F ] assert.
Semantics What does it mean?
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”
Closure syntax diff := [ :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.
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(); } });
Sort II List<String> list = /* some code here */; Collections.sort(list, { String a, String b | return o1.length() - o2.length() });
Sort III list = “some code here”. List sort: [ :a :b | a size <= b size ].
Iterate collection for (Object each : list) { System.out.println(each); } list.do({ Object each | System.out.println(each) }); list do: [ :each | Transcript show: each ].
Sequence Diagram :Client b :Closure a :Array a do: b b value: a1 b value: a2 b value: a3 b value: an
Times repeat for (Integer n = 0; n < 10; n++) { System.out.println(“foo”); } 10.timesRepeat({ System.out.println(“foo”) }); 10 timesRepeat: [ Transcript show: ‘foo’ ].
For loop for (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 ].
Branching if (game.hasWinner()) { System.out.prinrtln(game.getWinner()); } game.hasWinner().ifTrue({ System.out.println(game.getWinner()) }); game hasWinner ifTrue: [ Transcript show: game winner ].
True False Boolean ifTrue: aClosure ^aClosure value ifTrue: aClosure ^nil ifTrue: aClosure “abstract” Class Diagram
For loop int 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 ].
Closures and Collections Collection >> do: Collection >> allSatisfy: Collection >> anySatisfy: Collection >> contains: Collection >> inject:into: Collection >> detect: Collection >> select: Collection >> reject: ArrayedCollection >> sort:
Closures and Flow Control Boolean >> ifTrue: Boolean >> ifFalse: Boolean >> ifTrue:ifFalse: Integer >> timesRepeat: Integer >> to:do: BlockClosure >> whileTrue: BlockClosure >> whileFalse:
Extreme programming Family tree Eclipse IDE Unit Testing JIT compilation 1st wiki community Java, Self, JavaScript Refactoring Browser Design Patterns 1st Apple Prototype Collection framework Overlapping windows Bit Blitting Model-View-Controller Objects, Garbage Collection, Byte code, etc…
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 And some name dropping
Pure Object-Orientation • Pure object oriented language • Everything is an object • including primitive values • Everything is a message send • including the control flow
Persistent Object Memory • Smalltalk is language and environment • Everything inside the image • persistent objects • fully reflective system • incremental compilation • “hot debugging”
Virtual machine • Compiled code, ie byte code • Source code • Objects • Classes • IDE • Application *.im file, the image
Squeak Open-source Smalltalk www.squeak.org
Hands-on demo Postoffice exercise in Smalltalk
More see P2 wiki smallwiki.unibe.ch/p22007smallwiki