Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "10. Smalltalk and Closures Programmierung 2, SS 2007 Adrian Kuhn, PhD candidate Software Composition Group University of Bern."— Presentation transcript:

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

2 Beyond Java

3 Beyond is the future

4 Beyond Java 6.0 is Java 7.0

5 Beyond, also, is the past

6 Beyond Java is Smalltalk Developed in the 70ies Smalltalk-80 since 1980

7 For example, Closures Planned for Java 7.0 in 2008 Featured in Smalltalk since 1980

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

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

10 Get winner III hasWinner ^players contains: [ :each | each isWinner ]

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

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

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

14 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

15 Smalltalk An object oriented language Everything is an object –including primitive values Everything is a message send –including the control flow

16 Syntax How to format it?

17 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.

18 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.

19 Semantics What does it mean?

20 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”

21 Closure, more examples

22 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.

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

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

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

26 Iterate collection for (Object each : list) { System.out.println(each); } list.do({ Object each | System.out.println(each) }); list do: [ :each | Transcript show: each ].

27 Sequence Diagram :Clienta :Arrayb :Closure b value: a 1 a do: b b value: a 2 b value: a 3 b value: a n

28 Times repeat for (Integer n = 0; n < 10; n++) { System.out.println(“foo”); } 10.timesRepeat({ System.out.println(“foo”) }); 10 timesRepeat: [ Transcript show: ‘foo’ ].

29 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 ].

30 Branching if (game.hasWinner()) { System.out.prinrtln(game.getWinner()); } game.hasWinner().ifTrue({ System.out.println(game.getWinner()) }); game hasWinner ifTrue: [ Transcript show: game winner ].

31 Class Diagram True ifTrue: aClosure ^aClosure value False ifTrue: aClosure ^nil Boolean ifTrue: aClosure “abstract”

32 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 ].

33 Closures and Collections Collection >> do: Collection >> allSatisfy: Collection >> anySatisfy: Collection >> contains: Collection >> inject:into: Collection >> detect: Collection >> select: Collection >> reject: ArrayedCollection >> sort:

34 Closures and Flow Control Boolean >> ifTrue: Boolean >> ifFalse: Boolean >> ifTrue:ifFalse: Integer >> timesRepeat: Integer >> to:do: BlockClosure >> whileTrue: BlockClosure >> whileFalse:

35 More Smalltalk

36 Smalltalk’s family tree

37 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

38 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

39 Pure Object-Orientation Pure object oriented language Everything is an object –including primitive values Everything is a message send –including the control flow

40 Persistent Object Memory Smalltalk is language and environment Everything inside the image –persistent objects –fully reflective system –incremental compilation –“hot debugging”

41 Virtual machine Compiled code, ie byte code Source code Objects Classes IDE Application *.im file, the image

42 Squeak Open-source Smalltalk www.squeak.org

43 Squeak Screenshot

44 Hands-on demo Postoffice exercise in Smalltalk

45 More see P2 wiki smallwiki.unibe.ch/p22007smallwiki


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

Similar presentations


Ads by Google