A Little Journey in the Smalltalk...

216
Stéphane Ducasse Stéphane Ducasse [email protected] http://stephane.ducasse.free.fr/ !"#$ A Little Journey in the Smalltalk Syntax 1

Transcript of A Little Journey in the Smalltalk...

Page 1: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

Stéphane Ducasse

Stéphane [email protected]://stephane.ducasse.free.fr/

!"#$

A Little Journey in the Smalltalk Syntax

1

Page 2: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Goal

Lower your stress :)Show you that this is simple

2

Page 3: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Appetizer!

3

Page 4: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

4

Page 5: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Yeah!

Smalltalk is a dynamically typed language

5

Page 6: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

ArrayList<String> strings = new ArrayList<String>();

strings := ArrayList new.

6

Page 7: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Shorter

Thread regThread = new Thread( new Runnable() { public void run() { this.doSomething();} }); regThread.start();

[self doSomething] fork.

7

Page 8: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Smalltalk = Objects + Messages + (...)

8

Page 9: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with numbers

9

Page 10: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class

10

Page 11: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class >SmallInteger

11

Page 12: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class maxVal

12

Page 13: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class maxVal>1073741823

13

Page 14: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class maxVal + 1

14

Page 15: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class maxVal + 1>1073741824

15

Page 16: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1 class maxVal + 1) class

16

Page 17: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1 class maxVal + 1) class>LargePositiveInteger

17

Page 18: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1/3) + (2/3)

18

Page 19: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1/3) + (2/3)>1

19

Page 20: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2/3 + 1

20

Page 21: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2/3 + 1> 5/3

21

Page 22: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1000 factorial

22

Page 23: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1000 factorial 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

23

Page 24: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1000 factorial / 999 factorial

24

Page 25: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1000 factorial / 999 factorial> 1000

25

Page 26: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 100

26

Page 27: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x

27

Page 28: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

28

Page 29: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y

29

Page 30: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y>100

30

Page 31: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Points!

Points are created using @

31

Page 32: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100)

32

Page 33: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100) >30@200

33

Page 34: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100) >30@200

34

Page 35: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with characters, strings, arrays

35

Page 36: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

$C $h $a $r $a $c $t $e $r

$F $Q $U $E $N $T $i $N

36

Page 37: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

space? tab?

Character spaceCharacter tabCharacter cr

37

Page 38: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

'Strings'

'Tiramisu'

38

Page 39: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Characters

12 printString

> '12'

39

Page 40: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Strings are collections of chars

'Tiramisu' at: 1

40

Page 41: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Strings are collections of chars

'Tiramisu' at: 1

> $T

41

Page 42: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program — Finding the last character

42

Page 43: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str |

43

Page 44: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str | local variable

44

Page 45: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'.

45

Page 46: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignment

46

Page 47: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignmentstr at: str size

47

Page 48: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignmentstr at: str size message send

> $u

48

Page 49: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

double ' to get one

'L''Idiot'

> one string, containing a single apostrophe

49

Page 50: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'

50

Page 51: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'> 'Clavin & Hobbes'

51

Page 52: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'> 'Calvin & Hobbes'

52

Page 53: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Symbols: #Pharo

#Something is a symbol

Symbol is a unique string in the system

#Something == #Something > true

53

Page 54: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

“Comment”

“what a fun language lecture.I really liked the desserts”

54

Page 55: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie')

55

Page 56: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') size

56

Page 57: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') size> 3

57

Page 58: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

First element starts at 1

#('Calvin' 'hates' 'Suzie') at: 2

58

Page 59: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

First element starts at 1

#('Calvin' 'hates' 'Suzie') at: 2> 'hates'

59

Page 60: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

at: to access, at:put: to set

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

60

Page 61: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

> #('Calvin' 'loves' 'Suzie')

61

Page 62: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

Syntax Summarycomment: "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 2r101real: 1.5 6.03e-34 4.0 2.4e7fraction: 1/33boolean: true falsepoint: 10@120

62

Page 63: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with keywords-based messages

63

Page 64: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Keyword-based messages

arr at: 2 put: 'loves'

64

Page 65: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Keyword-based messages

arr at: 2 put: 'loves'

somehow like arr.atput(2,'loves')

65

Page 66: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

From Java to Smalltalk

postman.send(mail,recipient);

66

Page 67: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Removing

postman.send(mail,recipient);

67

Page 68: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Removing unnecessary

postman send mail recipient

68

Page 69: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

But without losing information

postman send mail to recipient

69

Page 70: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

postman send: mail to: recipient

postman.send(mail,recipient);

70

Page 71: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

postman send: mail to: recipient

The message is send:to:

postman.send(mail,recipient);

71

Page 72: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with variables

72

Page 73: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Shared or Global starts with Uppercase

Transcript cr .Transcript show: 'hello world'.Transcript cr .

73

Page 74: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

local or temps starts with lowercase

| str |str := 'Tiramisu'

74

Page 75: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

self, super, true, false, nil

self = thissuper

true, false are for Booleans

nil is UndefinedObject instance

75

Page 76: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

self, super, true, false, nil

self = this in Javasuper

true, false are for Booleans

nil is UndefinedObject instance

76

Page 77: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with classes

77

Page 78: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A class definition!

Superclass subclass: #Class instanceVariableNames: 'a b c'

... category: 'Package name'

78

Page 79: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A class definition!

Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

79

Page 80: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A class definition!

Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

80

Page 81: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with methods

81

Page 82: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

On Integer

asComplex"Answer a Complex number that represents value of the the receiver."

^ Complex real: self imaginary: 0

82

Page 83: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

On Boolean

xor: aBoolean "Exclusive OR. Answer true if the receiver is not equivalent to aBoolean."

^(self == aBoolean) not

83

Page 84: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Summary

self, supercan access instance variablescan define local variable | ... |Do not need to define argument types^ to return

84

Page 85: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with unary messages

85

Page 86: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class

86

Page 87: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 class> SmallInteger

87

Page 88: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

false not

88

Page 89: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

false not> true

89

Page 90: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Date today

90

Page 91: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Date today> 24 May 2009

91

Page 92: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Time now

92

Page 93: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Time now> 6:50:03 pm

93

Page 94: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Float pi

94

Page 95: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Float pi> 3.141592653589793

95

Page 96: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

We send messages to both objects and to classes!

1 classDate today

96

Page 97: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

We sent messages to objects or classes!

1 classDate today

97

Page 98: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

Fun with binary messages

98

Roadmap

Page 99: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

aReceiver aSelector anArgument

Used for arithmetic, comparison and logical operationsOne or two characters taken from:

+ - / \ * ~ < > = @ % | & ! ? ,

99

Page 100: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 + 2

100

Page 101: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 + 2>3

101

Page 102: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 => 3

102

Page 103: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 => 3> false

103

Page 104: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 @ 200

104

Page 105: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

'Black chocolate' , ' is good'

105

Page 106: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with keyword-based messages

106

Page 107: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

107

Page 108: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'> #('Calvin' 'loves' 'Suzie')

108

Page 109: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10@20 setX: 2

109

Page 110: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10@20 setX: 2> 2@20

110

Page 111: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

12 between: 10 and: 20

111

Page 112: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

12 between: 10 and: 20> true

112

Page 113: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

113

Page 114: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

114

Page 115: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

Browser newOnClass: Point

115

Roadmap

Page 116: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Browser newOnClass: Point

116

Page 117: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Browser newOnClass: Point

117

Page 118: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Yes there is a difference between doing (side effect)and returning an object

118

Page 119: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Browser newOnClass: Point> a Browser

119

Page 120: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Doing when you do not care about the answer:

Browser newOnClass: Point

120

Page 121: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Doing when you really want to see the answer:

10@20 setX: 2> 2@20

121

This is called printing

Page 122: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

doIt (do some Smalltalk code) vs printIt (doIt + printing              the answer)

122

Page 123: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

123

doIt (do some Smalltalk code) vs printIt (doIt + printing              the answer)

Page 124: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapMessages messagesmessagesagain messages....

124

Page 125: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Yes, there are only messages unary binary keywords

125

Page 126: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Composition: from left to right!

69 class inspect

69 class superclass superclass inspect

126

Page 127: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Unary> Binary> Keywords

127

Precedence

Page 128: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 + 3 squared

128

Page 129: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 + 3 squared> 2 + 9

129

Page 130: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 + 3 squared> 2 + 9> 11

130

Page 131: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Color gray - Color white = Color black

131

Page 132: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Color gray - Color white = Color black> aColor = Color black

132

Page 133: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Color gray - Color white = Color black> aColor = Color black> true

133

Page 134: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 raisedTo: 3 + 2

134

Page 135: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 raisedTo: 3 + 2> 2 raisedTo: 5

135

Page 136: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

2 raisedTo: 3 + 2> 2 raisedTo: 5> 32

136

Page 137: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1/3 + 2/3

137

No mathematical precedence

Page 138: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1/3 + 2/3>7/3 /3

138

No mathematical precedence

Page 139: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Parenthesized takes precedence!

139

(Msg) > Unary> Binary> Keywords

Page 140: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight

140

Page 141: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight

141

Page 142: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight> aRectangle bottomRight

142

Page 143: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight> aRectangle bottomRight> 100@100

143

Page 144: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

0@0 extent: 100@100 bottomRight

144

Page 145: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

0@0 extent: 100@100 bottomRight> Message not understood> 100 does not understand bottomRight

145

Page 146: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + 2 * 10

146

No mathematical precedence

Page 147: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + 2 * 10> 5 * 10

147

No mathematical precedence

Page 148: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + 2 * 10> 5 * 10> 50 argh!

148

No mathematical precedence

Page 149: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + (2 * 10)

149

No mathematical precedence

Page 150: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + (2 * 10)> 3 + 20

150

No mathematical precedence

Page 151: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

3 + (2 * 10)> 3 + 20> 23

151

No mathematical precedence

Page 152: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1/3 + 2/3 > 7/3 /3

152

No mathematical precedence

Page 153: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1/3 + 2/3 > (7/3) /3> 7/9

153

No mathematical precedence

Page 154: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1/3) + (2/3)

154

No mathematical precedence

Page 155: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(1/3) + (2/3)> 1

155

No mathematical precedence

Page 156: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(Msg) > Unary > Binary > Keywords from left to rightNo mathematical precedence

156

Only Messages

Page 157: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

(Msg) > Unary > Binary > Keywords from left to rightNo mathematical precedence

157

Only Messages

Page 158: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with blocks

158

Page 159: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Function definition

fct(x) = x * x + x

159

Page 160: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Function Application

fct (2) = 6fct (20) = 420

160

Page 161: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Function definition

fct(x) = x * x + x

|fct|fct:= [:x | x * x + x].

161

Page 162: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Function Application

fct (2) = 6fct (20) = 420

fct value: 2 > 6fct value: 20 > 420

162

Page 163: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Other examples

[2 + 3 + 4 + 5] value[:x | x + 3 + 4 + 5 ] value: 2[:x :y | x + y + 4 + 5] value: 2 value: 3

163

Page 164: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Block

anonymous function

[ :variable1 :variable2 | | tmp | expression1. ...variable1 ... ]

To execute the function, use the

    value: ... value: ...

message

164

Page 165: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Block

anonymous function — represented as an objectReally really cool!Can be passed to methods, stored in instance variables ...

[ :variable1 :variable2 | | tmp | expression1. ...variable1 ... ]

value: ... value: ...

165

Page 166: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with conditional

166

Page 167: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Example

3 > 0 ifTrue:['positive'] ifFalse:['negative']

167

Page 168: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Example

3 > 0 ifTrue:['positive'] ifFalse:['negative']

> 'positive'

168

Page 169: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Yes ifTrue:ifFalse: is a message!

Weather isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses]

ifTrue:ifFalse is sent to an object: a boolean!

169

Page 170: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Booleans

& | notor: and: (lazy)xor:ifTrue:ifFalse:ifFalse:ifTrue:...

170

Page 171: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Yes! ifTrue:ifFalse: is a message send to a Boolean.

But optimized by the compiler :)

171

Page 172: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 timesRepeat: [ Transcript show: 'hello'; cr]

172

Page 173: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

10 timesRepeat: [ Transcript show: 'hello'; cr]

173

Page 174: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

[x<y] whileTrue: [x := x + 3]

174

Page 175: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

aBlockTest whileTrueaBlockTest whileFalseaBlockTest whileTrue: aBlockBodyaBlockTest whileFalse: aBlockBodyanInteger timesRepeat: aBlockBody

175

Page 176: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Confused about () and [] ?

176

Page 177: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Use [ ] when you do not know the number of times something may be executed

(x isBlue) ifTrue: [ x schroumph ]

n timesRepeat: [ self shout ]

177

Page 178: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Control statement of other languages are replaced by messages sent to booleans:

(x isBlue) ifTrue: [ ]

178

Page 179: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

Fun with loops

179

Roadmap

Page 180: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 to: 100 do: [ :i | Transcript show: i ; space]

180

Page 181: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 to: 100 do: [ :i | Transcript show: i ; space]

181

Page 182: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 to: 100 by: 3 do: [ :i | Transcript show: i ; space]

182

Page 183: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

1 to: 100 by: 3 do: [ :i | Transcript show: i ; space]

183

Page 184: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

So: yes, there are real loops in Smalltalk!

to:do: to:by:do:

are just messages send to integers

184

Page 185: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

185

So: yes, there are real loops in Smalltalk!

to:do: to:by:do:

are just messages send to integers

Page 186: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

RoadmapFun with iterators

186

Page 187: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

ArrayList<String> strings = new ArrayList<String>(); for(Person person: persons) strings.add(person.name());

strings := persons collect [:person | person name].

187

Page 188: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(2 -3 4 -35 4) collect: [ :each| each abs]

188

Page 189: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(2 -3 4 -35 4) collect: [ :each| each abs]> #(2 3 4 35 4)

189

Page 190: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]

190

Page 191: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]> #(true false true false)

191

Page 192: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]

We can also do it this way:

|result|aCol := #( 2 -3 4 -35 4).result := aCol species new: aCol size.1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) odd].result

192

Page 193: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

193

#(15 10 19 68) collect: [:i | i odd ]

We can also do it this way:

|result|aCol := #( 2 -3 4 -35 4).result := aCol species new: aCol size.1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) odd].result

But don't!

Page 194: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) do: [:i | Transcript show: i ; cr ]

194

Page 195: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) do: [:i | Transcript show: i ; cr ]

195

Page 196: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(1 2 3) with: #(10 20 30) do: [:x :y| Transcript show: (y * x) ; cr ]

196

Page 197: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(1 2 3) with: #(10 20 30) do: [:x :y| Transcript show: (y * x) ; cr ]

197

Page 198: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

How is do: implemented?

198

Page 199: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

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

199

Page 200: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Some others... make them your friends

#(15 10 19 68) select: [:i|i odd]

#(15 10 19 68) reject: [:i|i odd]

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

200

Page 201: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd]

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

201

Some others... make them your friends

Page 202: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

202

Some others... make them your friends

Page 203: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd] > 19

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

203

Some others... make them your friends

Page 204: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd] > 19

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]> 1

204

Some others... make them your friends

Page 205: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Iterators are your best friends compact nice abstraction    puts the code with the data structure Just messages sent to collections

205

Page 206: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

206

Iterators are your best friends compact nice abstraction    puts the code with the data structure Just messages sent to collections

Page 207: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

A simple exercise

How do you write a method that does this?

#() ⧴ (nothing) #(a) ⧴ a #(a b c) ⧴ a, b, c

207

Page 208: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

#(a b c)do: [:each | Transcript show: each]separatedBy: [Transcript show: ', ']

208

Page 209: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

209

#(a b c)do: [:each | Transcript show: each]separatedBy: [Transcript show: ', ']

Page 210: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

More fun with Messages

210

Page 211: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

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

211

Page 212: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Multiple messages to one object ;

To send multiple messages to the same object, use ;

Transcript show: 3 printString; cr

is equivalent to:

Transcript show: 3 printString. Transcript cr

212

Page 213: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Hints ...

x isNil ifTrue: [...]

better:

x ifNil: [...]

213

Page 214: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

Hints ...

x includes: 3 ifTrue: [...]

is read as sending the message includes:ifTrue:

So: use parenthesis

(x includes: 3) ifTrue: [...]

214

Page 215: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

!"#$

215

Page 216: A Little Journey in the Smalltalk Syntaxweb.cecs.pdx.edu/~black/OOP/slides/funWithSmalltalkSyntax.pdfS.Ducasse!"#$ 1000 factorial 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799

S.Ducasse

Smalltalk is funPure, simple, powerful

www.seaside.stwww.dabbledb.comwww.pharo-project.org

216