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

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Chapter 1 Chapter 2 Chapter 3 Code Gone Wrong Random 5 pt 5 pt 5 pt
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Principles of Object-Oriented Software Development The language Smalltalk.
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Unit Six Assignment 1 Chris Boardley.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Smalltalk Andy Ko Johnvic Dualan Aaron Israel John John Llenteng Jehrom Christian De Vera Carlo Alvarez Michael John Butuyan.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
High-Level Programming Languages: C++
3A-1 1 Introduction to Smalltalk History of Smalltalk The philosophy of Smalltalk:  “...we have a universe of well-behaved objects that courteously ask.
CIS Computer Programming Logic
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
A brief introduction to 11ACM 朱旻申. Outline What is Groovy ? Differences between Groovy & Java About closure & Dagger Methods.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CS 403: Programming Languages Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Smalltalk for Unix & W i n d o w s Dennis Smith Cherniak Software Development Corporation.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Introduction to Java Java Translation Program Structure
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Squeak and Botkit-Port Jeff Forbes Joel Miller. Introduction ● Squeak is a pure OO language ● Based off of SmallTalk-80 – Designed at Xerox PARC by Alan.
A tool for test-driven development
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Smalltalk Mark Colangelo CMSC-631, Fall December 2002.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Basic Concepts: computer, program, programming …
SmallTalk Done By: Nathalie Arzu Erienne Burgess Shanice Joseph.
JAVA MULTIPLE CHOICE QUESTION.
Lambdas ---anonymous functions---
C Basics.
Software Programming J. Holvikivi 2014.
Aaron Bloomfield CS 415 Fall 2005
Smalltalk – a Pure (and first) OOPL
The Boolean (logical) data type boolean
Chapter (3) - Looping Questions.
Sridhar Narayan Java Basics Sridhar Narayan
Unit 6 - Variables - Fundamental Data Types
High Level Programming Languages
Introduction to Smalltalk
Let’s Talk about… Smalltalk.
CSE 3302 Programming Languages
Presentation transcript:

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 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, more examples

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

Sort II List 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 :Clienta :Arrayb :Closure b value: a 1 a do: b b value: a 2 b value: a 3 b value: a n

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

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

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:

More Smalltalk

Smalltalk’s family tree

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

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

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

Squeak Screenshot

Hands-on demo Postoffice exercise in Smalltalk

More see P2 wiki smallwiki.unibe.ch/p22007smallwiki