Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Principles of Object-Oriented Software Development The language Smalltalk.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
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 Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse8.1 Syntax and Messages The syntax of Smalltalk is simple and uniform, but it can look strange at first sight! Literals: numbers, strings,
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.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Some Basic Points on Classes
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
Stéphane Ducasse 1 Syntax and Messages.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.
JavaScript, Fourth Edition
JavaScript, Third Edition
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
3. Standard Classes. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify everything — by reifying its entire implementation model, Smalltalk.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
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.
Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes.
Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CIS Computer Programming Logic
Chapter 3: Data Types and Operators JavaScript - Introductory.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Stéphane Ducasse 1 Singleton.
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Stéphane Ducasse 1 Some Advanced Points on Classes.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
Let’s Play Objects.
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Smalltalk – a Pure (and first) OOPL
An overview of Java, Data types and variables
Chapter 2 Squeak: Object-oriented design with multimedia applications
Introduction to Smalltalk
Lexical Elements & Operators
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax

S.Ducasse 2 Goal Lower your stress :) Show you that this is simple

S.Ducasse 3 Appetizer!

S.Ducasse 4

5 Yeah! Smalltalk is a dynamically typed language

S.Ducasse 6 ArrayList strings = new ArrayList (); strings := ArrayList new.

S.Ducasse 7 Shorter Thread regThread = new Thread( new Runnable() { public void run() { this.doSomething();} }); regThread.start(); [self doSomething] fork.

S.Ducasse 8 Smalltalk = Objects + Messages + (...)

S.Ducasse 9 Roadmap Fun with numbers

S.Ducasse 10 1 class

S.Ducasse 11 1 class >SmallInteger

S.Ducasse 12 1 class maxVal

S.Ducasse 13 1 class maxVal >

S.Ducasse 14 1 class maxVal + 1

S.Ducasse 15 1 class maxVal + 1 >

S.Ducasse 16 (1 class maxVal + 1) class

S.Ducasse 17 (1 class maxVal + 1) class >LargePositiveInteger

S.Ducasse 18 (1/3) + (2/3)

S.Ducasse 19 (1/3) + (2/3) >1

S.Ducasse 20 2/3 + 1

S.Ducasse 21 2/3 + 1 > 5/3

S.Ducasse factorial

S.Ducasse factorial

S.Ducasse factorial / 999 factorial

S.Ducasse factorial / 999 factorial > 1000

S.Ducasse

S.Ducasse ) x

S.Ducasse ) x > 10

S.Ducasse ) x > ) y

S.Ducasse ) x > ) y >100

S.Ducasse 31 Points! Points are created

S.Ducasse 32 Puzzle +

S.Ducasse 33 Puzzle +

S.Ducasse 34 Puzzle +

S.Ducasse 35 Roadmap Fun with characters, strings, arrays

S.Ducasse 36 $C $h $a $r $a $c $t $e $r $F, $Q $U $E $N $T $i $N

S.Ducasse 37 space tab?! Character space Character tab Character cr

S.Ducasse 38 ‘Strings’ ‘Tiramisu’

S.Ducasse 39 Characters 12 printString > ’12’

S.Ducasse 40 Strings are collections of chars ‘Tiramisu’ at: 1

S.Ducasse 41 Strings are collections of chars ‘Tiramisu’ at: 1 > $T

S.Ducasse 42 A program! -- finding the last char

S.Ducasse 43 A program! | str |

S.Ducasse 44 A program! | str | local variable

S.Ducasse 45 A program! | str | local variable str := ‘Tiramisu’.

S.Ducasse 46 A program! | str | local variable str := ‘Tiramisu’. assignment

S.Ducasse 47 A program! | str | local variable str := ‘Tiramisu’. assignment str at: str length

S.Ducasse 48 A program! | str | local variable str := ‘Tiramisu’. assignment str at: str length message send > $u

S.Ducasse 49 double ‘ to get one ‘L’’Idiot’ > one string

S.Ducasse 50 For concatenation use, ‘Calvin’, ‘ & ‘, ‘Hobbes’

S.Ducasse 51 For concatenation use, ‘Calvin’, ‘ & ‘, ‘Hobbes’ > ‘Calvin & Hobbes’

S.Ducasse 52 For concatenation use, ‘Calvin’, ‘ & ‘, ‘Hobbes’ > ‘Calvin & Hobbes’

S.Ducasse 53 Symbols: #Pharo #Something is a symbol Symbol is a unique string in the system #Something == #Something > true

S.Ducasse 54 “Comment” “what a fun language lecture. I really liked the desserts”

S.Ducasse 55 #(Array) #(‘Calvin’ ‘hates’ ‘Suzie’)

S.Ducasse 56 #(Array) #(‘Calvin’ ‘hates’ ‘Suzie’) size

S.Ducasse 57 #(Array) #(‘Calvin’ ‘hates’ ‘Suzie’) size > 3

S.Ducasse 58 First element starts at 1 #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2

S.Ducasse 59 First element starts at 1 #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 > ‘hates’

S.Ducasse 60 at: to access, at:put: to set #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’

S.Ducasse 61 #(Array) #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’ > #(‘Calvin’ ‘loves’ ‘Suzie’)

S.Ducasse 62 Syntax Summary comment: “a comment” character: $c $h $a $r $a $c $t $e $r $s $# string: ‘a nice string’ ‘lulu’ ‘l’’idiot’ symbol: #mac #+ array: #(1 2 3 (1 3) $a 4) byte array: #[1 2 3] integer: 1, 2r101 real: 1.5, 6.03e-34,4, 2.4e7 fraction: 1/33 boolean: true, false point:

S.Ducasse 63 Roadmap Fun with keywords-based messages

S.Ducasse 64 Keyword-based messages arr at: 2 put: ‘loves’

S.Ducasse 65 Keyword-based messages arr at: 2 put: ‘loves’ somehow like arr.atput(2,‘loves’)

S.Ducasse 66 From Java to Smalltalk postman.send(mail,recipient);

S.Ducasse 67 Removing postman.send(mail,recipient);

S.Ducasse 68 Removing unnecessary postman send mail recipient

S.Ducasse 69 But without losing information postman send mail to recipient

S.Ducasse 70 postman send: mail to: recipient postman.send(mail,recipient);

S.Ducasse 71 postman send: mail to: recipient The message is send:to: postman.send(mail,recipient);

S.Ducasse 72 Roadmap Fun with variables

S.Ducasse 73 Shared or Global starts with Uppercase Transcript cr. Transcript show: ‘hello world’. Transcript cr.

S.Ducasse 74 local or temps starts with lowercase | str | str := ‘Tiramisu’

S.Ducasse 75 self, super, true, false, nil self = this super true, false are for Booleans nil is UndefinedObject instance

S.Ducasse 76 self, super, true, false, nil self = this in Java super true, false are for Booleans nil is UndefinedObject instance

S.Ducasse 77 Roadmap Fun with classes

S.Ducasse 78 A class definition! Superclass subclass: #Class instanceVariableNames: 'a b c'... category: 'Package name'

S.Ducasse 79 A class definition! Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

S.Ducasse 80 A class definition! Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

S.Ducasse 81 Roadmap Fun with methods

S.Ducasse 82 On Integer asComplex "Answer a Complex number that represents value of the the receiver." ^ Complex real: self imaginary: 0

S.Ducasse 83 On Boolean xor: aBoolean "Exclusive OR. Answer true if the receiver is not equivalent to aBoolean." ^(self == aBoolean) not

S.Ducasse 84 Summary self, super can access instance variables can define local variable |... | Do not need to define argument types ^ to return

S.Ducasse 85 Roadmap Fun with unary messages

S.Ducasse 86 1 class

S.Ducasse 87 1 class > SmallInteger

S.Ducasse 88 false not

S.Ducasse 89 false not > true

S.Ducasse 90 Date today

S.Ducasse 91 Date today > 24 May 2009

S.Ducasse 92 Time now

S.Ducasse 93 Time now > 6:50:13 pm

S.Ducasse 94 Float pi

S.Ducasse 95 Float pi >

S.Ducasse 96 We sent messages to objects or classes! 1 class Date today

S.Ducasse 97 We sent messages to objects or classes! 1 class Date today

S.Ducasse 98 Fun with binary messages Roadmap

S.Ducasse 99 aReceiver aSelector anArgument Used for arithmetic, comparison and logical operations One or two characters taken from: + - / \ * ~ % | & ! ?,

S.Ducasse

S.Ducasse >3

S.Ducasse => 3

S.Ducasse => 3 > false

S.Ducasse

S.Ducasse 105 ‘Black chocolate’, ‘ is good’

S.Ducasse 106 Roadmap Fun with keyword-based messages

S.Ducasse 107 #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’

S.Ducasse 108 #(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’ > #(‘Calvin’ ‘loves’ ‘Suzie’)

S.Ducasse 109 setX: 2

S.Ducasse 110 setX: 2 >

S.Ducasse between: 10 and: 20

S.Ducasse between: 10 and: 20 > true

S.Ducasse 113 receiver keyword1: argument1 keyword2: argument2 equivalent to receiver.keyword1keyword2(argument 1, argument2)

S.Ducasse 114 receiver keyword1: argument1 keyword2: argument2 equivalent to receiver.keyword1keyword2(argument 1, argument2)

S.Ducasse 115 Browser newOnClass: Point Roadmap

S.Ducasse 116 Browser newOnClass: Point

S.Ducasse 117 Browser newOnClass: Point

S.Ducasse 118 Yes there is a difference between doing (side effect) and returning an object

S.Ducasse 119 Browser newOnClass: Point > a Browser

S.Ducasse 120 Doing and do not care of the returned result Browser newOnClass: Point

S.Ducasse 121 Doing and really want to see the result! setX: 2 >

S.Ducasse 122 Doing vs printing (doing + print result)

S.Ducasse 123 Doing vs printing (doing + print result)

S.Ducasse 124 Roadmap Messages messages messages again messages....

S.Ducasse 125 Yes there are only messages unary binary keywords

S.Ducasse 126 Composition: from left to right! 69 class inspect 69 class superclass superclass inspect

S.Ducasse 127 Unary> Binary> Keywords Precedence

S.Ducasse squared

S.Ducasse squared > 2 + 9

S.Ducasse squared > > 11

S.Ducasse 131 Color gray - Color white = Color black

S.Ducasse 132 Color gray - Color white = Color black > aColor = Color black

S.Ducasse 133 Color gray - Color white = Color black > aColor = Color black > true

S.Ducasse raisedTo: 3 + 2

S.Ducasse raisedTo: > 2 raisedTo: 5

S.Ducasse raisedTo: > 2 raisedTo: 5 > 32

S.Ducasse 137 1/3 + 2/3 No mathematical precedence

S.Ducasse 138 1/3 + 2/3 >7/3 /3 No mathematical precedence

S.Ducasse 139 Parenthesized takes precedence! (Msg) > Unary> Binary> Keywords

S.Ducasse 140 extent: bottomRight

S.Ducasse 141 extent: bottomRight > (aPoint extent: anotherPoint) bottomRight

S.Ducasse 142 extent: bottomRight > (aPoint extent: anotherPoint) bottomRight > aRectangle bottomRight

S.Ducasse 143 extent: bottomRight > (aPoint extent: anotherPoint) bottomRight > aRectangle bottomRight >

S.Ducasse 144 extent: bottomRight

S.Ducasse 145 extent: bottomRight > Message not understood > 100 does not understand bottomRight

S.Ducasse * 10 No mathematical precedence

S.Ducasse * 10 > 5 * 10 No mathematical precedence

S.Ducasse * 10 > 5 * 10 > 50 argh! No mathematical precedence

S.Ducasse (2 * 10) No mathematical precedence

S.Ducasse (2 * 10) > No mathematical precedence

S.Ducasse (2 * 10) > > 23 No mathematical precedence

S.Ducasse 152 1/3 + 2/3 > 7/3 /3 No mathematical precedence

S.Ducasse 153 1/3 + 2/3 > (7/3) /3 > 7/9 No mathematical precedence

S.Ducasse 154 (1/3) + (2/3) No mathematical precedence

S.Ducasse 155 (1/3) + (2/3) > 1 No mathematical precedence

S.Ducasse 156 (Msg) > Unary > Binary > Keywords from left to right No mathematical precedence Only Messages

S.Ducasse 157 (Msg) > Unary > Binary > Keywords from left to right No mathematical precedence Only Messages

S.Ducasse 158 Roadmap Fun with blocks

S.Ducasse 159 Function definition fct(x) = x * x + x

S.Ducasse 160 Function Application fct (2) = 6 fct (20) = 420

S.Ducasse 161 Function definition fct(x) = x * x + x |fct| fct:= [:x | x * x + x].

S.Ducasse 162 Function Application fct (2) = 6 fct (20) = 420 fct value: 2 > 6 fct value: 20 > 420

S.Ducasse 163 Other examples [ ] value [:x | x ] value: 2 [:x :y | x + y ] value: 2 value: 3

S.Ducasse 164 Block anonymous method [ :variable1 :variable2 | | tmp | expression1....variable1...] value:...

S.Ducasse 165 Block anonymous method Really really cool! Can be passed to methods, stored in instance variables [ :variable1 :variable2 | | tmp | expression1....variable1...] value:...

S.Ducasse 166 Roadmap Fun with conditional

S.Ducasse 167 Example 3 > 0 ifTrue:['positive'] ifFalse:['negative' ]

S.Ducasse 168 Example 3 > 0 ifTrue:['positive'] ifFalse:['negative' ] > ‘positive’

S.Ducasse 169 Yes ifTrue:ifFalse: is a message! Weather isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses] ifTrue:ifFalse is sent to an object: a boolean!

S.Ducasse 170 Booleans & | not or: and: (lazy) xor: ifTrue:ifFalse: ifFalse:ifTrue:...

S.Ducasse 171 Yes! ifTrue:ifFalse: is a message send to a Boolean. But optimized by the compiler :)

S.Ducasse timesRepeat: [ Transcript show: 'hello'; cr]

S.Ducasse timesRepeat: [ Transcript show: 'hello'; cr]

S.Ducasse 174 [x<y] whileTrue: [x := x + 3]

S.Ducasse 175 aBlockTest whileTrue aBlockTest whileFalse aBlockTest whileTrue: aBlockBody aBlockTest whileFalse: aBlockBody anInteger timesRepeat: aBlockBody

S.Ducasse 176 Confused with () and [] ?

S.Ducasse 177 Only put [ ] when you do not the number of times something may be executed (x isBlue) ifTrue: [ x schroumph ] 10 timesRepeat: [ self shout ]

S.Ducasse 178 Conditions are messages sent to boolean (x isBlue) ifTrue: [ ]

S.Ducasse 179 Fun with loops Roadmap

S.Ducasse to: 100 do: [ :i | Transcript show: i ; space]

S.Ducasse to: 100 do: [ :i | Transcript show: i ; space]

S.Ducasse to: 100 by: 3 do: [ :i | Transcript show: i ; space]

S.Ducasse to: 100 by: 3 do: [ :i | Transcript show: i ; space]

S.Ducasse 184 So yes there are real loops in Smalltalk! to:do: to:by:do: are just messages send to integers

S.Ducasse 185 So yes there are real loops in Smalltalk! to:do: to:by:do: are just messages send to integers

S.Ducasse 186 Roadmap Fun with iterators

S.Ducasse 187 ArrayList strings = new ArrayList (); for(Person person: persons) strings.add(person.name()); strings := persons collect [:person | person name].

S.Ducasse 188 #( ) collect: [ :each| each abs]

S.Ducasse 189 #( ) collect: [ :each| each abs] > #( )

S.Ducasse 190 #( ) collect: [:i | i odd ]

S.Ducasse 191 #( ) collect: [:i | i odd ] > #(true false true false)

S.Ducasse 192 #( ) collect: [:i | i odd ] We can also do it that way! |result| aCol := #( ). result := aCol species new: aCol size. 1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) odd]. result

S.Ducasse 193 #( ) do: [:i | Transcript show: i ; cr ]

S.Ducasse 194 #( ) do: [:i | Transcript show: i ; cr ]

S.Ducasse 195 #(1 2 3) with: #( ) do: [:x :y| Transcript show: (y ** x) ; cr ]

S.Ducasse 196 #(1 2 3) with: #( ) do: [:x :y| Transcript show: (y ** x) ; cr ]

S.Ducasse 197 How do: is implemented?

S.Ducasse 198 How do: is implemented? SequenceableCollection>>do: aBlock "Evaluate aBlock with each of the receiver's elements as the argument." 1 to: self size do: [:i | aBlock value: (self at: i)]

S.Ducasse 199 Some others... friends #( ) select: [:i|i odd] #( ) reject: [:i|i odd] #( ) detect: [:i|i odd] #( ) detect: [:i|i odd] ifNone:[1]

S.Ducasse 200 Some others... friends #( ) select: [:i|i odd] > #(15 19) #( ) reject: [:i|i odd] #( ) detect: [:i|i odd] #( ) detect: [:i|i odd] ifNone:[1]

S.Ducasse 201 Some others... friends #( ) select: [:i|i odd] > #(15 19) #( ) reject: [:i|i odd] > #(10 68) #( ) detect: [:i|i odd] #( ) detect: [:i|i odd] ifNone:[1]

S.Ducasse 202 Some others... friends #( ) select: [:i|i odd] > #(15 19) #( ) reject: [:i|i odd] > #(10 68) #( ) detect: [:i|i odd] > 19 #( ) detect: [:i|i odd] ifNone:[1]

S.Ducasse 203 Some others... friends #( ) select: [:i|i odd] > #(15 19) #( ) reject: [:i|i odd] > #(10 68) #( ) detect: [:i|i odd] > 19 #( ) detect: [:i|i odd] ifNone:[1] > 1

S.Ducasse 204 Iterators are your best friends compact nice abstraction Just messages sent to collections

S.Ducasse 205 Iterators are your best friends compact nice abstraction Just messages sent to collections

S.Ducasse 206 A simple exercise How do you define the method that does that? #() -> ‘’ #(a) ->’a’ #(a b c) -> ‘a, b, c’

S.Ducasse 207 #(a b c) do: [:each | Transcript show: each printString] separatedBy: [Transcript show: ‘,’]

S.Ducasse 208 #(a b c) do: [:each | Transcript show: each printString] separatedBy: [Transcript show: ‘,’]

S.Ducasse 209

S.Ducasse 210 Messages Sequence message1. message2. message3. is a separator, not a terminator | macNode pcNode node1 printerNode | macNode := Workstation withName: #mac. Transcript cr. Transcript show: 1 printString. Transcript cr. Transcript show: 2 printString

S.Ducasse 211 Multiple messages to an objects ; To send multiple messages to the same object Transcript show: 1 printString. Transcript cr is equivalent to: Transcript show: 1 printString ; cr

S.Ducasse 212 Hints... x isNil ifTrue: [...] x includes: 3 ifTrue: [...] is read as the message includes:ifTrue: (x includes: 3) ifTrue: [...]

S.Ducasse 213

S.Ducasse 214 Smalltalk is fun Pure simple powerful (