Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax."— Presentation transcript:

1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ 1 A Little Journey in the Smalltalk Syntax

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

3 S.Ducasse 3 Appetizer!

4 S.Ducasse 4

5 5 Yeah! Smalltalk is a dynamically typed language

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

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

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

9 S.Ducasse 9 Roadmap Fun with numbers

10 S.Ducasse 10 1 class

11 S.Ducasse 11 1 class >SmallInteger

12 S.Ducasse 12 1 class maxVal

13 S.Ducasse 13 1 class maxVal >1073741823

14 S.Ducasse 14 1 class maxVal + 1

15 S.Ducasse 15 1 class maxVal + 1 >1073741824

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

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

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

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

20 S.Ducasse 20 2/3 + 1

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

22 S.Ducasse 22 1000 factorial

23 S.Ducasse 23 1000 factorial 40238726007709377354370243392300398571937486421071463254379991042993851239862902059204420848696 94048004799886101971960586316668729948085589013238296699445909974245040870737599188236277271887 32519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783 64784997701247663288983595573543251318532395846307555740911426241747434934755342864657661166779 73966688202912073791438537195882498081268678383745597317461360853795345242215865932019280908782 97308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426 23613141250878020800026168315102734182797770478463586817016436502415369139828126481021309276124 48963599287051149649754199093422215668325720808213331861168115536158365469840467089756029009505 37616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399 73628075013783761530712776192684903435262520001588853514733161170210396817592151090778801939317 81141945452572238655414610628921879602238389714760885062768629671466746975629112340824392081601 53780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745 99264295658174662830295557029902432415318161721046583203678690611726015878352075151628422554026 51704833042261439742869330616908979684825901254583271682264580665267699586526822728070757813918 58178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180 61213855860030143569452722420634463179746059468257310379008402443243846565724501440282188525247 09351906209290231364932734975655139587205596542287497740114133469627154228458623773875382304838 65688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193 89701786004081188972991831102117122984590164192106888438712185564612496079872290851929681937238 86426148396573822911231250241866493531439701374285319266498753372189406942814341185201580141233 44828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389 86355427719674282224875758676575234422020757363056949882508796892816275384886339690995982628095 61214509948717012445164612603790293091208890869420285106401821543994571568059418727489980942547 42173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004 15369010593398383577793941097002775347200000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000

24 S.Ducasse 24 1000 factorial / 999 factorial

25 S.Ducasse 25 1000 factorial / 999 factorial > 1000

26 S.Ducasse 26 10 @ 100

27 S.Ducasse 27 10 @ 100 (10 @ 100) x

28 S.Ducasse 28 10 @ 100 (10 @ 100) x > 10

29 S.Ducasse 29 10 @ 100 (10 @ 100) x > 10 (10 @ 100) y

30 S.Ducasse 30 10 @ 100 (10 @ 100) x > 10 (10 @ 100) y >100

31 S.Ducasse 31 Points! Points are created using @

32 S.Ducasse 32 Puzzle (10@100) + (20@100)

33 S.Ducasse 33 Puzzle (10@100) + (20@100) >30@200

34 S.Ducasse 34 Puzzle (10@100) + (20@100) >30@200

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

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

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

38 S.Ducasse 38 ‘Strings’ ‘Tiramisu’

39 S.Ducasse 39 Characters 12 printString > ’12’

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

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

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

43 S.Ducasse 43 A program! | str |

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

62 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: 10@120

63 S.Ducasse 63 Roadmap Fun with keywords-based messages

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

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

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

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

68 S.Ducasse 68 Removing unnecessary postman send mail recipient

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

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

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

72 S.Ducasse 72 Roadmap Fun with variables

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

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

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

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

77 S.Ducasse 77 Roadmap Fun with classes

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

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

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

81 S.Ducasse 81 Roadmap Fun with methods

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

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

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

85 S.Ducasse 85 Roadmap Fun with unary messages

86 S.Ducasse 86 1 class

87 S.Ducasse 87 1 class > SmallInteger

88 S.Ducasse 88 false not

89 S.Ducasse 89 false not > true

90 S.Ducasse 90 Date today

91 S.Ducasse 91 Date today > 24 May 2009

92 S.Ducasse 92 Time now

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

94 S.Ducasse 94 Float pi

95 S.Ducasse 95 Float pi > 3.141592653589793

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

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

98 S.Ducasse 98 Fun with binary messages Roadmap

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

100 S.Ducasse 100 1 + 2

101 S.Ducasse 101 1 + 2 >3

102 S.Ducasse 102 2 => 3

103 S.Ducasse 103 2 => 3 > false

104 S.Ducasse 104 10 @ 200

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

106 S.Ducasse 106 Roadmap Fun with keyword-based messages

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

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

109 S.Ducasse 109 10@20 setX: 2

110 S.Ducasse 110 10@20 setX: 2 > 2@20

111 S.Ducasse 111 12 between: 10 and: 20

112 S.Ducasse 112 12 between: 10 and: 20 > true

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

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

115 S.Ducasse 115 Browser newOnClass: Point Roadmap

116 S.Ducasse 116 Browser newOnClass: Point

117 S.Ducasse 117 Browser newOnClass: Point

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

119 S.Ducasse 119 Browser newOnClass: Point > a Browser

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

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

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

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

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

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

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

127 S.Ducasse 127 Unary> Binary> Keywords Precedence

128 S.Ducasse 128 2 + 3 squared

129 S.Ducasse 129 2 + 3 squared > 2 + 9

130 S.Ducasse 130 2 + 3 squared > 2 + 9 > 11

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

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

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

134 S.Ducasse 134 2 raisedTo: 3 + 2

135 S.Ducasse 135 2 raisedTo: 3 + 2 > 2 raisedTo: 5

136 S.Ducasse 136 2 raisedTo: 3 + 2 > 2 raisedTo: 5 > 32

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

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

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

140 S.Ducasse 140 (0@0 extent: 100@100) bottomRight

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

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

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

144 S.Ducasse 144 0@0 extent: 100@100 bottomRight

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

146 S.Ducasse 146 3 + 2 * 10 No mathematical precedence

147 S.Ducasse 147 3 + 2 * 10 > 5 * 10 No mathematical precedence

148 S.Ducasse 148 3 + 2 * 10 > 5 * 10 > 50 argh! No mathematical precedence

149 S.Ducasse 149 3 + (2 * 10) No mathematical precedence

150 S.Ducasse 150 3 + (2 * 10) > 3 + 20 No mathematical precedence

151 S.Ducasse 151 3 + (2 * 10) > 3 + 20 > 23 No mathematical precedence

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

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

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

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

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

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

158 S.Ducasse 158 Roadmap Fun with blocks

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

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

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

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

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

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

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

166 S.Ducasse 166 Roadmap Fun with conditional

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

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

169 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!

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

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

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

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

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

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

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

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

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

179 S.Ducasse 179 Fun with loops Roadmap

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

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

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

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

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

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

186 S.Ducasse 186 Roadmap Fun with iterators

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

188 S.Ducasse 188 #(2 -3 4 -35 4) collect: [ :each| each abs]

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

190 S.Ducasse 190 #(15 10 19 68) collect: [:i | i odd ]

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

192 S.Ducasse 192 #(15 10 19 68) collect: [:i | i odd ] We can also do it that 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

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

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

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

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

197 S.Ducasse 197 How do: is implemented?

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

199 S.Ducasse 199 Some others... 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 S.Ducasse 200 Some others... friends #(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 S.Ducasse 201 Some others... friends #(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 S.Ducasse 202 Some others... friends #(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 S.Ducasse 203 Some others... friends #(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 S.Ducasse 204 Iterators are your best friends compact nice abstraction Just messages sent to collections

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

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

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

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

209 S.Ducasse 209

210 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

211 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

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

213 S.Ducasse 213

214 S.Ducasse 214 Smalltalk is fun Pure simple powerful www.seaside.st (www.dabbledb.com) www.pharo-project.org


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

Similar presentations


Ads by Google