1 / 45

10. Smalltalk and Closures

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

roseboyd
Download Presentation

10. Smalltalk and Closures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  24. Sort II List<String> 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 :Client b :Closure a :Array a do: b b value: a1 b value: a2 b value: a3 b value: an

  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. True False Boolean ifTrue: aClosure ^aClosure value ifTrue: aClosure ^nil ifTrue: aClosure “abstract” Class Diagram

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

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

  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

More Related