1
00:00:17,990 --> 00:00:21,870
all right so being absent-minded as I am

2
00:00:21,870 --> 00:00:23,190
I forgot to mention something very

3
00:00:23,190 --> 00:00:27,270
important last time which is first class

4
00:00:27,270 --> 00:00:30,060
attendance is not mandatory but second

5
00:00:30,060 --> 00:00:31,800
if I do remember you from class

6
00:00:31,800 --> 00:00:33,150
especially if you are see interesting

7
00:00:33,150 --> 00:00:35,520
questions and so on I will give you some

8
00:00:35,520 --> 00:00:37,080
bonus points when the final project

9
00:00:37,080 --> 00:00:39,180
comes right so that's my reward for

10
00:00:39,180 --> 00:00:42,840
class participation okay it's a slightly

11
00:00:42,840 --> 00:00:45,960
informal but I do remember people

12
00:00:45,960 --> 00:00:47,820
especially the faces names it's very

13
00:00:47,820 --> 00:00:52,620
hard for me but we'll see all right okay

14
00:00:52,620 --> 00:00:56,970
so let me give you some idea of what I

15
00:00:56,970 --> 00:01:00,300
want to do today right and I mean all

16
00:01:00,300 --> 00:01:01,890
these things are gonna be important for

17
00:01:01,890 --> 00:01:03,870
the rest of the class so I do want first

18
00:01:03,870 --> 00:01:05,580
to start with an introduction to Scala

19
00:01:05,580 --> 00:01:09,299
and I will really try to do two things

20
00:01:09,299 --> 00:01:11,850
one is give a broad introduction to

21
00:01:11,850 --> 00:01:13,710
Scala as a language I have no intention

22
00:01:13,710 --> 00:01:14,850
of going through all the features all

23
00:01:14,850 --> 00:01:15,900
the things I just want to get too

24
00:01:15,900 --> 00:01:17,280
excited about Scala why should you

25
00:01:17,280 --> 00:01:18,930
program its color in the first place and

26
00:01:18,930 --> 00:01:21,450
the second thing is I want to spend time

27
00:01:21,450 --> 00:01:23,909
on Scala actors which is the thing I

28
00:01:23,909 --> 00:01:25,979
mostly care about and it's the model

29
00:01:25,979 --> 00:01:29,369
your most unfamiliar with okay so at the

30
00:01:29,369 --> 00:01:31,350
end of today I want to to essentially

31
00:01:31,350 --> 00:01:33,630
have a decent starting point to start

32
00:01:33,630 --> 00:01:35,369
learning Scala as a language but a very

33
00:01:35,369 --> 00:01:36,630
good starting point from the point of

34
00:01:36,630 --> 00:01:38,310
view of what are the sectors and how can

35
00:01:38,310 --> 00:01:40,859
you use them okay

36
00:01:40,859 --> 00:01:43,590
I still want if we have time since we

37
00:01:43,590 --> 00:01:49,649
have two sessions today to wrap up the

38
00:01:49,649 --> 00:01:50,969
discussion about what distributed

39
00:01:50,969 --> 00:01:53,520
systems are all about right that's going

40
00:01:53,520 --> 00:01:56,100
to set the pace for what is it that

41
00:01:56,100 --> 00:01:57,719
we're going to see in this class all

42
00:01:57,719 --> 00:02:01,979
right so first Scala and in order to

43
00:02:01,979 --> 00:02:05,299
introduce Cal I'm gonna use slides from

44
00:02:05,299 --> 00:02:08,240
conference where scholar was presented

45
00:02:08,240 --> 00:02:11,340
Martin O'Dell ski is one of the course

46
00:02:11,340 --> 00:02:13,590
color developers and I mean he has the

47
00:02:13,590 --> 00:02:15,060
insight of what Scala is I have no

48
00:02:15,060 --> 00:02:16,230
intention to go through all the slides

49
00:02:16,230 --> 00:02:20,400
you already have in the Google Group

50
00:02:20,400 --> 00:02:21,990
that you are supposed to sign up for

51
00:02:21,990 --> 00:02:24,390
right it's on link from the class web

52
00:02:24,390 --> 00:02:27,030
page you already have an announcements a

53
00:02:27,030 --> 00:02:29,150
saying where the slides are

54
00:02:29,150 --> 00:02:33,750
right so let's go quickly but I'm gonna

55
00:02:33,750 --> 00:02:35,280
stop at the interesting points from the

56
00:02:35,280 --> 00:02:37,500
slides that Martin presented that at the

57
00:02:37,500 --> 00:02:39,330
conference to introduce Carla and gives

58
00:02:39,330 --> 00:02:41,040
a lot of the motivation of why border

59
00:02:41,040 --> 00:02:42,240
design another language you should

60
00:02:42,240 --> 00:02:44,130
always ask this question why do we need

61
00:02:44,130 --> 00:02:46,830
another language right lots of jokes on

62
00:02:46,830 --> 00:02:48,360
the Internet of what every language does

63
00:02:48,360 --> 00:02:49,560
and maybe it would be interesting to

64
00:02:49,560 --> 00:02:51,989
look at some jokes about Scala I'll try

65
00:02:51,989 --> 00:02:54,110
to dig up something until next time okay

66
00:02:54,110 --> 00:02:58,920
right so of course lots of lots of lots

67
00:02:58,920 --> 00:03:00,510
of different languages some of them are

68
00:03:00,510 --> 00:03:02,420
extremely special purpose like sequel

69
00:03:02,420 --> 00:03:04,769
I'm not going to talk much about sequel

70
00:03:04,769 --> 00:03:06,870
in in this class but I do talk a lot

71
00:03:06,870 --> 00:03:08,610
about sequel in database implementation

72
00:03:08,610 --> 00:03:12,450
tests for example right for example one

73
00:03:12,450 --> 00:03:13,860
interesting language that I'm excited

74
00:03:13,860 --> 00:03:17,370
about but for reasons you might not be

75
00:03:17,370 --> 00:03:21,660
obvious is the JavaScript why would I be

76
00:03:21,660 --> 00:03:23,160
excited about a language like JavaScript

77
00:03:23,160 --> 00:03:27,030
let's try to throw some reasons out well

78
00:03:27,030 --> 00:03:28,950
so actually as a language JavaScript is

79
00:03:28,950 --> 00:03:31,200
not fantastic Scala it's much much

80
00:03:31,200 --> 00:03:32,820
better I'm going to point out how many

81
00:03:32,820 --> 00:03:34,110
people have seen some snippets of

82
00:03:34,110 --> 00:03:37,079
JavaScript right well you should do some

83
00:03:37,079 --> 00:03:38,880
view source when you look at the webpage

84
00:03:38,880 --> 00:03:40,410
right and all of them have JavaScript

85
00:03:40,410 --> 00:03:42,000
now so the reason I'm excited about

86
00:03:42,000 --> 00:03:44,250
javascript really and this can point out

87
00:03:44,250 --> 00:03:46,170
reasons to be excited about languages is

88
00:03:46,170 --> 00:03:48,450
because it's supported very well by all

89
00:03:48,450 --> 00:03:50,610
the major browsers so if you want stuff

90
00:03:50,610 --> 00:03:53,070
to run in the browser you have to learn

91
00:03:53,070 --> 00:03:55,140
JavaScript has nothing to do how cool

92
00:03:55,140 --> 00:03:57,600
the language is or how exciting certain

93
00:03:57,600 --> 00:03:59,519
things are there are interesting things

94
00:03:59,519 --> 00:04:01,980
in the java language by the way java

95
00:04:01,980 --> 00:04:03,750
language was designed in a week all

96
00:04:03,750 --> 00:04:03,959
right

97
00:04:03,959 --> 00:04:05,790
they're interesting in the java language

98
00:04:05,790 --> 00:04:07,560
I'll point out some of them as we go for

99
00:04:07,560 --> 00:04:09,930
Scala for those of you that have seen

100
00:04:09,930 --> 00:04:11,579
JavaScript but sometimes you can be

101
00:04:11,579 --> 00:04:13,049
excited just because of the availability

102
00:04:13,049 --> 00:04:14,790
of the language that's not the case for

103
00:04:14,790 --> 00:04:17,070
Scala okay but Scala it's cool for many

104
00:04:17,070 --> 00:04:20,250
other reasons okay zoo of languages many

105
00:04:20,250 --> 00:04:23,370
many many such languages now to a large

106
00:04:23,370 --> 00:04:26,910
extent even beyond the actors Scala it's

107
00:04:26,910 --> 00:04:30,860
interesting because they finally the

108
00:04:30,860 --> 00:04:33,750
language community right the researchers

109
00:04:33,750 --> 00:04:36,060
in in in programming languages have done

110
00:04:36,060 --> 00:04:37,620
the right thing and it's just the

111
00:04:37,620 --> 00:04:39,060
perfect blend of all kinds of

112
00:04:39,060 --> 00:04:40,470
interesting stuff I'm gonna point out

113
00:04:40,470 --> 00:04:42,110
what they are

114
00:04:42,110 --> 00:04:46,530
20-watt Martin has in here it right you

115
00:04:46,530 --> 00:04:47,760
have all of this discussion about

116
00:04:47,760 --> 00:04:49,920
languages what's good and bad right each

117
00:04:49,920 --> 00:04:51,930
language has a sweet spot and an

118
00:04:51,930 --> 00:04:53,880
interesting question is could you design

119
00:04:53,880 --> 00:04:55,770
a language that has a large sweet spot

120
00:04:55,770 --> 00:04:57,450
and to some extent that's what Scala is

121
00:04:57,450 --> 00:05:01,440
all right now of course communicate

122
00:05:01,440 --> 00:05:03,000
between languages is really bad but

123
00:05:03,000 --> 00:05:06,120
Scala does some of that well with with

124
00:05:06,120 --> 00:05:11,910
Java okay okay now actually this puzzled

125
00:05:11,910 --> 00:05:13,530
me a little bit what do these guys mean

126
00:05:13,530 --> 00:05:15,210
by a scalable language I mean this is

127
00:05:15,210 --> 00:05:17,490
what they mean by Scala what they mean

128
00:05:17,490 --> 00:05:19,620
and you're gonna see tricks in how they

129
00:05:19,620 --> 00:05:21,540
do it and maybe Scala should be taught

130
00:05:21,540 --> 00:05:23,250
actually in a language class because

131
00:05:23,250 --> 00:05:24,720
it's a very good example of an extremely

132
00:05:24,720 --> 00:05:26,310
good design of a language so it's

133
00:05:26,310 --> 00:05:28,440
scalable in the sense that has enough

134
00:05:28,440 --> 00:05:30,060
flexibility in the syntax to allow you

135
00:05:30,060 --> 00:05:32,010
to invent your own sub variants of the

136
00:05:32,010 --> 00:05:34,350
language and that bothered me for for a

137
00:05:34,350 --> 00:05:36,030
while until I sit down to see exactly

138
00:05:36,030 --> 00:05:37,950
what that means are pointed out when we

139
00:05:37,950 --> 00:05:40,350
reach the right slide it turns out that

140
00:05:40,350 --> 00:05:43,140
if you relax a simple rule how can you

141
00:05:43,140 --> 00:05:47,190
call functions you can in fact get a

142
00:05:47,190 --> 00:05:48,960
very nice relaxation of the language

143
00:05:48,960 --> 00:05:51,540
that allows you to encode seemingly

144
00:05:51,540 --> 00:05:54,000
specific languages into a broad language

145
00:05:54,000 --> 00:05:56,160
okay I'll point that out specifically

146
00:05:56,160 --> 00:05:57,930
because some languages allow one way to

147
00:05:57,930 --> 00:05:59,280
call functions at the language is

148
00:05:59,280 --> 00:06:00,510
another way to call functions color

149
00:06:00,510 --> 00:06:02,340
allows both and sadly it feels much

150
00:06:02,340 --> 00:06:06,600
better okay so if I don't so let's go

151
00:06:06,600 --> 00:06:07,800
through the slides I'm sure I'll find an

152
00:06:07,800 --> 00:06:09,240
opportunity to show you that

153
00:06:09,240 --> 00:06:13,410
okay now Scala has many things going for

154
00:06:13,410 --> 00:06:18,290
it for example it can work as a

155
00:06:18,290 --> 00:06:20,970
scripting language so in fact it can be

156
00:06:20,970 --> 00:06:23,010
interpreted you're gonna play with it

157
00:06:23,010 --> 00:06:25,110
you're gonna download Scala and fire up

158
00:06:25,110 --> 00:06:26,640
the scale executable and literally you

159
00:06:26,640 --> 00:06:29,990
can type there directly Scala code and

160
00:06:29,990 --> 00:06:32,130
there is a big advantage with this kind

161
00:06:32,130 --> 00:06:34,140
of languages because you get instant

162
00:06:34,140 --> 00:06:35,490
feedback on what is it that you're

163
00:06:35,490 --> 00:06:36,960
actually doing and a lot of languages

164
00:06:36,960 --> 00:06:39,570
are like this nowadays right python is

165
00:06:39,570 --> 00:06:40,770
like this how many people programmed in

166
00:06:40,770 --> 00:06:43,290
Python right I mean Python tends to be

167
00:06:43,290 --> 00:06:47,190
like this right as an example right so

168
00:06:47,190 --> 00:06:48,630
for example you could do things like oh

169
00:06:48,630 --> 00:06:51,150
let's define a map us I'm gonna go

170
00:06:51,150 --> 00:06:52,410
through what these things mean in a

171
00:06:52,410 --> 00:06:55,260
second right a map that maps us to wash

172
00:06:55,260 --> 00:06:57,090
and friends to Perry so on and so forth

173
00:06:57,090 --> 00:06:59,040
right then we define some sort of a

174
00:06:59,040 --> 00:07:02,520
function and then this is you see

175
00:07:02,520 --> 00:07:03,900
operators in the language like plus

176
00:07:03,900 --> 00:07:05,250
equals seems to be some sort of an

177
00:07:05,250 --> 00:07:08,090
operator so what is this capital now

178
00:07:08,090 --> 00:07:10,320
it's some sort of a map we don't quite

179
00:07:10,320 --> 00:07:11,550
know what it is it's probably some sort

180
00:07:11,550 --> 00:07:14,730
of a dictionary right then this operator

181
00:07:14,730 --> 00:07:16,590
plus C code probably adds entries to the

182
00:07:16,590 --> 00:07:18,840
dictionary right so it seems that this

183
00:07:18,840 --> 00:07:21,780
language has dictionaries or maybe it's

184
00:07:21,780 --> 00:07:23,220
not in the language maybe in some sort

185
00:07:23,220 --> 00:07:25,680
of a library has operators that make

186
00:07:25,680 --> 00:07:28,920
your life easier right then we can call

187
00:07:28,920 --> 00:07:31,590
capital or friends so somehow capital is

188
00:07:31,590 --> 00:07:34,050
not only some sort of a map but some

189
00:07:34,050 --> 00:07:37,170
sort of a function as well so we can

190
00:07:37,170 --> 00:07:38,640
look things up in a natural way in the

191
00:07:38,640 --> 00:07:42,510
dictionary right and because we are

192
00:07:42,510 --> 00:07:43,890
running things in interpreter we get

193
00:07:43,890 --> 00:07:45,630
immediately the answer to this right so

194
00:07:45,630 --> 00:07:48,180
the answer to capital friends is a

195
00:07:48,180 --> 00:07:50,910
string Perris and notice something

196
00:07:50,910 --> 00:07:52,170
interesting that already has a name

197
00:07:52,170 --> 00:07:55,860
write rest seven so the interpreter

198
00:07:55,860 --> 00:07:57,600
already created a local variable for us

199
00:07:57,600 --> 00:08:00,390
and we can we can use it how many people

200
00:08:00,390 --> 00:08:04,140
program in something like I mean even an

201
00:08:04,140 --> 00:08:06,000
interactive session of MATLAB but

202
00:08:06,000 --> 00:08:08,970
definitely Mathematica right good

203
00:08:08,970 --> 00:08:10,350
mathematician here so in Mathematica

204
00:08:10,350 --> 00:08:11,670
Mathematica tends to be exactly in the

205
00:08:11,670 --> 00:08:14,400
same way right you start interactively

206
00:08:14,400 --> 00:08:16,260
type things define things whatever you

207
00:08:16,260 --> 00:08:18,870
do and all the intermediate results get

208
00:08:18,870 --> 00:08:21,120
named so you can go back and and refer

209
00:08:21,120 --> 00:08:22,470
to them it's very convenient

210
00:08:22,470 --> 00:08:25,650
now of course interpreted languages are

211
00:08:25,650 --> 00:08:28,080
nice scripting languages are nice but

212
00:08:28,080 --> 00:08:29,430
they have the tendency to be quite slow

213
00:08:29,430 --> 00:08:33,780
so thus color designers decided to in

214
00:08:33,780 --> 00:08:35,849
fact support both the compile mode and

215
00:08:35,849 --> 00:08:37,500
an interactive mode very few languages

216
00:08:37,500 --> 00:08:41,309
do this right okay and usually languages

217
00:08:41,309 --> 00:08:42,990
acquire some sort of compilation much

218
00:08:42,990 --> 00:08:45,060
later in their life and is never good

219
00:08:45,060 --> 00:08:48,150
right so for example MATLAB in my

220
00:08:48,150 --> 00:08:49,590
opinion doesn't have a particularly good

221
00:08:49,590 --> 00:08:52,260
way to compile things it wants to be

222
00:08:52,260 --> 00:08:54,030
interactive and then somehow they

223
00:08:54,030 --> 00:08:55,380
squeezed in some kind of a compiled

224
00:08:55,380 --> 00:08:57,570
version but it's not quite right okay it

225
00:08:57,570 --> 00:08:59,130
turns out that the scala designers

226
00:08:59,130 --> 00:09:01,320
wanted to make scholar compatible from

227
00:09:01,320 --> 00:09:04,140
the beginning compilable what exactly

228
00:09:04,140 --> 00:09:06,030
does it mean compiler in this

229
00:09:06,030 --> 00:09:07,650
circumstance in particular in general

230
00:09:07,650 --> 00:09:09,270
right

231
00:09:09,270 --> 00:09:11,250
means you already map it to some kind of

232
00:09:11,250 --> 00:09:12,810
a much more efficient representation

233
00:09:12,810 --> 00:09:15,480
that can be accepted much faster right

234
00:09:15,480 --> 00:09:17,550
now Scala designers and I give them a

235
00:09:17,550 --> 00:09:19,680
lot of credit for this right skaar

236
00:09:19,680 --> 00:09:21,630
designers took a really good decision

237
00:09:21,630 --> 00:09:24,690
when it comes to this okay they decided

238
00:09:24,690 --> 00:09:26,640
to piggyback Scala on top of Java

239
00:09:26,640 --> 00:09:28,440
so essentially what the Scarlet designer

240
00:09:28,440 --> 00:09:30,990
wanted to do is fix Java put a lot of

241
00:09:30,990 --> 00:09:32,970
cool stuff into a Java like language

242
00:09:32,970 --> 00:09:35,700
remove all the not so cool stuff compile

243
00:09:35,700 --> 00:09:37,530
against the bytecode that runs on top of

244
00:09:37,530 --> 00:09:39,030
the java virtual machine to get all the

245
00:09:39,030 --> 00:09:40,680
benefits that you get from such a

246
00:09:40,680 --> 00:09:43,860
virtualization technique right what's

247
00:09:43,860 --> 00:09:45,720
one of the big benefits all the tools

248
00:09:45,720 --> 00:09:47,790
that go from the java virtual machine to

249
00:09:47,790 --> 00:09:49,620
extremely efficient implementation using

250
00:09:49,620 --> 00:09:50,970
the just-in-time compiler or whatever

251
00:09:50,970 --> 00:09:52,730
else is there are readily available

252
00:09:52,730 --> 00:09:54,600
actually the connection between Scala

253
00:09:54,600 --> 00:09:56,310
and Java and this is important for you

254
00:09:56,310 --> 00:09:58,020
when you're going to do your project the

255
00:09:58,020 --> 00:09:59,190
interaction between Scala and Java is

256
00:09:59,190 --> 00:10:01,730
extremely tight you can call Java

257
00:10:01,730 --> 00:10:05,070
classes methods whatever from Scala and

258
00:10:05,070 --> 00:10:07,530
to with some effort the other way around

259
00:10:07,530 --> 00:10:09,840
as well okay so in a natural way you can

260
00:10:09,840 --> 00:10:12,450
import Java libraries in Scala and call

261
00:10:12,450 --> 00:10:16,020
Java methods and classes and instantiate

262
00:10:16,020 --> 00:10:18,210
things and so on and so forth so to some

263
00:10:18,210 --> 00:10:20,880
extent Java itself Java makes itself

264
00:10:20,880 --> 00:10:22,800
compacted I'm sorry Scala makes itself

265
00:10:22,800 --> 00:10:25,320
compatible with Java right now why would

266
00:10:25,320 --> 00:10:26,970
you do this why would you piggyback a

267
00:10:26,970 --> 00:10:29,180
language on top of another language

268
00:10:29,180 --> 00:10:31,830
it's the libraries right and I want you

269
00:10:31,830 --> 00:10:33,600
to understand this when you design a new

270
00:10:33,600 --> 00:10:35,280
language and it's very tempting to do so

271
00:10:35,280 --> 00:10:37,650
right when you design a new language you

272
00:10:37,650 --> 00:10:39,990
have a big problem on your hands the

273
00:10:39,990 --> 00:10:41,130
language is the language is the core

274
00:10:41,130 --> 00:10:43,290
language but there is so much more to

275
00:10:43,290 --> 00:10:45,270
language in particular all those useful

276
00:10:45,270 --> 00:10:47,160
tools all those libraries that can put

277
00:10:47,160 --> 00:10:50,240
things together right that allow you to

278
00:10:50,240 --> 00:10:53,400
do things in the language now the Scala

279
00:10:53,400 --> 00:10:55,230
people took a very nice shortcut that

280
00:10:55,230 --> 00:10:57,180
said hey let's just use all the Java

281
00:10:57,180 --> 00:10:57,870
stuff out there

282
00:10:57,870 --> 00:11:00,270
great they already brought the language

283
00:11:00,270 --> 00:11:03,060
from whatever some curiosity looks cute

284
00:11:03,060 --> 00:11:04,410
but it doesn't have anything interesting

285
00:11:04,410 --> 00:11:05,910
going for it too oh we can do everything

286
00:11:05,910 --> 00:11:07,650
we can do in Java and we can actually

287
00:11:07,650 --> 00:11:11,250
mix and match Java and and Scala okay

288
00:11:11,250 --> 00:11:13,680
you can form those dot class files which

289
00:11:13,680 --> 00:11:15,840
are the combined binary files either in

290
00:11:15,840 --> 00:11:17,340
Java or install and put them together in

291
00:11:17,340 --> 00:11:20,250
a big jar and run all of that okay so

292
00:11:20,250 --> 00:11:23,089
that was a really really good decision

293
00:11:23,089 --> 00:11:26,629
on the on the side of the scholar

294
00:11:26,629 --> 00:11:29,029
designers and now scholar people like to

295
00:11:29,029 --> 00:11:30,889
think about it as the future of Java

296
00:11:30,889 --> 00:11:33,290
okay all the things Java people want to

297
00:11:33,290 --> 00:11:34,730
insert in the language they already have

298
00:11:34,730 --> 00:11:36,470
done it in Scala but throughout all the

299
00:11:36,470 --> 00:11:38,509
ugly stuff I'll mention some of it oh by

300
00:11:38,509 --> 00:11:40,639
the way how many people here are have

301
00:11:40,639 --> 00:11:42,769
done significant work in Java

302
00:11:42,769 --> 00:11:45,019
significance in coded at list of

303
00:11:45,019 --> 00:11:46,279
thousand lines of code or something like

304
00:11:46,279 --> 00:11:48,529
that right okay

305
00:11:48,529 --> 00:11:51,439
so I'll make Java references for those

306
00:11:51,439 --> 00:11:53,019
people that bother to raise the hand

307
00:11:53,019 --> 00:11:58,370
okay now Java actually draws a lot of

308
00:11:58,370 --> 00:12:01,189
inspiration from a series of languages

309
00:12:01,189 --> 00:12:05,029
but more prominently from ML okay how

310
00:12:05,029 --> 00:12:07,579
many people heard about ml and

311
00:12:07,579 --> 00:12:10,990
especially I mean variants like okay Moe

312
00:12:10,990 --> 00:12:15,470
right so by the way ml stands for metal

313
00:12:15,470 --> 00:12:18,579
language and was the darling of the

314
00:12:18,579 --> 00:12:20,959
languages research community for a very

315
00:12:20,959 --> 00:12:23,149
long time for example when I was a

316
00:12:23,149 --> 00:12:25,819
student at Cornell right for the type

317
00:12:25,819 --> 00:12:27,769
theory class which was in fact some kind

318
00:12:27,769 --> 00:12:29,749
of a language class we have to do

319
00:12:29,749 --> 00:12:31,759
projects in ml and ml it's a really cool

320
00:12:31,759 --> 00:12:33,470
language but it has this problem that

321
00:12:33,470 --> 00:12:35,179
the libraries are not at the level of

322
00:12:35,179 --> 00:12:36,499
all the other libraries and it seems

323
00:12:36,499 --> 00:12:39,290
somewhat esoteric so what Scala

324
00:12:39,290 --> 00:12:42,110
designers did it took all the nice stuff

325
00:12:42,110 --> 00:12:44,209
from ml and mixed it up with a nice

326
00:12:44,209 --> 00:12:45,829
stuff from Java to get this kind of a

327
00:12:45,829 --> 00:12:47,660
mixed language and you're gonna see some

328
00:12:47,660 --> 00:12:49,459
of them and they are really really cool

329
00:12:49,459 --> 00:12:52,579
now one thing about Scala that I want

330
00:12:52,579 --> 00:12:53,870
you to appreciate from the beginning it

331
00:12:53,870 --> 00:12:55,999
says it's that it's a much more compact

332
00:12:55,999 --> 00:12:58,699
language than you are used to I mean it

333
00:12:58,699 --> 00:13:01,009
it almost gets to the compactness of

334
00:13:01,009 --> 00:13:02,329
some of the advanced scripting languages

335
00:13:02,329 --> 00:13:05,839
a very special purpose right so the fact

336
00:13:05,839 --> 00:13:06,980
that you see a program that's ten lines

337
00:13:06,980 --> 00:13:08,300
of code doesn't mean you don't need a

338
00:13:08,300 --> 00:13:10,129
little bit of time to understand it

339
00:13:10,129 --> 00:13:11,720
because of how contact the languages I'm

340
00:13:11,720 --> 00:13:14,059
gonna point out just how nicely Scala

341
00:13:14,059 --> 00:13:17,259
does some of these things okay

342
00:13:17,259 --> 00:13:19,730
that's important for you why because

343
00:13:19,730 --> 00:13:21,709
when you write your program a lot of

344
00:13:21,709 --> 00:13:22,699
your programs are going to fit on a

345
00:13:22,699 --> 00:13:24,620
single screen and it's gonna be I

346
00:13:24,620 --> 00:13:27,319
believe a shock to you to see how short

347
00:13:27,319 --> 00:13:29,809
some of these programs are gonna be I'm

348
00:13:29,809 --> 00:13:31,100
gonna ask you to do assignments and

349
00:13:31,100 --> 00:13:32,480
you're gonna end up with 40 lines of

350
00:13:32,480 --> 00:13:34,970
code for something that seems like

351
00:13:34,970 --> 00:13:36,360
impossible to do with

352
00:13:36,360 --> 00:13:38,670
but two thousand lines of code E&C right

353
00:13:38,670 --> 00:13:42,569
so this is my way of showing you the

354
00:13:42,569 --> 00:13:44,639
good life with the programming languages

355
00:13:44,639 --> 00:13:47,129
right so my purpose is not only to have

356
00:13:47,129 --> 00:13:48,899
something going on for this DCB systems

357
00:13:48,899 --> 00:13:49,170
class

358
00:13:49,170 --> 00:13:51,629
I truly believe at Scala is the language

359
00:13:51,629 --> 00:13:52,739
of the future now it's not good for

360
00:13:52,739 --> 00:13:54,329
everything but is good for an amazing

361
00:13:54,329 --> 00:13:55,920
number of things and I'll point out

362
00:13:55,920 --> 00:13:57,509
throughout the class and some of them

363
00:13:57,509 --> 00:14:01,910
today of why I believe that's true okay

364
00:14:01,910 --> 00:14:04,220
good so I'm not gonna go through this

365
00:14:04,220 --> 00:14:06,509
talks to Java in a negative way and

366
00:14:06,509 --> 00:14:08,429
whatever Java talks to Scala talks to

367
00:14:08,429 --> 00:14:10,699
through the Java interface in particular

368
00:14:10,699 --> 00:14:13,799
C right Java has a bridge to see native

369
00:14:13,799 --> 00:14:15,179
functions and whatnot Scala can

370
00:14:15,179 --> 00:14:17,759
piggyback on top of it okay performance

371
00:14:17,759 --> 00:14:20,369
on par with Java now to some extent I've

372
00:14:20,369 --> 00:14:22,199
seen numbers that show that the Scala

373
00:14:22,199 --> 00:14:23,819
performance is better and this is

374
00:14:23,819 --> 00:14:26,160
somewhat strange right

375
00:14:26,160 --> 00:14:28,889
it's essentially a side effect of the

376
00:14:28,889 --> 00:14:31,040
fact that you have more information

377
00:14:31,040 --> 00:14:33,209
because of the functional programming

378
00:14:33,209 --> 00:14:35,279
part that's permanent in Scala and

379
00:14:35,279 --> 00:14:37,259
that's more optimizable at least under

380
00:14:37,259 --> 00:14:40,949
certain circumstances okay okay

381
00:14:40,949 --> 00:14:43,919
now the and I I do want you to

382
00:14:43,919 --> 00:14:45,059
understand this from the beginning

383
00:14:45,059 --> 00:14:47,639
because once you understand this core

384
00:14:47,639 --> 00:14:49,019
concept it's easy to understand how the

385
00:14:49,019 --> 00:14:51,869
language sticks right so thus car

386
00:14:51,869 --> 00:14:53,999
designers like the ML designers are

387
00:14:53,999 --> 00:14:56,429
obsessed by cleanness of mechanisms they

388
00:14:56,429 --> 00:14:59,639
have very few mechanisms but are very

389
00:14:59,639 --> 00:15:01,889
these mechanisms are very very powerful

390
00:15:01,889 --> 00:15:03,749
right so Scala is very big on

391
00:15:03,749 --> 00:15:05,819
compositionality you can do smaller

392
00:15:05,819 --> 00:15:07,139
things and compose them in a clean way

393
00:15:07,139 --> 00:15:08,610
almost anything can be composed with

394
00:15:08,610 --> 00:15:10,199
anything so it's a language that doesn't

395
00:15:10,199 --> 00:15:11,669
really have too many exceptions so let

396
00:15:11,669 --> 00:15:13,290
me give you an idea of an accept of a

397
00:15:13,290 --> 00:15:16,799
breakage of the rule that is in the end

398
00:15:16,799 --> 00:15:18,509
the nightmare if you do large-scale Java

399
00:15:18,509 --> 00:15:22,049
programming is right in Java everything

400
00:15:22,049 --> 00:15:24,480
it's an object right except the basic

401
00:15:24,480 --> 00:15:28,199
types so ends doubles the native types

402
00:15:28,199 --> 00:15:30,149
are not objects they don't behave like

403
00:15:30,149 --> 00:15:32,189
objects they have their own set of rules

404
00:15:32,189 --> 00:15:33,839
and they create their own set of math

405
00:15:33,839 --> 00:15:36,540
right in Scala those things are objects

406
00:15:36,540 --> 00:15:38,610
as well why not make them objects why

407
00:15:38,610 --> 00:15:40,439
not treat them like native things now

408
00:15:40,439 --> 00:15:42,720
you deal with any issues related to

409
00:15:42,720 --> 00:15:44,970
performance when you compile but not in

410
00:15:44,970 --> 00:15:46,499
the semantics you exposed to the user

411
00:15:46,499 --> 00:15:49,710
and that's really the mantra in Scala ok

412
00:15:49,710 --> 00:15:53,279
so a lot of the things that are very

413
00:15:53,279 --> 00:15:55,350
cumbersome in Java they become very

414
00:15:55,350 --> 00:15:56,970
straightforward in Scala and to some

415
00:15:56,970 --> 00:15:57,990
extent a lot of things that are

416
00:15:57,990 --> 00:15:59,520
cumbersome the other languages like C++

417
00:15:59,520 --> 00:16:03,960
and whatnot right right well we'll go

418
00:16:03,960 --> 00:16:05,250
through this but one prominent example

419
00:16:05,250 --> 00:16:07,860
is this idea of a trade oh by the way

420
00:16:07,860 --> 00:16:09,540
the trades make it into all the modern

421
00:16:09,540 --> 00:16:11,730
languages C++ started to have trade Java

422
00:16:11,730 --> 00:16:13,529
wants to have trades trades are

423
00:16:13,529 --> 00:16:16,800
basically characteristics that are parts

424
00:16:16,800 --> 00:16:18,420
of an interface right so you remember

425
00:16:18,420 --> 00:16:19,709
from Java you can define an interface

426
00:16:19,709 --> 00:16:21,779
and then an implementation of the

427
00:16:21,779 --> 00:16:24,089
interface right and then you somehow can

428
00:16:24,089 --> 00:16:25,589
inherit from interface is not clear why

429
00:16:25,589 --> 00:16:27,149
that is these guys cleaned it up they

430
00:16:27,149 --> 00:16:29,550
say it straight right to a trade can be

431
00:16:29,550 --> 00:16:32,940
oh you have something like an output

432
00:16:32,940 --> 00:16:35,490
output medium or some kind of a property

433
00:16:35,490 --> 00:16:37,800
and you can say hey this class what I

434
00:16:37,800 --> 00:16:38,970
want to define has this straight and

435
00:16:38,970 --> 00:16:40,110
this straight and this straight and I

436
00:16:40,110 --> 00:16:42,720
think my iPad is really annoyed okay

437
00:16:42,720 --> 00:16:43,800
sorry about this

438
00:16:43,800 --> 00:16:46,380
I have to remember to keep on doing

439
00:16:46,380 --> 00:16:47,850
something with the screen right so

440
00:16:47,850 --> 00:16:50,610
trades a very clean way and they have a

441
00:16:50,610 --> 00:16:52,470
nice way to mix up traits it's called

442
00:16:52,470 --> 00:16:54,779
mixing types you say a little bit of

443
00:16:54,779 --> 00:16:56,790
this this this and this and then inherit

444
00:16:56,790 --> 00:16:57,839
this from the class and that's how

445
00:16:57,839 --> 00:16:59,520
implementing is a much much more cleaner

446
00:16:59,520 --> 00:17:02,400
interest than Java so very few rules

447
00:17:02,400 --> 00:17:07,380
very powerful highly composable okay all

448
00:17:07,380 --> 00:17:10,020
right so this might suggest and these

449
00:17:10,020 --> 00:17:11,429
guys really want to run away from this

450
00:17:11,429 --> 00:17:13,439
that Java is some kind of a kitchen sink

451
00:17:13,439 --> 00:17:16,530
language now how many people heard about

452
00:17:16,530 --> 00:17:21,329
ADA how many people know the history of

453
00:17:21,329 --> 00:17:23,040
ADA why am i mentioning a that suddenly

454
00:17:23,040 --> 00:17:24,480
why why am i jumping up in now because

455
00:17:24,480 --> 00:17:27,540
ADA is a kitchen sink language okay so

456
00:17:27,540 --> 00:17:29,520
the story goes like this right in the in

457
00:17:29,520 --> 00:17:31,740
about 1980 Department of Defense

458
00:17:31,740 --> 00:17:34,050
realized that they are using about 400

459
00:17:34,050 --> 00:17:36,210
languages and I said hey let's design

460
00:17:36,210 --> 00:17:39,380
one language to rule them all okay and

461
00:17:39,380 --> 00:17:41,760
they form the big committee and the

462
00:17:41,760 --> 00:17:43,020
committee came up with a set of features

463
00:17:43,020 --> 00:17:45,330
that everybody inside the Department of

464
00:17:45,330 --> 00:17:46,590
Defense would have liked using a

465
00:17:46,590 --> 00:17:48,960
language and a language specification

466
00:17:48,960 --> 00:17:51,900
they called it ADA after Ada Lovelace

467
00:17:51,900 --> 00:17:54,809
which was the basically the first

468
00:17:54,809 --> 00:17:57,780
programmer she wrote the first program

469
00:17:57,780 --> 00:17:59,820
for Charles Babbage analytical engine

470
00:17:59,820 --> 00:18:02,640
right which was never really finished if

471
00:18:02,640 --> 00:18:03,730
the first

472
00:18:03,730 --> 00:18:08,740
non executed program okay so okay fine

473
00:18:08,740 --> 00:18:10,780
big committee formed the language the

474
00:18:10,780 --> 00:18:12,460
language specification was so

475
00:18:12,460 --> 00:18:14,470
complicated the first full

476
00:18:14,470 --> 00:18:16,510
implementation of EDI popped up only 15

477
00:18:16,510 --> 00:18:20,710
years later okay in 95 right now that a

478
00:18:20,710 --> 00:18:23,200
kitchen sink language it had everything

479
00:18:23,200 --> 00:18:25,600
that anybody wanted but it was so late

480
00:18:25,600 --> 00:18:27,190
that nobody is really using ADA for

481
00:18:27,190 --> 00:18:29,410
anything so Scarlett's trying not to be

482
00:18:29,410 --> 00:18:32,200
that but by relaxing that function call

483
00:18:32,200 --> 00:18:34,420
it can simulate that he can do some of

484
00:18:34,420 --> 00:18:36,220
the kitchen sink so even though in the

485
00:18:36,220 --> 00:18:37,480
end the roots are very simple so

486
00:18:37,480 --> 00:18:39,400
remember when you see a scallop program

487
00:18:39,400 --> 00:18:42,460
things are in fact basic mechanisms

488
00:18:42,460 --> 00:18:44,950
composed in a very clean way so it's

489
00:18:44,950 --> 00:18:46,570
always clean you just have to look for

490
00:18:46,570 --> 00:18:48,130
that cleaners once you get the cleaners

491
00:18:48,130 --> 00:18:51,040
the language is relatively easy right so

492
00:18:51,040 --> 00:18:54,190
broad comparison with Scala right you

493
00:18:54,190 --> 00:18:56,530
have a pure object system no more basic

494
00:18:56,530 --> 00:18:59,560
types that are special you have operator

495
00:18:59,560 --> 00:19:02,980
overloading right in a very very nice we

496
00:19:02,980 --> 00:19:04,300
actually operators of duty like

497
00:19:04,300 --> 00:19:05,770
functions and functions like operators

498
00:19:05,770 --> 00:19:07,210
this is really the relaxation I may have

499
00:19:07,210 --> 00:19:09,880
mentioned which if you think about it in

500
00:19:09,880 --> 00:19:11,380
makes sense right why would you treat

501
00:19:11,380 --> 00:19:13,780
the operators in a special way because

502
00:19:13,780 --> 00:19:17,020
C++ decided to do so C++ has a mess with

503
00:19:17,020 --> 00:19:19,900
the operators right so Scala tries to

504
00:19:19,900 --> 00:19:22,420
straighten it up and says there are all

505
00:19:22,420 --> 00:19:24,760
functions operator Frank they're all

506
00:19:24,760 --> 00:19:26,680
functions and you can call a function I

507
00:19:26,680 --> 00:19:28,180
can operate an operator like function

508
00:19:28,180 --> 00:19:30,250
but it makes sense to call a function

509
00:19:30,250 --> 00:19:32,740
like an operator only essentially if it

510
00:19:32,740 --> 00:19:34,510
has two arguments because otherwise it

511
00:19:34,510 --> 00:19:35,950
doesn't quite look like an operator so a

512
00:19:35,950 --> 00:19:37,300
function with two arguments can call

513
00:19:37,300 --> 00:19:39,040
kimiko like an operator one or two

514
00:19:39,040 --> 00:19:41,440
functions it's either the the unary or

515
00:19:41,440 --> 00:19:43,840
binary version of it and if it has more

516
00:19:43,840 --> 00:19:45,730
than two arguments then you have to use

517
00:19:45,730 --> 00:19:48,580
the normal function call okay it's

518
00:19:48,580 --> 00:19:51,550
optional to use the infix notation for

519
00:19:51,550 --> 00:19:53,680
for a function alright but you can use

520
00:19:53,680 --> 00:19:55,570
it and that can make actually things a

521
00:19:55,570 --> 00:19:58,230
lot more legible it's interesting okay

522
00:19:58,230 --> 00:20:00,000
closures

523
00:20:00,000 --> 00:20:02,620
so closures are all over the place in

524
00:20:02,620 --> 00:20:05,440
JavaScript right we have to come back to

525
00:20:05,440 --> 00:20:07,570
these things right anybody has any idea

526
00:20:07,570 --> 00:20:10,230
what closures are

527
00:20:13,399 --> 00:20:17,759
right so I got here one possible answer

528
00:20:17,759 --> 00:20:20,549
which is an anonymous function now by

529
00:20:20,549 --> 00:20:21,719
the way anonymous functions are called

530
00:20:21,719 --> 00:20:23,489
lambda functions right

531
00:20:23,489 --> 00:20:24,809
usually in the language that's because

532
00:20:24,809 --> 00:20:26,820
of the language people by the way the

533
00:20:26,820 --> 00:20:28,320
lambda functions were introduced far

534
00:20:28,320 --> 00:20:30,629
before any programming language was

535
00:20:30,629 --> 00:20:31,919
introduced why am I telling you all

536
00:20:31,919 --> 00:20:32,999
these things because everything is

537
00:20:32,999 --> 00:20:34,919
connected with everything right then the

538
00:20:34,919 --> 00:20:37,249
functions are actually a competing

539
00:20:37,249 --> 00:20:39,779
Universal model that competes with the

540
00:20:39,779 --> 00:20:41,899
Turing machines right lambda calculus

541
00:20:41,899 --> 00:20:44,820
was introduced by church in the 30s as

542
00:20:44,820 --> 00:20:46,979
an alternative to something that can

543
00:20:46,979 --> 00:20:49,139
compute anything right so the land the

544
00:20:49,139 --> 00:20:50,849
idea of a lambda something was

545
00:20:50,849 --> 00:20:52,619
introduced well before programming

546
00:20:52,619 --> 00:20:55,499
languages were fashionable right it was

547
00:20:55,499 --> 00:20:57,299
in in fact a universal programming

548
00:20:57,299 --> 00:20:59,269
language but by the way it's extremely

549
00:20:59,269 --> 00:21:01,739
cumbersome and to a logic said you can

550
00:21:01,739 --> 00:21:03,149
think of it as extremely crappy but you

551
00:21:03,149 --> 00:21:04,249
can do everything

552
00:21:04,249 --> 00:21:07,619
it's much worse than curing machine a

553
00:21:07,619 --> 00:21:09,210
Turing machine at least has something

554
00:21:09,210 --> 00:21:11,820
that moves right the lambda calculus has

555
00:21:11,820 --> 00:21:15,299
nothing except X and lambda but it can

556
00:21:15,299 --> 00:21:16,440
do everything right

557
00:21:16,440 --> 00:21:18,659
so closures are really more powerful

558
00:21:18,659 --> 00:21:20,609
than lambda functions I'm not only

559
00:21:20,609 --> 00:21:22,440
anonymous functions I'm going to come

560
00:21:22,440 --> 00:21:24,029
back to this but they can actually

561
00:21:24,029 --> 00:21:26,700
access variables in the surroundings

562
00:21:26,700 --> 00:21:29,639
where they are defined so some sort of a

563
00:21:29,639 --> 00:21:33,599
partial function that uses as global

564
00:21:33,599 --> 00:21:35,849
variables the local variables in the

565
00:21:35,849 --> 00:21:37,979
environment in which is embedded so this

566
00:21:37,979 --> 00:21:39,749
actually gives you some super super cool

567
00:21:39,749 --> 00:21:41,639
stuff you can do and this is why

568
00:21:41,639 --> 00:21:43,349
JavaScript programming looks crazy on

569
00:21:43,349 --> 00:21:44,999
portions because it makes use of these

570
00:21:44,999 --> 00:21:47,999
closures all over the place all right so

571
00:21:47,999 --> 00:21:50,339
we're going to have some examples now by

572
00:21:50,339 --> 00:21:52,169
the way all of these abstractions have

573
00:21:52,169 --> 00:21:54,749
to be treated with care any abstraction

574
00:21:54,749 --> 00:21:56,759
if abused is going to produce impossible

575
00:21:56,759 --> 00:21:58,320
to read code I want you to understand

576
00:21:58,320 --> 00:21:59,219
that doesn't matter how clean the

577
00:21:59,219 --> 00:22:01,409
language is you can always go nuts and

578
00:22:01,409 --> 00:22:03,330
produce something that's that can't be

579
00:22:03,330 --> 00:22:05,759
deciphered some that some languages

580
00:22:05,759 --> 00:22:07,049
produce in a much more natural way

581
00:22:07,049 --> 00:22:10,169
impossible to read code like Perl right

582
00:22:10,169 --> 00:22:11,639
so one of the jokes I found on the

583
00:22:11,639 --> 00:22:14,849
internet was why there are no obfuscated

584
00:22:14,849 --> 00:22:18,779
code competitions in Perl because

585
00:22:18,779 --> 00:22:20,700
everybody would win it's just a cryptic

586
00:22:20,700 --> 00:22:23,369
language right this crazy language Scala

587
00:22:23,369 --> 00:22:25,149
tries to be the opposite but try not to

588
00:22:25,149 --> 00:22:28,719
use the abstractions ok ok the mix in

589
00:22:28,719 --> 00:22:30,849
this replaces that inheritance right

590
00:22:30,849 --> 00:22:33,580
mixing means take these traits and all

591
00:22:33,580 --> 00:22:35,139
of that those characteristics to this

592
00:22:35,139 --> 00:22:36,580
class right makes a lot more sense

593
00:22:36,580 --> 00:22:38,529
because it's a way to compose things as

594
00:22:38,529 --> 00:22:43,229
opposed to some mysterious thing right

595
00:22:43,619 --> 00:22:45,789
existential types we're gonna see later

596
00:22:45,789 --> 00:22:49,779
abstract types right Oh what exactly are

597
00:22:49,779 --> 00:22:52,179
these abstract types why would you do

598
00:22:52,179 --> 00:22:53,619
abstract act now in general what you

599
00:22:53,619 --> 00:22:55,419
want to have in a language is some sort

600
00:22:55,419 --> 00:22:56,919
of templating because it tends to be

601
00:22:56,919 --> 00:22:59,559
very powerful you want to define for

602
00:22:59,559 --> 00:23:01,029
example the notion of a list or a map

603
00:23:01,029 --> 00:23:02,830
we've seen the map you want to define a

604
00:23:02,830 --> 00:23:05,229
map let's say from some kind of a key

605
00:23:05,229 --> 00:23:07,479
that's a string to values but you want

606
00:23:07,479 --> 00:23:08,950
the values to be very generic if

607
00:23:08,950 --> 00:23:11,289
possible right now you can do that in

608
00:23:11,289 --> 00:23:12,969
very inefficient ways for example having

609
00:23:12,969 --> 00:23:14,619
a generic object and then you can link

610
00:23:14,619 --> 00:23:16,479
to anything but would be nice to have

611
00:23:16,479 --> 00:23:18,489
so-called type 2 dictionaries that

612
00:23:18,489 --> 00:23:20,769
always go let's say from a string to a

613
00:23:20,769 --> 00:23:22,539
specific type now how would you define

614
00:23:22,539 --> 00:23:24,849
that well you can always do your own

615
00:23:24,849 --> 00:23:26,440
definition for every type you introduce

616
00:23:26,440 --> 00:23:27,759
about that's insane so this is why

617
00:23:27,759 --> 00:23:29,950
templates were introduced in C++ to have

618
00:23:29,950 --> 00:23:32,320
some generic mechanism to say hey use

619
00:23:32,320 --> 00:23:33,909
this code by replace this tied by this

620
00:23:33,909 --> 00:23:36,249
specific instance of it and then figure

621
00:23:36,249 --> 00:23:37,059
out the rest

622
00:23:37,059 --> 00:23:39,279
right Scala has a built into the

623
00:23:39,279 --> 00:23:42,039
language in a much much nicer way than

624
00:23:42,039 --> 00:23:45,279
let's say C++ has write the C++ template

625
00:23:45,279 --> 00:23:49,599
mechanism it's just insane right I mean

626
00:23:49,599 --> 00:23:51,969
after 20 years of programming in C++ I

627
00:23:51,969 --> 00:23:56,349
still hate it it's ok but scarless much

628
00:23:56,349 --> 00:23:57,700
cleaner we're going to see some examples

629
00:23:57,700 --> 00:24:00,279
ok pattern magic

630
00:24:00,279 --> 00:24:04,659
okay now pattern matching itself it's

631
00:24:04,659 --> 00:24:07,330
worth examples to follow but pattern

632
00:24:07,330 --> 00:24:09,249
matching itself it's worth switching to

633
00:24:09,249 --> 00:24:11,200
a language like this right once you get

634
00:24:11,200 --> 00:24:13,539
used to it there's nothing like it right

635
00:24:13,539 --> 00:24:17,080
now ever matching in Scala we're gonna

636
00:24:17,080 --> 00:24:18,729
see exactly what this means but pattern

637
00:24:18,729 --> 00:24:21,820
matching is color comes from ML data

638
00:24:21,820 --> 00:24:23,679
matching in ml comes from languages like

639
00:24:23,679 --> 00:24:24,789
Prolog how many of you heard about

640
00:24:24,789 --> 00:24:28,719
Prolog so what's this Prolog thing it's

641
00:24:28,719 --> 00:24:30,759
some sort of logical programming

642
00:24:30,759 --> 00:24:34,989
language well not quite ok it does so

643
00:24:34,989 --> 00:24:37,089
much more than that but in principle

644
00:24:37,089 --> 00:24:38,770
it's the first language learning

645
00:24:38,770 --> 00:24:40,240
reduce on a large scale this idea that

646
00:24:40,240 --> 00:24:43,000
you have patterns structures you say if

647
00:24:43,000 --> 00:24:44,890
the structure is kind of like this do

648
00:24:44,890 --> 00:24:47,500
this right and that's inherited with a

649
00:24:47,500 --> 00:24:50,770
nicer syntax through ml in Scala and it

650
00:24:50,770 --> 00:24:55,140
makes the code extremely legible right

651
00:24:55,140 --> 00:24:57,640
so I would strongly encourage you to

652
00:24:57,640 --> 00:24:59,140
take advantage of things like this in

653
00:24:59,140 --> 00:25:00,850
the language because the code is much

654
00:25:00,850 --> 00:25:03,850
much much better to look at to debug or

655
00:25:03,850 --> 00:25:05,350
there's almost no debugging actually to

656
00:25:05,350 --> 00:25:10,720
be done okay all right so Martin has

657
00:25:10,720 --> 00:25:13,380
cheat sheet here

658
00:25:13,380 --> 00:25:15,970
Java is like this Scala it's like this

659
00:25:15,970 --> 00:25:18,030
now let me let me mention something

660
00:25:18,030 --> 00:25:21,430
important and that already makes it much

661
00:25:21,430 --> 00:25:24,070
easier to read scholar programs types in

662
00:25:24,070 --> 00:25:26,080
a language like C or C++ come in front

663
00:25:26,080 --> 00:25:27,850
of the object like you have int function

664
00:25:27,850 --> 00:25:34,360
int of X well this color so I do want

665
00:25:34,360 --> 00:25:35,980
you to understand this is why I'm

666
00:25:35,980 --> 00:25:37,240
explaining so much connection with other

667
00:25:37,240 --> 00:25:38,890
languages I do want you to understand

668
00:25:38,890 --> 00:25:40,810
that nothing is created in a vacuum and

669
00:25:40,810 --> 00:25:42,970
people always have a certain inspiration

670
00:25:42,970 --> 00:25:47,170
and they set certain standards so ML had

671
00:25:47,170 --> 00:25:50,170
a really cool way to deal with the types

672
00:25:50,170 --> 00:25:52,480
not only had a very nice way to express

673
00:25:52,480 --> 00:25:54,610
the types that makes more sense than

674
00:25:54,610 --> 00:25:57,700
than the C way the C way it's insane in

675
00:25:57,700 --> 00:25:58,960
itself especially when you have pointers

676
00:25:58,960 --> 00:26:00,190
and reference whatever it's involved

677
00:26:00,190 --> 00:26:02,890
right C and C++ way so ML had a much

678
00:26:02,890 --> 00:26:04,870
nicer way to express types but had

679
00:26:04,870 --> 00:26:06,670
something much more interesting than

680
00:26:06,670 --> 00:26:08,760
that something called type inference

681
00:26:08,760 --> 00:26:11,020
anybody has any idea what type inference

682
00:26:11,020 --> 00:26:13,230
is

683
00:26:17,510 --> 00:26:21,730
right so allows so types are annoying

684
00:26:21,730 --> 00:26:24,650
okay how many of you have written for

685
00:26:24,650 --> 00:26:26,450
example a C++ program in which you try

686
00:26:26,450 --> 00:26:28,730
to use a library right and use one of

687
00:26:28,730 --> 00:26:31,520
those STL types in which the template

688
00:26:31,520 --> 00:26:33,350
argument of the STL type had five

689
00:26:33,350 --> 00:26:35,510
arguments and the name of the type was

690
00:26:35,510 --> 00:26:38,840
about yay big right and every time you

691
00:26:38,840 --> 00:26:40,640
had to go look back in your code to see

692
00:26:40,640 --> 00:26:43,010
what that type was to the point that at

693
00:26:43,010 --> 00:26:44,240
some point you said hey you know what

694
00:26:44,240 --> 00:26:46,190
let's use that nice facility and C++ to

695
00:26:46,190 --> 00:26:48,380
say type def this type and give you some

696
00:26:48,380 --> 00:26:51,530
same name because I'm tired of it right

697
00:26:51,530 --> 00:26:54,350
by the way C++ is getting type inference

698
00:26:54,350 --> 00:26:57,679
features already has some of them and to

699
00:26:57,679 --> 00:27:00,679
some extent as a programmer you really

700
00:27:00,679 --> 00:27:02,390
feel that the compiler should know what

701
00:27:02,390 --> 00:27:03,860
the type of that is I mean you see me

702
00:27:03,860 --> 00:27:05,360
using it how comes you don't know the

703
00:27:05,360 --> 00:27:06,860
type I mean what could I be talking

704
00:27:06,860 --> 00:27:08,090
about and this is what the type

705
00:27:08,090 --> 00:27:10,400
inference is all about right allows you

706
00:27:10,400 --> 00:27:12,470
to say yeah whatever you deal with the

707
00:27:12,470 --> 00:27:14,660
type and it works in most circumstances

708
00:27:14,660 --> 00:27:17,240
and it's a really powerful feature right

709
00:27:17,240 --> 00:27:20,030
it works in most circumstances there are

710
00:27:20,030 --> 00:27:21,530
some exceptions for example recursive

711
00:27:21,530 --> 00:27:22,700
functions it's very hard to figure out

712
00:27:22,700 --> 00:27:24,110
what the type of a recursive function is

713
00:27:24,110 --> 00:27:25,940
because the type inference mechanism can

714
00:27:25,940 --> 00:27:27,590
go in an infinite loop when it runs the

715
00:27:27,590 --> 00:27:28,850
algorithm that's one of the reasons

716
00:27:28,850 --> 00:27:31,700
right so the language designers did in

717
00:27:31,700 --> 00:27:35,720
Scala borrowing from the ML mechanisms

718
00:27:35,720 --> 00:27:38,720
right essentially have a very powerful

719
00:27:38,720 --> 00:27:40,760
type inference which pretty much means

720
00:27:40,760 --> 00:27:43,190
that for the most part you can omit the

721
00:27:43,190 --> 00:27:45,070
type but then you have a syntax problem

722
00:27:45,070 --> 00:27:49,250
right if the type is in front then how

723
00:27:49,250 --> 00:27:50,870
would you define a variable for which

724
00:27:50,870 --> 00:27:52,460
you don't want to say what the type is

725
00:27:52,460 --> 00:27:53,510
and you want to let the type inference

726
00:27:53,510 --> 00:27:56,809
mechanism figure it out oops then you

727
00:27:56,809 --> 00:27:59,750
have another problem okay and the other

728
00:27:59,750 --> 00:28:01,730
problem is the fact that you would like

729
00:28:01,730 --> 00:28:05,030
a natural way to specify whether a value

730
00:28:05,030 --> 00:28:09,320
is constant or non constant right I mean

731
00:28:09,320 --> 00:28:10,520
first of all what would you have

732
00:28:10,520 --> 00:28:13,910
constant values because they definitely

733
00:28:13,910 --> 00:28:16,720
make the the compiler do a better job

734
00:28:16,720 --> 00:28:19,669
okay but it's broader than that okay

735
00:28:19,669 --> 00:28:21,470
in general knowing that something

736
00:28:21,470 --> 00:28:23,169
doesn't change makes it much much easier

737
00:28:23,169 --> 00:28:27,620
to allow different things different

738
00:28:27,620 --> 00:28:29,750
threads to use it there is no danger

739
00:28:29,750 --> 00:28:30,690
that's going to be any

740
00:28:30,690 --> 00:28:34,530
like because it's read-only right so one

741
00:28:34,530 --> 00:28:38,160
of the desire goals in Scala was make

742
00:28:38,160 --> 00:28:40,170
sure we can indicate whether a certain

743
00:28:40,170 --> 00:28:44,010
value is constant or not okay so they

744
00:28:44,010 --> 00:28:47,340
did this by a bit first of all the type

745
00:28:47,340 --> 00:28:49,710
is specified exactly if you don't want

746
00:28:49,710 --> 00:28:50,910
to use the type inference mechanism the

747
00:28:50,910 --> 00:28:53,400
type is specified like in ml namely a

748
00:28:53,400 --> 00:28:56,460
variable : and then a type specifier

749
00:28:56,460 --> 00:28:59,100
as opposed to put it in front and you

750
00:28:59,100 --> 00:29:03,990
can actually use two ways to specify a

751
00:29:03,990 --> 00:29:08,280
variable either var or Val Val means

752
00:29:08,280 --> 00:29:11,670
it's a constant value immutable we are

753
00:29:11,670 --> 00:29:13,770
going to come back to this VAR means

754
00:29:13,770 --> 00:29:15,870
it's a true variable you can change the

755
00:29:15,870 --> 00:29:17,220
value and if you change the value

756
00:29:17,220 --> 00:29:18,810
somebody else can see that you change

757
00:29:18,810 --> 00:29:20,940
the value now obviously if you do some

758
00:29:20,940 --> 00:29:22,620
kind of a multi-threaded programming a

759
00:29:22,620 --> 00:29:24,570
vowel is no problem at all because the

760
00:29:24,570 --> 00:29:26,090
language won't allow you to change it a

761
00:29:26,090 --> 00:29:28,740
var it's problematic has to be protected

762
00:29:28,740 --> 00:29:30,060
by some kind of mutually exclusive

763
00:29:30,060 --> 00:29:31,350
region okay so this is why they are

764
00:29:31,350 --> 00:29:35,130
they're troublesome okay now the Val

765
00:29:35,130 --> 00:29:38,100
itself can be troublesome because every

766
00:29:38,100 --> 00:29:40,170
time you want to use it into some other

767
00:29:40,170 --> 00:29:41,790
object that object itself becomes

768
00:29:41,790 --> 00:29:43,530
immutable rights immutable it's exactly

769
00:29:43,530 --> 00:29:44,670
what the name suggests you cannot change

770
00:29:44,670 --> 00:29:46,230
the structure you can embed it into

771
00:29:46,230 --> 00:29:47,550
other structures and do other things but

772
00:29:47,550 --> 00:29:49,460
you cannot change that specific object

773
00:29:49,460 --> 00:29:53,850
okay Java code is index that's basically

774
00:29:53,850 --> 00:29:55,530
the mutable version is borrowed from C

775
00:29:55,530 --> 00:30:00,150
C++ final string X this is the the Const

776
00:30:00,150 --> 00:30:01,410
right it would be equivalent to some

777
00:30:01,410 --> 00:30:03,480
kind of Const something in C++ for

778
00:30:03,480 --> 00:30:09,090
example right so just pay attention to

779
00:30:09,090 --> 00:30:11,340
those VAR val by the way borrowed from

780
00:30:11,340 --> 00:30:14,250
ml as well and they'll had some some of

781
00:30:14,250 --> 00:30:16,740
the some of these imperative features so

782
00:30:16,740 --> 00:30:22,020
it allowed VARs which is was argued that

783
00:30:22,020 --> 00:30:24,240
is not good for languages that wants to

784
00:30:24,240 --> 00:30:25,830
be functional by the way what does a

785
00:30:25,830 --> 00:30:27,870
functional programming language mean and

786
00:30:27,870 --> 00:30:30,090
Scala is to a large extent it can be

787
00:30:30,090 --> 00:30:31,770
functional so what's a functional

788
00:30:31,770 --> 00:30:34,440
programming language but this is ideas

789
00:30:34,440 --> 00:30:37,020
that mathematicians had right a pure

790
00:30:37,020 --> 00:30:39,420
function is some kind of a manipulation

791
00:30:39,420 --> 00:30:41,610
that has no side effects it can produce

792
00:30:41,610 --> 00:30:43,260
a value but that's it is not allowed to

793
00:30:43,260 --> 00:30:44,429
influence the world in

794
00:30:44,429 --> 00:30:45,899
other way except for the value it

795
00:30:45,899 --> 00:30:47,850
produces it doesn't change anything it

796
00:30:47,850 --> 00:30:50,039
doesn't even need the notion of a state

797
00:30:50,039 --> 00:30:53,159
it's a pure transformation for example x

798
00:30:53,159 --> 00:30:55,590
squared right X goes into X square it's

799
00:30:55,590 --> 00:30:59,190
a pure transformation but it's print X a

800
00:30:59,190 --> 00:31:01,980
pure transformation no because it has to

801
00:31:01,980 --> 00:31:04,169
change the state of what goes on the

802
00:31:04,169 --> 00:31:06,600
screen so you have to manipulate some

803
00:31:06,600 --> 00:31:07,980
sort of a state to the state of the

804
00:31:07,980 --> 00:31:10,049
pixels on the screen right so print X

805
00:31:10,049 --> 00:31:14,460
not a pure function X goes to X square a

806
00:31:14,460 --> 00:31:16,499
pure function a by the way all modern

807
00:31:16,499 --> 00:31:17,820
compilers try to figure out if a

808
00:31:17,820 --> 00:31:19,320
function is pure or not because if it's

809
00:31:19,320 --> 00:31:21,299
pure it can be optimized much much much

810
00:31:21,299 --> 00:31:22,769
better actually dealing with side

811
00:31:22,769 --> 00:31:25,080
effects is one of the the hardest

812
00:31:25,080 --> 00:31:26,639
problems to deal with when you compile a

813
00:31:26,639 --> 00:31:31,919
language okay so Scott designers really

814
00:31:31,919 --> 00:31:34,889
wanted a lot of this functional

815
00:31:34,889 --> 00:31:36,990
programming to go on it can be optimized

816
00:31:36,990 --> 00:31:38,899
better the code is much cleaner

817
00:31:38,899 --> 00:31:41,519
side-effects in any function are bound

818
00:31:41,519 --> 00:31:43,950
to create hard to understand scenarios

819
00:31:43,950 --> 00:31:45,960
right you look at the function for

820
00:31:45,960 --> 00:31:49,740
example imagine that ax square not only

821
00:31:49,740 --> 00:31:51,539
it returns the square but changes a

822
00:31:51,539 --> 00:31:53,070
global variable to remember what the

823
00:31:53,070 --> 00:31:55,110
last square was you see this code all

824
00:31:55,110 --> 00:31:58,049
the time right now that can actually be

825
00:31:58,049 --> 00:32:01,169
very problematic right because then you

826
00:32:01,169 --> 00:32:02,610
can think about I'm gonna do a nice

827
00:32:02,610 --> 00:32:04,230
optimization the nice optimization is

828
00:32:04,230 --> 00:32:06,090
gonna be if I have to square the number

829
00:32:06,090 --> 00:32:08,220
twice hey I can just access that global

830
00:32:08,220 --> 00:32:09,690
variable that already has the last

831
00:32:09,690 --> 00:32:11,429
square except that in the meantime you

832
00:32:11,429 --> 00:32:13,139
had somebody else in another friend call

833
00:32:13,139 --> 00:32:15,779
X square and suddenly you're side effect

834
00:32:15,779 --> 00:32:18,059
is not thread safe oops

835
00:32:18,059 --> 00:32:20,340
right so it's very important to keep as

836
00:32:20,340 --> 00:32:22,350
much as possible this functional flavour

837
00:32:22,350 --> 00:32:24,149
to a language now a pure function

838
00:32:24,149 --> 00:32:25,440
programming languages are really

839
00:32:25,440 --> 00:32:27,119
annoying so Scala people wanted to stay

840
00:32:27,119 --> 00:32:29,659
away from them they allow imperative VAR

841
00:32:29,659 --> 00:32:35,179
imperative all very functional okay okay

842
00:32:35,179 --> 00:32:38,820
expressions now Scala has something

843
00:32:38,820 --> 00:32:43,230
really cool is this a so called case

844
00:32:43,230 --> 00:32:46,590
expressions right now you do have some

845
00:32:46,590 --> 00:32:49,049
sort of a case expression maybe not

846
00:32:49,049 --> 00:32:52,590
quite in languages like C write a switch

847
00:32:52,590 --> 00:32:55,109
case statement so now let me ask you a

848
00:32:55,109 --> 00:32:56,999
question right from your

849
00:32:56,999 --> 00:33:00,719
cds or c++ days what allowed for the

850
00:33:00,719 --> 00:33:06,919
case part in a case expression all right

851
00:33:06,919 --> 00:33:11,539
so now right I'm sorry

852
00:33:11,539 --> 00:33:14,039
well definitely not an object but really

853
00:33:14,039 --> 00:33:16,259
in C++ is just integers but that's

854
00:33:16,259 --> 00:33:18,389
really annoying because why because

855
00:33:18,389 --> 00:33:20,399
suddenly we are forced to have a nomes

856
00:33:20,399 --> 00:33:22,379
which happen to be some sort of fancy

857
00:33:22,379 --> 00:33:24,689
integers everywhere right so we were

858
00:33:24,689 --> 00:33:26,609
already given some sort of a recipe you

859
00:33:26,609 --> 00:33:27,989
want to use a switch fine

860
00:33:27,989 --> 00:33:31,229
use the gnomes and or pure integers I'm

861
00:33:31,229 --> 00:33:33,269
not gonna allow anything else right the

862
00:33:33,269 --> 00:33:37,349
code is very hard to keep nice ok now

863
00:33:37,349 --> 00:33:39,509
what would be the coolest thing ever to

864
00:33:39,509 --> 00:33:41,449
say oh you know what I have an object

865
00:33:41,449 --> 00:33:44,609
object and if the object happens to be

866
00:33:44,609 --> 00:33:46,739
in this case well we don't have an

867
00:33:46,739 --> 00:33:48,389
example here if the object happens to be

868
00:33:48,389 --> 00:33:50,459
I don't know an integer then I want you

869
00:33:50,459 --> 00:33:52,049
to do this if it happens to be a string

870
00:33:52,049 --> 00:33:53,399
I want you to do this wouldn't that be

871
00:33:53,399 --> 00:33:55,949
cool what skål allows that now how the

872
00:33:55,949 --> 00:33:57,359
language implements this is a different

873
00:33:57,359 --> 00:33:59,039
story but actually scull allows much

874
00:33:59,039 --> 00:34:00,869
more than that you can actually do very

875
00:34:00,869 --> 00:34:03,149
deep pattern matching in which you can

876
00:34:03,149 --> 00:34:04,859
specify even sub patterns this comes

877
00:34:04,859 --> 00:34:08,250
from Prolog right so you can say if you

878
00:34:08,250 --> 00:34:13,918
see a string that matches this string

879
00:34:13,918 --> 00:34:16,049
matching pattern then you do this if you

880
00:34:16,049 --> 00:34:17,819
string that matches this other pair and

881
00:34:17,819 --> 00:34:19,259
do this if you see an integer you do

882
00:34:19,259 --> 00:34:21,299
this makes the code extremely readable

883
00:34:21,299 --> 00:34:23,069
ok we're gonna come back to this later

884
00:34:23,069 --> 00:34:27,960
so the the expression match in case it's

885
00:34:27,960 --> 00:34:30,179
very powerful in Scala much more

886
00:34:30,179 --> 00:34:32,009
powerful than the switch statement in

887
00:34:32,009 --> 00:34:35,039
languages like Java or C++ ok much much

888
00:34:35,039 --> 00:34:37,829
more and this makes the code extremely

889
00:34:37,829 --> 00:34:39,480
legible so when you combine a very

890
00:34:39,480 --> 00:34:41,579
compact language together with the case

891
00:34:41,579 --> 00:34:43,440
expressions and we're gonna see this all

892
00:34:43,440 --> 00:34:46,529
over the place in Scala it makes a very

893
00:34:46,529 --> 00:34:48,509
powerful compact language that's easy to

894
00:34:48,509 --> 00:34:50,699
read because it fits almost everything

895
00:34:50,699 --> 00:34:54,559
sits on a single on a single page right

896
00:34:54,559 --> 00:34:57,329
all right now another very annoying

897
00:34:57,329 --> 00:34:59,369
thing and this really bothers me because

898
00:34:59,369 --> 00:35:02,400
it's so cumbersome to implement right so

899
00:35:02,400 --> 00:35:05,009
you almost always in a language so

900
00:35:05,009 --> 00:35:07,170
classes are nice right classes specify

901
00:35:07,170 --> 00:35:08,880
your common interface that multiple

902
00:35:08,880 --> 00:35:09,930
objects I'm going to have and then you

903
00:35:09,930 --> 00:35:10,770
can talk about these

904
00:35:10,770 --> 00:35:11,940
of classes right you're all familiar

905
00:35:11,940 --> 00:35:14,280
with that that's what I miss you P but

906
00:35:14,280 --> 00:35:16,830
you almost always want either static

907
00:35:16,830 --> 00:35:19,530
members of a class so what exactly is a

908
00:35:19,530 --> 00:35:21,750
static member this comes from C++ C plus

909
00:35:21,750 --> 00:35:25,470
was patrolling than they did right so

910
00:35:25,470 --> 00:35:29,900
what's a static member I'm sorry

911
00:35:37,800 --> 00:35:41,859
so it's actually not only that so the

912
00:35:41,859 --> 00:35:43,990
definition I got is static members are

913
00:35:43,990 --> 00:35:45,910
members that exist even after the values

914
00:35:45,910 --> 00:35:47,410
are deleted well the existing one before

915
00:35:47,410 --> 00:35:49,119
any value is created actually static

916
00:35:49,119 --> 00:35:53,050
members just exist not only that they

917
00:35:53,050 --> 00:35:54,910
are common for all elements of the class

918
00:35:54,910 --> 00:35:57,849
so there's this weird aura

919
00:35:57,849 --> 00:36:00,960
attached to them with this weird rules

920
00:36:00,960 --> 00:36:03,579
well this car designers took probably

921
00:36:03,579 --> 00:36:04,930
the best decision of them all and you

922
00:36:04,930 --> 00:36:06,310
have to be aware of this because by the

923
00:36:06,310 --> 00:36:07,480
way every when you look at the Scala

924
00:36:07,480 --> 00:36:09,520
documentation you're gonna see two

925
00:36:09,520 --> 00:36:11,859
choices there do you mean the object or

926
00:36:11,859 --> 00:36:13,060
you do me in the class so what they did

927
00:36:13,060 --> 00:36:15,430
is they realize that quite often you

928
00:36:15,430 --> 00:36:18,310
want either static members or in general

929
00:36:18,310 --> 00:36:21,369
to have a single term for a class one

930
00:36:21,369 --> 00:36:22,990
specific member of the class will always

931
00:36:22,990 --> 00:36:25,480
exist that does something so literally

932
00:36:25,480 --> 00:36:27,900
what they said is hey why don't we call

933
00:36:27,900 --> 00:36:32,200
we have two keywords a class specifies

934
00:36:32,200 --> 00:36:33,910
the class definition what of those

935
00:36:33,910 --> 00:36:36,250
objects do and the object specifies that

936
00:36:36,250 --> 00:36:38,380
singleton what they a special guy can do

937
00:36:38,380 --> 00:36:41,349
right and by separating things that make

938
00:36:41,349 --> 00:36:42,940
make sure like much much much easier

939
00:36:42,940 --> 00:36:44,290
so anything that normally you would

940
00:36:44,290 --> 00:36:46,720
associate with a static goes into object

941
00:36:46,720 --> 00:36:48,030
that has the same name of the class

942
00:36:48,030 --> 00:36:50,470
anything that goes with all of the

943
00:36:50,470 --> 00:36:52,780
members of the class goes into the into

944
00:36:52,780 --> 00:36:55,890
the class itself so by default all

945
00:36:55,890 --> 00:36:59,290
classes in Scala can have a single term

946
00:36:59,290 --> 00:37:00,790
you just have to call it object simple

947
00:37:00,790 --> 00:37:02,140
and just have to tell me what that guy

948
00:37:02,140 --> 00:37:04,510
does it's a very convenient way to do a

949
00:37:04,510 --> 00:37:06,280
very common thing that's singleton

950
00:37:06,280 --> 00:37:10,230
object okay

951
00:37:11,380 --> 00:37:13,180
all right trades I'm not gonna go

952
00:37:13,180 --> 00:37:15,970
through this right they they define

953
00:37:15,970 --> 00:37:18,220
interfaces they could be abstract so

954
00:37:18,220 --> 00:37:19,540
abstract means you specify some

955
00:37:19,540 --> 00:37:21,550
constraints on them right you're

956
00:37:21,550 --> 00:37:22,450
basically just saying for example

957
00:37:22,450 --> 00:37:25,750
abstract abstract method it's something

958
00:37:25,750 --> 00:37:27,970
that map's a string into an end but I'm

959
00:37:27,970 --> 00:37:29,110
not gonna tell you what it is it has to

960
00:37:29,110 --> 00:37:32,080
be defined later so these are also ideas

961
00:37:32,080 --> 00:37:33,550
coming from this languages like Prolog

962
00:37:33,550 --> 00:37:34,930
in which you can partially define

963
00:37:34,930 --> 00:37:37,000
something elected hanging and cannot be

964
00:37:37,000 --> 00:37:39,610
used until you fully instantiated okay

965
00:37:39,610 --> 00:37:41,080
you can do some sort of a person

966
00:37:41,080 --> 00:37:42,880
association in this case it's very

967
00:37:42,880 --> 00:37:44,440
partial nothing is defined except the

968
00:37:44,440 --> 00:37:47,140
type of the function okay or this one is

969
00:37:47,140 --> 00:37:50,440
more specific you say the concrete

970
00:37:50,440 --> 00:37:53,710
method Maps a string and notice that

971
00:37:53,710 --> 00:37:56,350
there is no return type type inference

972
00:37:56,350 --> 00:37:59,710
is gonna kick in right any function has

973
00:37:59,710 --> 00:38:02,200
a very clearly defined return type

974
00:38:02,200 --> 00:38:04,690
because Scala is a strongly typed

975
00:38:04,690 --> 00:38:06,400
language what's a strongly typed

976
00:38:06,400 --> 00:38:11,110
language lots of big words today so

977
00:38:11,110 --> 00:38:14,680
what's a weakly typed language so let's

978
00:38:14,680 --> 00:38:20,530
let's vote on some languages okay so

979
00:38:20,530 --> 00:38:22,540
let's say something like I mean even

980
00:38:22,540 --> 00:38:23,380
Python

981
00:38:23,380 --> 00:38:25,510
what's Python strongly typed or actually

982
00:38:25,510 --> 00:38:26,800
usually you don't call them necessary

983
00:38:26,800 --> 00:38:28,120
weakly typed languages you call them

984
00:38:28,120 --> 00:38:31,240
dynamic type languages so what's a

985
00:38:31,240 --> 00:38:33,520
dynamic type language the type of an

986
00:38:33,520 --> 00:38:36,240
object I'm sorry

987
00:38:37,500 --> 00:38:40,810
right right so you can do weird stuff

988
00:38:40,810 --> 00:38:44,500
like X is equal to two int of X so X was

989
00:38:44,500 --> 00:38:46,270
a string and you call the function to

990
00:38:46,270 --> 00:38:47,500
int and you take the value of X and put

991
00:38:47,500 --> 00:38:49,360
it into X and now suddenly access an end

992
00:38:49,360 --> 00:38:51,370
even though it was a string before now

993
00:38:51,370 --> 00:38:55,900
that makes for very cute code but also

994
00:38:55,900 --> 00:38:58,480
makes for not particularly efficient

995
00:38:58,480 --> 00:39:01,210
code why you have to carry the type of

996
00:39:01,210 --> 00:39:05,170
an object with the object so an object

997
00:39:05,170 --> 00:39:07,330
is not gonna be only the value of the

998
00:39:07,330 --> 00:39:08,710
object is gonna be the type of the

999
00:39:08,710 --> 00:39:10,660
object and then all kinds of other

1000
00:39:10,660 --> 00:39:11,650
things for good measure

1001
00:39:11,650 --> 00:39:14,560
okay so that adds a lot of balance to

1002
00:39:14,560 --> 00:39:17,370
the language yes

1003
00:39:21,110 --> 00:39:23,550
trait is comparable with abstract class

1004
00:39:23,550 --> 00:39:25,920
the important thing to watch for in

1005
00:39:25,920 --> 00:39:29,040
Scala is that it has much cleaner better

1006
00:39:29,040 --> 00:39:31,110
confined and more basic definitions of

1007
00:39:31,110 --> 00:39:34,230
everything plus composite composing the

1008
00:39:34,230 --> 00:39:38,070
trouble with Java is it was derived from

1009
00:39:38,070 --> 00:39:39,750
simple as pass and it did not remove all

1010
00:39:39,750 --> 00:39:42,330
the problems in C++ right

1011
00:39:42,330 --> 00:39:44,700
the trouble with C++ is it's a language

1012
00:39:44,700 --> 00:39:49,530
designed by the C hackers to provide job

1013
00:39:49,530 --> 00:39:54,900
security for C hackers I mean it's it's

1014
00:39:54,900 --> 00:39:56,700
an indescribable language I mean don't

1015
00:39:56,700 --> 00:39:58,500
get me wrong it's the right language to

1016
00:39:58,500 --> 00:40:00,360
do certain things for example

1017
00:40:00,360 --> 00:40:05,850
high-performance anything right but the

1018
00:40:05,850 --> 00:40:07,440
language itself still puzzles me after

1019
00:40:07,440 --> 00:40:09,810
20 years anyway Scala much much much

1020
00:40:09,810 --> 00:40:11,370
cleaner and you're right ok you're gonna

1021
00:40:11,370 --> 00:40:13,470
have to use this tricks so a strongly

1022
00:40:13,470 --> 00:40:15,540
typed language is a language in which at

1023
00:40:15,540 --> 00:40:18,570
every single point in a program every

1024
00:40:18,570 --> 00:40:21,810
variable has precisely one type and the

1025
00:40:21,810 --> 00:40:23,670
compiler can figure out exactly what

1026
00:40:23,670 --> 00:40:26,910
type it has ok so literally and by the

1027
00:40:26,910 --> 00:40:28,920
way this is going to be really your only

1028
00:40:28,920 --> 00:40:30,600
problem in Scala your only major problem

1029
00:40:30,600 --> 00:40:33,840
in Scala it's nice to use the type

1030
00:40:33,840 --> 00:40:36,510
inference if the system cannot use the

1031
00:40:36,510 --> 00:40:39,750
type inference it will ask you for some

1032
00:40:39,750 --> 00:40:41,670
types in there to figure out what the

1033
00:40:41,670 --> 00:40:43,860
type how to finish the type inference

1034
00:40:43,860 --> 00:40:47,280
and some of those messages are gonna get

1035
00:40:47,280 --> 00:40:49,320
annoying especially when you use it and

1036
00:40:49,320 --> 00:40:51,210
so telling things like futures ok which

1037
00:40:51,210 --> 00:40:53,790
I will touch upon a little bit right you

1038
00:40:53,790 --> 00:40:55,710
use futures you're gonna get this very

1039
00:40:55,710 --> 00:40:57,450
weird message saying that I can't quite

1040
00:40:57,450 --> 00:40:59,100
figure out how to instantiate this type

1041
00:40:59,100 --> 00:41:01,740
and that's that's when you're gonna hate

1042
00:41:01,740 --> 00:41:03,390
scroll a little bit but trust me it's

1043
00:41:03,390 --> 00:41:07,050
worth the effort ok to actually learn it

1044
00:41:07,050 --> 00:41:10,890
so in this circumstance the return type

1045
00:41:10,890 --> 00:41:12,870
it's inferred and how could possibly is

1046
00:41:12,870 --> 00:41:15,630
18:4 well you see X is a string you see

1047
00:41:15,630 --> 00:41:18,210
a plus between a string and a field and

1048
00:41:18,210 --> 00:41:19,800
plus it's only supported for strings you

1049
00:41:19,800 --> 00:41:21,000
can look it up at what functions are

1050
00:41:21,000 --> 00:41:22,290
defined so you can actually infer that

1051
00:41:22,290 --> 00:41:25,410
the result it's in fact a string right

1052
00:41:25,410 --> 00:41:27,600
not to mention that the field itself

1053
00:41:27,600 --> 00:41:29,250
it's a string how do we know it's a

1054
00:41:29,250 --> 00:41:30,720
string look at how its instantiate

1055
00:41:30,720 --> 00:41:33,330
it remember the type of an object is

1056
00:41:33,330 --> 00:41:35,310
fully determined at compile time and

1057
00:41:35,310 --> 00:41:38,369
because this constant will be

1058
00:41:38,369 --> 00:41:40,590
automatically converted to a string it's

1059
00:41:40,590 --> 00:41:41,849
essentially a constant string this is

1060
00:41:41,849 --> 00:41:43,830
how scallop defines it you know that you

1061
00:41:43,830 --> 00:41:45,510
can infer immediately field it's a

1062
00:41:45,510 --> 00:41:47,700
string X is a string classes between

1063
00:41:47,700 --> 00:41:49,020
strings of the result is string you look

1064
00:41:49,020 --> 00:41:53,640
up in the function right by the way this

1065
00:41:53,640 --> 00:41:57,030
type inference exists now in C++ they

1066
00:41:57,030 --> 00:41:58,500
have a fancy name for it is called Auto

1067
00:41:58,500 --> 00:42:01,380
right if you just put autos everywhere

1068
00:42:01,380 --> 00:42:02,790
and the compiler is going to try to

1069
00:42:02,790 --> 00:42:04,530
figure out what is it that you want to

1070
00:42:04,530 --> 00:42:06,060
do and in particular is very good for

1071
00:42:06,060 --> 00:42:08,430
variables you can say Auto X is equal to

1072
00:42:08,430 --> 00:42:12,180
an expression amazing especially when

1073
00:42:12,180 --> 00:42:13,740
you have those ugly templates

1074
00:42:13,740 --> 00:42:17,250
instantiated right in C++ it's my my

1075
00:42:17,250 --> 00:42:20,040
favorite new addition to the C++ in the

1076
00:42:20,040 --> 00:42:23,880
C++ x11 ok the auto thing they had the

1077
00:42:23,880 --> 00:42:26,210
GCC had a feature before called type

1078
00:42:26,210 --> 00:42:29,010
type def and type info and things like

1079
00:42:29,010 --> 00:42:30,750
that that were not quite as good as Auto

1080
00:42:30,750 --> 00:42:37,980
okay good right so here is an

1081
00:42:37,980 --> 00:42:39,630
interesting question right now the

1082
00:42:39,630 --> 00:42:41,010
slides are really messed up but not too

1083
00:42:41,010 --> 00:42:42,599
much so we have this capital it's equal

1084
00:42:42,599 --> 00:42:44,490
to map US goes into Washington what's

1085
00:42:44,490 --> 00:42:48,690
this what's this arrow now usually in a

1086
00:42:48,690 --> 00:42:49,980
language that's some kind of a special

1087
00:42:49,980 --> 00:42:51,839
key or special operators some sort of a

1088
00:42:51,839 --> 00:42:52,200
thing right

1089
00:42:52,200 --> 00:42:53,849
most of the anguishes would in fact for

1090
00:42:53,849 --> 00:42:55,260
example python has this will have a

1091
00:42:55,260 --> 00:42:58,320
built in dictionary type or PHP is like

1092
00:42:58,320 --> 00:43:00,240
this right as a built-in dictionary type

1093
00:43:00,240 --> 00:43:01,950
that has its own little syntax and then

1094
00:43:01,950 --> 00:43:03,900
you have to hardwire it in the in the

1095
00:43:03,900 --> 00:43:05,400
syntax of the language and whatnot well

1096
00:43:05,400 --> 00:43:07,500
it turns out that here the map is

1097
00:43:07,500 --> 00:43:09,480
completely in a library and uses no

1098
00:43:09,480 --> 00:43:11,339
special anything in this color language

1099
00:43:11,339 --> 00:43:14,839
and this is in fact an operator it's a I

1100
00:43:14,839 --> 00:43:19,530
use it it's a library defined operator

1101
00:43:19,530 --> 00:43:21,450
and essentially what that operator does

1102
00:43:21,450 --> 00:43:25,410
it forms a topple so it takes the two

1103
00:43:25,410 --> 00:43:26,550
members at the left hand and the right

1104
00:43:26,550 --> 00:43:28,589
hand bundles them into the top of the

1105
00:43:28,589 --> 00:43:30,630
temple it's a basic type in Scala right

1106
00:43:30,630 --> 00:43:33,060
if you take multiple values and you glue

1107
00:43:33,060 --> 00:43:35,280
them together that forms sort some kind

1108
00:43:35,280 --> 00:43:36,920
of a generalization of the record right

1109
00:43:36,920 --> 00:43:39,660
you're familiar with tapas most of them

1110
00:43:39,660 --> 00:43:41,310
I mean most languages don't have a

1111
00:43:41,310 --> 00:43:42,780
particularly good way to form tapas what

1112
00:43:42,780 --> 00:43:43,890
Scala does

1113
00:43:43,890 --> 00:43:45,779
so this is actually I think a couple is

1114
00:43:45,779 --> 00:43:47,700
written with open brackets and comma

1115
00:43:47,700 --> 00:43:49,710
separated elements inside so this the US

1116
00:43:49,710 --> 00:43:51,029
goes to Washington it's equipment to

1117
00:43:51,029 --> 00:43:52,859
open bracket u.s. comma Washington okay

1118
00:43:52,859 --> 00:43:54,599
so this is basically some sort of syntax

1119
00:43:54,599 --> 00:43:56,579
sugar it's a very shuttle function that

1120
00:43:56,579 --> 00:43:57,119
does that

1121
00:43:57,119 --> 00:44:00,839
okay so you're gonna see a lot of these

1122
00:44:00,839 --> 00:44:02,819
things and you can invent your own and

1123
00:44:02,819 --> 00:44:06,359
actually the arrow can be any anything

1124
00:44:06,359 --> 00:44:09,089
that looks like a function right so the

1125
00:44:09,089 --> 00:44:10,470
error is really a function that takes

1126
00:44:10,470 --> 00:44:12,630
two arguments and forms a single result

1127
00:44:12,630 --> 00:44:19,369
that's of type a type topple okay right

1128
00:44:19,369 --> 00:44:21,660
right so we talked about the tied right

1129
00:44:21,660 --> 00:44:26,630
the colon it's used to specify types yes

1130
00:44:31,540 --> 00:44:35,800
you know but this is syntax for four so

1131
00:44:35,800 --> 00:44:39,850
that that basically means C takes the

1132
00:44:39,850 --> 00:44:41,320
value capital K is when you go over to

1133
00:44:41,320 --> 00:44:43,270
for loop but the four here is some kind

1134
00:44:43,270 --> 00:44:45,160
of a almost like a functional

1135
00:44:45,160 --> 00:44:48,640
programming for it feels more so I mean

1136
00:44:48,640 --> 00:44:50,050
I'm gonna ask you to go and delve into

1137
00:44:50,050 --> 00:44:52,450
the language and so on but because the

1138
00:44:52,450 --> 00:44:55,180
language has a functional flavor the for

1139
00:44:55,180 --> 00:44:57,880
loops can look nicer right the for loops

1140
00:44:57,880 --> 00:45:00,730
in C and any language derived from C are

1141
00:45:00,730 --> 00:45:02,800
weird contortion of things right with

1142
00:45:02,800 --> 00:45:05,260
their own weird rules to a large extent

1143
00:45:05,260 --> 00:45:07,750
this four can be some sort of a function

1144
00:45:07,750 --> 00:45:11,410
that goes over some map or some sort of

1145
00:45:11,410 --> 00:45:13,420
a container and enumerates the elements

1146
00:45:13,420 --> 00:45:14,830
in the container and for each of them

1147
00:45:14,830 --> 00:45:16,810
gives names to local variables and then

1148
00:45:16,810 --> 00:45:18,610
you can do something with the names this

1149
00:45:18,610 --> 00:45:21,550
is what most high-level more script like

1150
00:45:21,550 --> 00:45:23,620
languages do for example this is how

1151
00:45:23,620 --> 00:45:25,480
many of you wrote anything in PHP for

1152
00:45:25,480 --> 00:45:27,130
example but I mean even in JavaScript is

1153
00:45:27,130 --> 00:45:31,150
true right so the syntax tends to be

1154
00:45:31,150 --> 00:45:32,650
like this you go over a right I mean you

1155
00:45:32,650 --> 00:45:35,200
have for each going over an array an

1156
00:45:35,200 --> 00:45:36,820
array arrays are really dictionaries it

1157
00:45:36,820 --> 00:45:42,430
for example in PHP or in pipe okay let's

1158
00:45:42,430 --> 00:45:45,820
keep over some of this right so this is

1159
00:45:45,820 --> 00:45:47,590
basically more elaborate example right

1160
00:45:47,590 --> 00:45:50,470
you have in long flow doubles are basic

1161
00:45:50,470 --> 00:45:52,540
types you might want to graduate to more

1162
00:45:52,540 --> 00:45:54,280
advanced types why would you do that

1163
00:45:54,280 --> 00:45:56,440
well because the hardware starts to

1164
00:45:56,440 --> 00:45:58,420
support very fancy types for example I

1165
00:45:58,420 --> 00:46:01,510
just found out couple of weeks back that

1166
00:46:01,510 --> 00:46:04,960
the new GCC compiler supports 128 bit

1167
00:46:04,960 --> 00:46:07,750
floats that's great by the way if you

1168
00:46:07,750 --> 00:46:09,010
want to do very large-scale data

1169
00:46:09,010 --> 00:46:10,630
processing because otherwise you can

1170
00:46:10,630 --> 00:46:13,510
overflow even a 80 bit float the double

1171
00:46:13,510 --> 00:46:15,730
right so 128 it's perfect

1172
00:46:15,730 --> 00:46:17,950
but then ok fine how I'm gonna use a 128

1173
00:46:17,950 --> 00:46:20,620
bit float you know for example of how

1174
00:46:20,620 --> 00:46:23,730
language is like c plus plus do this

1175
00:46:23,730 --> 00:46:26,200
well languages like c++ are weird they

1176
00:46:26,200 --> 00:46:28,210
have connections with assembly right so

1177
00:46:28,210 --> 00:46:29,590
worst comes to worst you just generate

1178
00:46:29,590 --> 00:46:30,880
some assembly instructions at the lowest

1179
00:46:30,880 --> 00:46:32,890
level and be done with it and this is

1180
00:46:32,890 --> 00:46:34,660
literally how you do things but it's

1181
00:46:34,660 --> 00:46:37,510
important to add support for things like

1182
00:46:37,510 --> 00:46:40,150
this in the language in a more library

1183
00:46:40,150 --> 00:46:42,250
way rather than put them in the core

1184
00:46:42,250 --> 00:46:43,780
language right so this is an example of

1185
00:46:43,780 --> 00:46:45,340
how you could support

1186
00:46:45,340 --> 00:46:48,580
types but the problem is in Java these

1187
00:46:48,580 --> 00:46:50,440
basic types that are natively supported

1188
00:46:50,440 --> 00:46:52,480
are treated differently than anything

1189
00:46:52,480 --> 00:46:55,120
derived from object any object line type

1190
00:46:55,120 --> 00:46:57,190
right but not so in Scala and you can go

1191
00:46:57,190 --> 00:46:58,570
through this example I don't quite have

1192
00:46:58,570 --> 00:47:01,900
time to go for it right for example look

1193
00:47:01,900 --> 00:47:03,430
at this this is a factorial function

1194
00:47:03,430 --> 00:47:05,410
defined on this type begin which still

1195
00:47:05,410 --> 00:47:08,470
needs to be defined right in this case

1196
00:47:08,470 --> 00:47:10,360
there is a full type specification you

1197
00:47:10,360 --> 00:47:12,460
say the input is a single value X of

1198
00:47:12,460 --> 00:47:14,830
type big int and returns big and it

1199
00:47:14,830 --> 00:47:17,260
makes it so much nicer to say you take

1200
00:47:17,260 --> 00:47:18,640
this as an input and then this is the

1201
00:47:18,640 --> 00:47:20,260
type of the output as opposed to the C

1202
00:47:20,260 --> 00:47:22,420
way in which the return is somewhere and

1203
00:47:22,420 --> 00:47:24,520
it's all over the place right and notice

1204
00:47:24,520 --> 00:47:26,470
you just need the DEF definite defines a

1205
00:47:26,470 --> 00:47:28,300
function that's similar to Python I

1206
00:47:28,300 --> 00:47:32,920
think right varville do the constant I

1207
00:47:32,920 --> 00:47:35,080
mean the non constant mutable and

1208
00:47:35,080 --> 00:47:38,280
immutable variables very nicely right

1209
00:47:38,280 --> 00:47:41,530
also the Scala has another feature

1210
00:47:41,530 --> 00:47:43,300
that's borrowed from functional

1211
00:47:43,300 --> 00:47:45,070
programming languages which is the last

1212
00:47:45,070 --> 00:47:46,690
statement in the body of a function

1213
00:47:46,690 --> 00:47:50,100
determines the return that makes for

1214
00:47:50,100 --> 00:47:53,350
more compact code in which you don't

1215
00:47:53,350 --> 00:47:55,630
write return whatever is the last thing

1216
00:47:55,630 --> 00:47:56,890
you did that's the thing that determines

1217
00:47:56,890 --> 00:48:01,360
the value right in this case what's that

1218
00:48:01,360 --> 00:48:04,270
with this if statement that essentially

1219
00:48:04,270 --> 00:48:08,230
has two two branches the one if you go

1220
00:48:08,230 --> 00:48:10,540
left and the x times factorial X minus

1221
00:48:10,540 --> 00:48:12,640
one if you go right okay and now need to

1222
00:48:12,640 --> 00:48:13,960
say return this or return in two

1223
00:48:13,960 --> 00:48:16,360
optional feeling okay if you put no

1224
00:48:16,360 --> 00:48:18,610
return in the body of a function the

1225
00:48:18,610 --> 00:48:20,290
last statement is the return by the way

1226
00:48:20,290 --> 00:48:22,240
that's true in JavaScript that's true in

1227
00:48:22,240 --> 00:48:25,180
PHP right so these guys were paying

1228
00:48:25,180 --> 00:48:26,470
attention to how people like to use

1229
00:48:26,470 --> 00:48:30,220
languages okay much uglier version in

1230
00:48:30,220 --> 00:48:36,100
Java okay the example is more elaborate

1231
00:48:36,100 --> 00:48:38,470
right the plus it's really defined like

1232
00:48:38,470 --> 00:48:40,840
any other function why not as long as

1233
00:48:40,840 --> 00:48:41,920
the language can figure out when you

1234
00:48:41,920 --> 00:48:43,420
mean function and it can because if you

1235
00:48:43,420 --> 00:48:44,980
put def that means you're defining a

1236
00:48:44,980 --> 00:48:52,030
function actually C and C++ are very

1237
00:48:52,030 --> 00:48:53,770
funny with this it's quite hard to

1238
00:48:53,770 --> 00:48:54,820
figure out what is it that you're

1239
00:48:54,820 --> 00:48:56,170
defining it there because you can use

1240
00:48:56,170 --> 00:48:57,940
those brackets to do

1241
00:48:57,940 --> 00:48:59,830
other things not only define functions

1242
00:48:59,830 --> 00:49:01,120
right it's so much nice if you have a

1243
00:49:01,120 --> 00:49:03,160
nice keyword that says hey I'm defining

1244
00:49:03,160 --> 00:49:04,660
the function I'm not doing anything else

1245
00:49:04,660 --> 00:49:06,400
right I mean for example javascript has

1246
00:49:06,400 --> 00:49:07,900
that it's called function and this is

1247
00:49:07,900 --> 00:49:16,750
that right control structures this is

1248
00:49:16,750 --> 00:49:18,520
this is interesting and this is why they

1249
00:49:18,520 --> 00:49:19,660
mean by a scalable language you can

1250
00:49:19,660 --> 00:49:21,010
define your own control control

1251
00:49:21,010 --> 00:49:24,940
structures for example this music now in

1252
00:49:24,940 --> 00:49:30,520
order to do that right you need you need

1253
00:49:30,520 --> 00:49:33,070
some magic let me let me see the more

1254
00:49:33,070 --> 00:49:34,750
elaborate example okay so this is an

1255
00:49:34,750 --> 00:49:37,090
interesting example okay so let's look

1256
00:49:37,090 --> 00:49:40,870
at this now you're going to notice

1257
00:49:40,870 --> 00:49:42,640
something that similar actually to Java

1258
00:49:42,640 --> 00:49:45,040
but Java is too slow to do this and it's

1259
00:49:45,040 --> 00:49:46,420
similar but with a different syntax in

1260
00:49:46,420 --> 00:49:48,850
C++ that means a template definition of

1261
00:49:48,850 --> 00:49:50,020
a function

1262
00:49:50,020 --> 00:49:52,120
so what really what's really a template

1263
00:49:52,120 --> 00:49:54,490
did you ever ask yourself so what are

1264
00:49:54,490 --> 00:49:55,540
you doing when you're defining the

1265
00:49:55,540 --> 00:49:58,480
template for example the templating in

1266
00:49:58,480 --> 00:50:01,180
C++ or Java or some other languages so

1267
00:50:01,180 --> 00:50:02,080
what's the difference between a normal

1268
00:50:02,080 --> 00:50:05,230
function and a template a function the

1269
00:50:05,230 --> 00:50:06,940
template a function it's a recipe to

1270
00:50:06,940 --> 00:50:09,970
generate functions a template cannot run

1271
00:50:09,970 --> 00:50:12,430
it needs to be instantiated to become a

1272
00:50:12,430 --> 00:50:14,530
normal function right so it's really

1273
00:50:14,530 --> 00:50:18,040
some sort of a pattern some sort of if

1274
00:50:18,040 --> 00:50:21,910
you want yes a recipe that's a good way

1275
00:50:21,910 --> 00:50:23,920
right so you say if you replace this by

1276
00:50:23,920 --> 00:50:25,180
this and this by this and this by this

1277
00:50:25,180 --> 00:50:26,770
then you can get something that looks

1278
00:50:26,770 --> 00:50:28,900
like a normal function so actually the

1279
00:50:28,900 --> 00:50:30,520
easiest way to think about it is a form

1280
00:50:30,520 --> 00:50:33,460
of cogeneration the compiler internally

1281
00:50:33,460 --> 00:50:36,010
if you have a template will replace the

1282
00:50:36,010 --> 00:50:38,170
template variable with a specific type

1283
00:50:38,170 --> 00:50:40,360
write the code as if you would write it

1284
00:50:40,360 --> 00:50:42,160
but now with the type plugged in is

1285
00:50:42,160 --> 00:50:44,050
exactly the way you specify and then go

1286
00:50:44,050 --> 00:50:45,430
and compile it like any other normal

1287
00:50:45,430 --> 00:50:49,540
function but okay this sounds great but

1288
00:50:49,540 --> 00:50:50,950
the question is what's the language to

1289
00:50:50,950 --> 00:50:52,540
specify these templates and what can you

1290
00:50:52,540 --> 00:50:56,470
do in that language okay and what can

1291
00:50:56,470 --> 00:50:58,120
you express in that language so usually

1292
00:50:58,120 --> 00:51:00,760
the template languages are sub languages

1293
00:51:00,760 --> 00:51:03,400
within the big language and sometimes

1294
00:51:03,400 --> 00:51:05,020
they were added much later and they look

1295
00:51:05,020 --> 00:51:09,550
much stranger right the Scala designers

1296
00:51:09,550 --> 00:51:11,110
decided to design the templating

1297
00:51:11,110 --> 00:51:11,589
mechanism

1298
00:51:11,589 --> 00:51:12,969
at the same time as the rest of the

1299
00:51:12,969 --> 00:51:14,710
language so it actually makes sense and

1300
00:51:14,710 --> 00:51:16,479
by the way they borrowed from the so

1301
00:51:16,479 --> 00:51:19,390
called alpha types in ml okay alpha

1302
00:51:19,390 --> 00:51:21,579
types are generic types in ml okay it's

1303
00:51:21,579 --> 00:51:24,700
a very fancy version of this templating

1304
00:51:24,700 --> 00:51:27,609
mechanism now not to scare developers

1305
00:51:27,609 --> 00:51:29,680
sculler people did a very good job at

1306
00:51:29,680 --> 00:51:31,599
not scaring people ml it's a very scary

1307
00:51:31,599 --> 00:51:33,369
language is full of this extremely

1308
00:51:33,369 --> 00:51:35,859
heavyweight terminology right I mean

1309
00:51:35,859 --> 00:51:37,690
literally to understand fully what alpha

1310
00:51:37,690 --> 00:51:39,819
types that do in ml you have to take an

1311
00:51:39,819 --> 00:51:41,859
advanced class in tide theory okay where

1312
00:51:41,859 --> 00:51:43,690
they talk about the foundation of type

1313
00:51:43,690 --> 00:51:46,180
inference and whatnot Scala people still

1314
00:51:46,180 --> 00:51:47,769
do that but try to stay away from these

1315
00:51:47,769 --> 00:51:49,359
fancy things like alpha types okay but

1316
00:51:49,359 --> 00:51:51,039
they call them template types and this

1317
00:51:51,039 --> 00:51:52,660
is really a template specification you

1318
00:51:52,660 --> 00:51:55,180
say I'm defining a function but it's not

1319
00:51:55,180 --> 00:51:57,099
is really a template right using I know

1320
00:51:57,099 --> 00:51:58,239
it's a template because of the square

1321
00:51:58,239 --> 00:51:59,710
bracket it's going to depend on a

1322
00:51:59,710 --> 00:52:01,719
parameter T and by the way this is a

1323
00:52:01,719 --> 00:52:04,719
restriction on T so this less than :

1324
00:52:04,719 --> 00:52:06,819
it's special notation only when you're

1325
00:52:06,819 --> 00:52:09,549
describing templates to say this is a

1326
00:52:09,549 --> 00:52:10,690
restriction and in this case it

1327
00:52:10,690 --> 00:52:13,269
specifies that the type T must have the

1328
00:52:13,269 --> 00:52:18,009
method closed now in for example in C++

1329
00:52:18,009 --> 00:52:19,989
that's implicit by the way you use the

1330
00:52:19,989 --> 00:52:21,940
template right you say let's call the

1331
00:52:21,940 --> 00:52:23,200
template this and this and this and then

1332
00:52:23,200 --> 00:52:25,180
let's use it and when you compile the

1333
00:52:25,180 --> 00:52:27,160
code it explodes and says hey you're

1334
00:52:27,160 --> 00:52:28,749
supposed to have this method and you

1335
00:52:28,749 --> 00:52:31,809
don't have it sometimes it's hard to

1336
00:52:31,809 --> 00:52:33,219
determine what's going on and you get

1337
00:52:33,219 --> 00:52:36,960
really big messages right in Scala

1338
00:52:36,960 --> 00:52:39,400
because you have this constraints on the

1339
00:52:39,400 --> 00:52:41,769
type from the beginning it's much easier

1340
00:52:41,769 --> 00:52:43,599
to produce nice every message and say

1341
00:52:43,599 --> 00:52:45,729
hey you must have closed and by the way

1342
00:52:45,729 --> 00:52:47,710
you can put here here things like you

1343
00:52:47,710 --> 00:52:49,630
know you need in the future so it's not

1344
00:52:49,630 --> 00:52:51,460
only a use base but it's a future proof

1345
00:52:51,460 --> 00:52:53,469
I told you you must have closed even if

1346
00:52:53,469 --> 00:52:54,640
I don't use it you must have closed

1347
00:52:54,640 --> 00:52:56,170
otherwise I don't allow you to be

1348
00:52:56,170 --> 00:52:58,539
instantiating it this way okay now there

1349
00:52:58,539 --> 00:53:02,729
is a very interesting type in Scala and

1350
00:53:02,789 --> 00:53:06,359
am i over time

1351
00:53:07,599 --> 00:53:10,150
well we have two hours anyway so we'll

1352
00:53:10,150 --> 00:53:12,690
take a break in a different way so I

1353
00:53:12,690 --> 00:53:15,789
mentioned those closures the closures

1354
00:53:15,789 --> 00:53:17,380
themselves are first-class citizens

1355
00:53:17,380 --> 00:53:18,430
actually functions are first class

1356
00:53:18,430 --> 00:53:20,109
citizens I'm gonna mention a second

1357
00:53:20,109 --> 00:53:21,729
precisely what this means but closures

1358
00:53:21,729 --> 00:53:23,769
are first-class citizens so the closure

1359
00:53:23,769 --> 00:53:26,589
type is this unit so this function is

1360
00:53:26,589 --> 00:53:27,999
actually interesting because it is

1361
00:53:27,999 --> 00:53:29,559
essentially your what you're essentially

1362
00:53:29,559 --> 00:53:34,329
saying is that you take as an argument

1363
00:53:34,329 --> 00:53:38,079
for this using a block there takes some

1364
00:53:38,079 --> 00:53:40,900
type into this closure so it takes some

1365
00:53:40,900 --> 00:53:42,489
kind of a closure argument and we're

1366
00:53:42,489 --> 00:53:43,719
going to see this all over the place in

1367
00:53:43,719 --> 00:53:46,779
Scala ok you have this they are almost

1368
00:53:46,779 --> 00:53:49,449
kind of a meta function right they add

1369
00:53:49,449 --> 00:53:50,890
something for themselves we take a

1370
00:53:50,890 --> 00:53:53,559
closer and they embed that closure into

1371
00:53:53,559 --> 00:53:58,959
bigger code to do bigger things okay all

1372
00:53:58,959 --> 00:54:01,569
right now I'll let you go over this and

1373
00:54:01,569 --> 00:54:02,739
try to figure out what he does and

1374
00:54:02,739 --> 00:54:03,849
you're gonna have to do some of these

1375
00:54:03,849 --> 00:54:06,789
things and one of the things that the

1376
00:54:06,789 --> 00:54:09,309
Scala people are most proud of is this

1377
00:54:09,309 --> 00:54:11,890
break and continue story and they don't

1378
00:54:11,890 --> 00:54:15,279
support breaking continue you don't need

1379
00:54:15,279 --> 00:54:20,410
them well I mean how many people here

1380
00:54:20,410 --> 00:54:21,969
have used break and continue in their

1381
00:54:21,969 --> 00:54:25,839
code so now how upset would you be if I

1382
00:54:25,839 --> 00:54:26,890
tell you you don't have break and

1383
00:54:26,890 --> 00:54:30,759
continue well I mean first of all break

1384
00:54:30,759 --> 00:54:33,069
and continue makes for not so nice code

1385
00:54:33,069 --> 00:54:35,319
ok essentially when you write that code

1386
00:54:35,319 --> 00:54:36,819
you're hacked and you're hacking a lot I

1387
00:54:36,819 --> 00:54:38,440
mean that's the first argument but the

1388
00:54:38,440 --> 00:54:39,670
second argument and this is kind of the

1389
00:54:39,670 --> 00:54:42,219
ultimate argument is it's easy to

1390
00:54:42,219 --> 00:54:43,719
provide breaking continued functionality

1391
00:54:43,719 --> 00:54:44,979
in the language but you provide it in a

1392
00:54:44,979 --> 00:54:46,839
library so this is how you know you have

1393
00:54:46,839 --> 00:54:48,249
a very powerful language if a lot of

1394
00:54:48,249 --> 00:54:49,479
things that normally would have to be

1395
00:54:49,479 --> 00:54:50,920
built in into the language are now in

1396
00:54:50,920 --> 00:54:53,079
libraries say I do it in a library and

1397
00:54:53,079 --> 00:54:54,940
you can do it in a library this is

1398
00:54:54,940 --> 00:54:57,039
strange you can do it in a library and

1399
00:54:57,039 --> 00:54:59,170
this is an example of how you could you

1400
00:54:59,170 --> 00:55:01,029
could call it right so let's look at

1401
00:55:01,029 --> 00:55:02,619
this code you say breakable and then

1402
00:55:02,619 --> 00:55:04,599
some kind of an open bracket and a piece

1403
00:55:04,599 --> 00:55:08,229
of code that uses break in here now how

1404
00:55:08,229 --> 00:55:11,229
would this work what exactly is going on

1405
00:55:11,229 --> 00:55:15,009
in here well what's happening is so the

1406
00:55:15,009 --> 00:55:18,560
big question here is what breakable

1407
00:55:18,560 --> 00:55:21,410
right so what's breakable well it turns

1408
00:55:21,410 --> 00:55:23,450
out that breakable it's a function but

1409
00:55:23,450 --> 00:55:27,460
breakable takes as an argument a closure

1410
00:55:27,460 --> 00:55:30,410
now how do we define a closure in

1411
00:55:30,410 --> 00:55:33,320
general in in a man this is not only an

1412
00:55:33,320 --> 00:55:35,420
anonymous piece of code but actually can

1413
00:55:35,420 --> 00:55:36,740
borrow from things that come from

1414
00:55:36,740 --> 00:55:38,660
outside variables in the local context

1415
00:55:38,660 --> 00:55:41,660
right if you do open curly braket a

1416
00:55:41,660 --> 00:55:43,100
piece of code close curly bracket that

1417
00:55:43,100 --> 00:55:45,410
in itself already is a closure so the

1418
00:55:45,410 --> 00:55:47,210
way the language treats it is oh I

1419
00:55:47,210 --> 00:55:49,400
already have this abstract object called

1420
00:55:49,400 --> 00:55:50,780
the closure that I can actually pass

1421
00:55:50,780 --> 00:55:52,040
through this function that takes as

1422
00:55:52,040 --> 00:55:56,330
input closures right so we can look at

1423
00:55:56,330 --> 00:56:00,020
the breakable right so this how you

1424
00:56:00,020 --> 00:56:01,960
could define it right so this is define

1425
00:56:01,960 --> 00:56:06,500
breakable right takes as an argument one

1426
00:56:06,500 --> 00:56:08,450
of the I'm sorry it takes as an argument

1427
00:56:08,450 --> 00:56:11,270
an input and the type of the input can

1428
00:56:11,270 --> 00:56:13,790
be importing is in fact unit takes takes

1429
00:56:13,790 --> 00:56:16,400
a closure and produces a closure right

1430
00:56:16,400 --> 00:56:19,520
and what closure does it produce well

1431
00:56:19,520 --> 00:56:20,990
it's going to put the following code

1432
00:56:20,990 --> 00:56:24,260
truck starts a try block then puts the

1433
00:56:24,260 --> 00:56:26,540
the the argument which is the operand

1434
00:56:26,540 --> 00:56:28,490
the full closure that you wrote puts it

1435
00:56:28,490 --> 00:56:30,980
in and then a catch and it says well if

1436
00:56:30,980 --> 00:56:33,410
the expression which is exceptions right

1437
00:56:33,410 --> 00:56:36,110
if the exception was break exception

1438
00:56:36,110 --> 00:56:40,040
then you return nothing otherwise you

1439
00:56:40,040 --> 00:56:42,290
just return the normal result of the

1440
00:56:42,290 --> 00:56:44,870
closure it's very very compact language

1441
00:56:44,870 --> 00:56:47,870
and then what does the function break do

1442
00:56:47,870 --> 00:56:50,990
just throws the exception okay so now

1443
00:56:50,990 --> 00:56:52,610
these kind of features are extremely

1444
00:56:52,610 --> 00:56:54,020
advanced features this almost looks like

1445
00:56:54,020 --> 00:56:55,670
cogeneration you're composing code

1446
00:56:55,670 --> 00:56:57,620
without a code to make bigger code but

1447
00:56:57,620 --> 00:56:59,060
in a different way than calling the

1448
00:56:59,060 --> 00:57:01,880
function right so these closures because

1449
00:57:01,880 --> 00:57:04,490
they can actually borrow from the

1450
00:57:04,490 --> 00:57:06,200
context notice that you don't call it

1451
00:57:06,200 --> 00:57:08,150
even like a function you say it's as if

1452
00:57:08,150 --> 00:57:10,690
you say take that code and dump it here

1453
00:57:10,690 --> 00:57:14,060
right it's very very powerful feature so

1454
00:57:14,060 --> 00:57:16,490
not so much that you really need to do

1455
00:57:16,490 --> 00:57:17,720
this break and continue which is

1456
00:57:17,720 --> 00:57:18,770
probably not something you should do in

1457
00:57:18,770 --> 00:57:21,020
your code too often but it gives an

1458
00:57:21,020 --> 00:57:22,250
example of how powerful the language

1459
00:57:22,250 --> 00:57:25,250
itself is okay now let me make a

1460
00:57:25,250 --> 00:57:27,890
suggestion since the clock is not

1461
00:57:27,890 --> 00:57:31,010
stopping I don't really need a break we

1462
00:57:31,010 --> 00:57:32,410
can go on and we just stop

1463
00:57:32,410 --> 00:57:34,690
and then we have a bigger break later is

1464
00:57:34,690 --> 00:57:40,089
that fine all right good so let's not go

1465
00:57:40,089 --> 00:57:42,220
for all of these things I'm gonna let

1466
00:57:42,220 --> 00:57:44,500
you actually go would they see you

1467
00:57:44,500 --> 00:57:46,630
exactly what functional means have fun

1468
00:57:46,630 --> 00:57:47,170
with it

1469
00:57:47,170 --> 00:57:51,059
my advice by the way is Google okay

1470
00:57:51,059 --> 00:57:53,890
there are large I mean there are many

1471
00:57:53,890 --> 00:57:55,569
ways to Google one of them is Google

1472
00:57:55,569 --> 00:57:57,579
code the Google code and find projects

1473
00:57:57,579 --> 00:58:01,420
written in Scala Google for Scala code

1474
00:58:01,420 --> 00:58:04,119
or whatever and find many examples I

1475
00:58:04,119 --> 00:58:05,890
mean the best way to learn programming

1476
00:58:05,890 --> 00:58:07,930
is to see good examples of code written

1477
00:58:07,930 --> 00:58:10,420
in that program what I'm not asking you

1478
00:58:10,420 --> 00:58:11,980
to do is read the definition of the

1479
00:58:11,980 --> 00:58:13,359
length the abstract definition of the

1480
00:58:13,359 --> 00:58:14,769
language and suddenly write good

1481
00:58:14,769 --> 00:58:18,099
programs all of these languages come

1482
00:58:18,099 --> 00:58:21,700
with language designers had a certain

1483
00:58:21,700 --> 00:58:23,500
thing in mind the language doesn't

1484
00:58:23,500 --> 00:58:25,690
restrict you to do the nice organization

1485
00:58:25,690 --> 00:58:27,670
for it but if you see some examples you

1486
00:58:27,670 --> 00:58:29,589
say ah that's a much much neater way to

1487
00:58:29,589 --> 00:58:32,829
to tackle things right so just Google

1488
00:58:32,829 --> 00:58:35,380
another suggestion I have is as you're

1489
00:58:35,380 --> 00:58:36,609
gonna Google you're gonna bump into

1490
00:58:36,609 --> 00:58:38,410
interesting resources about Scala or

1491
00:58:38,410 --> 00:58:40,750
other things for the class throw them on

1492
00:58:40,750 --> 00:58:43,390
the mailing list write anything that

1493
00:58:43,390 --> 00:58:45,009
helps you learn so anything that's not a

1494
00:58:45,009 --> 00:58:46,960
solution to a specific problem I asked

1495
00:58:46,960 --> 00:58:49,089
in class it's something that other

1496
00:58:49,089 --> 00:58:50,259
people might be interested on the

1497
00:58:50,259 --> 00:58:52,750
mailing list you find a very good

1498
00:58:52,750 --> 00:58:56,410
tutorial on Scala throw it in you find a

1499
00:58:56,410 --> 00:58:59,589
book that's Creative Commons books so I

1500
00:58:59,589 --> 00:59:01,599
don't want to see proprietary books that

1501
00:59:01,599 --> 00:59:03,250
are shared illegally on the mailing this

1502
00:59:03,250 --> 00:59:05,170
is not good okay but there are so many

1503
00:59:05,170 --> 00:59:07,599
resources out there right

1504
00:59:07,599 --> 00:59:09,220
you have snippets of code for example

1505
00:59:09,220 --> 00:59:10,390
some people are gonna say hey how do you

1506
00:59:10,390 --> 00:59:11,289
do this in Scala

1507
00:59:11,289 --> 00:59:13,089
you can write snippets of code again if

1508
00:59:13,089 --> 00:59:14,740
they are not really a significant part

1509
00:59:14,740 --> 00:59:16,720
of the solution that's fine and I want

1510
00:59:16,720 --> 00:59:19,240
you to use the mailing list as some kind

1511
00:59:19,240 --> 00:59:22,450
of a social network to learn Scala write

1512
00:59:22,450 --> 00:59:24,670
n distributed systems in general right

1513
00:59:24,670 --> 00:59:26,140
the goal of this class is really for you

1514
00:59:26,140 --> 00:59:28,089
to learn and not necessarily for you to

1515
00:59:28,089 --> 00:59:30,309
get a grade which will but it's a kind

1516
00:59:30,309 --> 00:59:34,170
of a side effect right

1517
00:59:34,809 --> 00:59:39,069
right so many many features partial

1518
00:59:39,069 --> 00:59:40,390
functions and whatnot I mean it's a

1519
00:59:40,390 --> 00:59:44,229
really cool language right now what I do

1520
00:59:44,229 --> 00:59:45,969
want to spend time on but I want to

1521
00:59:45,969 --> 00:59:47,709
switch to the other slides is this actor

1522
00:59:47,709 --> 00:59:50,109
stuff okay this is much more important

1523
00:59:50,109 --> 00:59:51,789
for me in this class right so hopefully

1524
00:59:51,789 --> 00:59:53,289
you're exciting about Scarl you're gonna

1525
00:59:53,289 --> 00:59:55,299
look go learn Scala look at all kinds of

1526
00:59:55,299 --> 00:59:58,269
cool examples okay let's switch to

1527
00:59:58,269 --> 01:00:00,729
actors and we're gonna see more examples

1528
01:00:00,729 --> 01:00:02,589
of those case classes which are very

1529
01:00:02,589 --> 01:00:06,959
interesting okay

1530
01:00:06,959 --> 01:00:11,799
all right so actors so I may give you a

1531
01:00:11,799 --> 01:00:13,299
better introduction to this actors I

1532
01:00:13,299 --> 01:00:14,979
mean it's exactly what I have on these

1533
01:00:14,979 --> 01:00:16,390
slides because this guy already thought

1534
01:00:16,390 --> 01:00:18,309
about it some good introduction okay

1535
01:00:18,309 --> 01:00:20,199
right so this is kind of the best

1536
01:00:20,199 --> 01:00:24,249
introduction of them all so this class

1537
01:00:24,249 --> 01:00:28,239
is about distributed systems right one

1538
01:00:28,239 --> 01:00:30,159
thing that I suggested and I want to

1539
01:00:30,159 --> 01:00:33,209
follow up on it throughout the class is

1540
01:00:33,209 --> 01:00:35,529
even if you just have multiple cores in

1541
01:00:35,529 --> 01:00:36,909
a single machine you might as well call

1542
01:00:36,909 --> 01:00:38,919
it a distributed system and this sounds

1543
01:00:38,919 --> 01:00:40,269
a little bit like hey what are we

1544
01:00:40,269 --> 01:00:41,409
talking about the super systems are

1545
01:00:41,409 --> 01:00:42,699
about networking and going to other

1546
01:00:42,699 --> 01:00:44,109
machines and doing stuff like this but

1547
01:00:44,109 --> 01:00:46,839
if you do treat a multi-core machine

1548
01:00:46,839 --> 01:00:48,009
like a disability system you're gonna

1549
01:00:48,009 --> 01:00:49,209
get much better performance of that

1550
01:00:49,209 --> 01:00:53,890
multi-core machine alright now this is

1551
01:00:53,890 --> 01:00:55,599
very hard to be intuitive because you

1552
01:00:55,599 --> 01:00:58,059
have the feeling that hey if I work on

1553
01:00:58,059 --> 01:00:59,469
the same machine I could do so much more

1554
01:00:59,469 --> 01:01:00,789
stuff right share the memory everything

1555
01:01:00,789 --> 01:01:02,890
is instantaneous and whatnot but it

1556
01:01:02,890 --> 01:01:04,599
turns out that all of those ways are

1557
01:01:04,599 --> 01:01:08,319
just a very long rope you can use to

1558
01:01:08,319 --> 01:01:09,849
hang yourself in very interesting ways

1559
01:01:09,849 --> 01:01:12,189
right so you get performance but you

1560
01:01:12,189 --> 01:01:15,189
lose almost anything else okay now

1561
01:01:15,189 --> 01:01:17,140
interesting with this actor model will

1562
01:01:17,140 --> 01:01:19,839
allow you to make the Rope much shorter

1563
01:01:19,839 --> 01:01:21,999
to the point that is not usable okay so

1564
01:01:21,999 --> 01:01:23,650
into a logics and the actor mode allows

1565
01:01:23,650 --> 01:01:25,419
you to pretend you don't have all those

1566
01:01:25,419 --> 01:01:27,939
great facilities that are all pitfalls

1567
01:01:27,939 --> 01:01:30,849
and you pretend that you don't have

1568
01:01:30,849 --> 01:01:32,559
share memory and you pretend that you

1569
01:01:32,559 --> 01:01:34,359
have to talk to somebody as if they are

1570
01:01:34,359 --> 01:01:35,799
at the other end of the world even

1571
01:01:35,799 --> 01:01:38,619
though it's simply just another little

1572
01:01:38,619 --> 01:01:39,669
object that lives in the same memory

1573
01:01:39,669 --> 01:01:41,650
space and that turns out to be very

1574
01:01:41,650 --> 01:01:43,749
healthy and produces much better

1575
01:01:43,749 --> 01:01:45,969
programs and to a large extent produces

1576
01:01:45,969 --> 01:01:47,199
more efficient programs I'll come back

1577
01:01:47,199 --> 01:01:48,599
to this later in class

1578
01:01:48,599 --> 01:01:50,800
efficiency it's a very tricky business

1579
01:01:50,800 --> 01:01:53,369
in the database implementation class

1580
01:01:53,369 --> 01:01:56,250
talked extensively about it

1581
01:01:56,250 --> 01:01:58,660
smartness it's actually in the way of

1582
01:01:58,660 --> 01:02:00,220
efficiency almost all the time on modern

1583
01:02:00,220 --> 01:02:01,450
architectures okay I'm not going to go

1584
01:02:01,450 --> 01:02:03,460
into all the details but the actor model

1585
01:02:03,460 --> 01:02:04,990
can actually produce very efficient code

1586
01:02:04,990 --> 01:02:06,550
you're going to convince yourself by

1587
01:02:06,550 --> 01:02:07,990
doing the first project that this is

1588
01:02:07,990 --> 01:02:11,980
true okay so let's look at this slide so

1589
01:02:11,980 --> 01:02:15,910
I mean this is really what's going on

1590
01:02:15,910 --> 01:02:19,720
now so my favorite question is how many

1591
01:02:19,720 --> 01:02:23,099
people have a uniprocessor cell phone I

1592
01:02:23,099 --> 01:02:25,359
ask some of these questions maybe last

1593
01:02:25,359 --> 01:02:27,430
time a cell phone that doesn't have at

1594
01:02:27,430 --> 01:02:30,040
least two cores where Samsung has now

1595
01:02:30,040 --> 01:02:31,900
four cores in them and their cell phones

1596
01:02:31,900 --> 01:02:33,910
okay right is it a smart phone maybe a

1597
01:02:33,910 --> 01:02:37,150
very old one I've heard that you can if

1598
01:02:37,150 --> 01:02:39,040
you sign up for a new plan they give you

1599
01:02:39,040 --> 01:02:41,530
a new phone for free now that has two

1600
01:02:41,530 --> 01:02:45,250
cores right so why do we have all this

1601
01:02:45,250 --> 01:02:48,640
course so what's the deal with the

1602
01:02:48,640 --> 01:02:51,550
course who asked for cores in the first

1603
01:02:51,550 --> 01:02:55,599
place right so what happened what what

1604
01:02:55,599 --> 01:02:57,490
happened they ran out of other things to

1605
01:02:57,490 --> 01:02:58,990
do okay

1606
01:02:58,990 --> 01:03:02,160
so how many of you remember the the the

1607
01:03:02,160 --> 01:03:05,829
frequency Wars in which my CPU is faster

1608
01:03:05,829 --> 01:03:07,450
than yours because it has more Giga

1609
01:03:07,450 --> 01:03:10,180
Hertz right you meet your friend on the

1610
01:03:10,180 --> 01:03:11,530
on the street and say how many Giga

1611
01:03:11,530 --> 01:03:15,670
Hertz does your computer have right so

1612
01:03:15,670 --> 01:03:17,770
how many Giga Hertz does your computer

1613
01:03:17,770 --> 01:03:19,329
have how many people know how many Giga

1614
01:03:19,329 --> 01:03:24,020
Hertz their computer has so Rafi

1615
01:03:24,020 --> 01:03:27,650
I took points 2.6 now a 2.6 gigahertz

1616
01:03:27,650 --> 01:03:31,940
processor popped up in about 2003 right

1617
01:03:31,940 --> 01:03:34,010
now if you look at the frequency before

1618
01:03:34,010 --> 01:03:38,720
it was going very nicely up anybody has

1619
01:03:38,720 --> 01:03:41,600
any idea in 85 when the 386 was

1620
01:03:41,600 --> 01:03:43,010
introduced what was the frequency at

1621
01:03:43,010 --> 01:03:46,700
which 386 was running initially 40

1622
01:03:46,700 --> 01:03:49,340
megahertz megahertz is a thousand times

1623
01:03:49,340 --> 01:03:51,410
smaller than gigahertz right so we are

1624
01:03:51,410 --> 01:03:52,640
talking about a hundred times

1625
01:03:52,640 --> 01:03:55,210
improvement in the clock speed since 85

1626
01:03:55,210 --> 01:03:58,780
it was constantly going up very nicely

1627
01:03:58,780 --> 01:04:01,990
to such a great extent that when

1628
01:04:01,990 --> 01:04:04,040
Microsoft was designing a new operating

1629
01:04:04,040 --> 01:04:05,869
system they were designing for the clock

1630
01:04:05,869 --> 01:04:09,200
frequency two years ahead right it's

1631
01:04:09,200 --> 01:04:11,090
like hey if the frequency is going to

1632
01:04:11,090 --> 01:04:14,240
double anyway so we can have this really

1633
01:04:14,240 --> 01:04:15,740
inefficient operating system that's

1634
01:04:15,740 --> 01:04:18,200
gonna run just right on the frequencies

1635
01:04:18,200 --> 01:04:20,210
two years from now right literally they

1636
01:04:20,210 --> 01:04:22,010
were doing this okay caught up with them

1637
01:04:22,010 --> 01:04:24,650
lately but right so what happened in

1638
01:04:24,650 --> 01:04:27,080
about 2000 to 2003 they ran out of

1639
01:04:27,080 --> 01:04:31,340
gigahertz why who here it's in in EE

1640
01:04:31,340 --> 01:04:34,400
Department so what happened with the

1641
01:04:34,400 --> 01:04:41,690
frequency right so the frequency has a

1642
01:04:41,690 --> 01:04:43,940
nasty habit of not even bothering to go

1643
01:04:43,940 --> 01:04:45,980
linearly with I mean the power

1644
01:04:45,980 --> 01:04:47,300
requirement is not even linear in the

1645
01:04:47,300 --> 01:04:50,180
frequency right and now simply to

1646
01:04:50,180 --> 01:04:52,550
process are too hot right that's the

1647
01:04:52,550 --> 01:04:54,830
problem you will a lot I mean you can do

1648
01:04:54,830 --> 01:04:57,320
how many people have liquid cooling on

1649
01:04:57,320 --> 01:05:02,480
their desktop some gamers do right they

1650
01:05:02,480 --> 01:05:05,420
sell these very fancy stuff in which you

1651
01:05:05,420 --> 01:05:07,250
can know overclock the processor I've

1652
01:05:07,250 --> 01:05:09,440
heard about five gigahertz processors it

1653
01:05:09,440 --> 01:05:10,760
was sold as a two point something

1654
01:05:10,760 --> 01:05:13,340
gigahertz processor and runs at five you

1655
01:05:13,340 --> 01:05:14,869
just put enough cooling and maybe you

1656
01:05:14,869 --> 01:05:17,480
can manage the process all right so good

1657
01:05:17,480 --> 01:05:19,340
that's out the frequency doesn't go out

1658
01:05:19,340 --> 01:05:21,130
because it becomes impossible to control

1659
01:05:21,130 --> 01:05:24,560
the the power dissipation it's not so

1660
01:05:24,560 --> 01:05:26,300
much you can't put enough power we can

1661
01:05:26,300 --> 01:05:32,090
do that okay more than I mean you can

1662
01:05:32,090 --> 01:05:33,980
upgrade your power supply in the worse

1663
01:05:33,980 --> 01:05:37,100
way in the in the worst case right

1664
01:05:37,100 --> 01:05:38,210
any problem is where does all that

1665
01:05:38,210 --> 01:05:39,830
energy goes and that's very problematic

1666
01:05:39,830 --> 01:05:44,480
okay so frequency levels up but Moore's

1667
01:05:44,480 --> 01:05:44,900
law

1668
01:05:44,900 --> 01:05:49,370
who knows what Moore's Law is well the

1669
01:05:49,370 --> 01:05:51,680
founder of Intel made an interesting

1670
01:05:51,680 --> 01:05:53,810
observation that we are going to be busy

1671
01:05:53,810 --> 01:05:55,700
forever because we are gonna be

1672
01:05:55,700 --> 01:05:56,780
sensually double the number of

1673
01:05:56,780 --> 01:05:58,490
processors every 18 member of

1674
01:05:58,490 --> 01:06:00,710
transistors he never said anything about

1675
01:06:00,710 --> 01:06:02,960
the frequency but he was the kicker and

1676
01:06:02,960 --> 01:06:06,350
people can confirm this if at least for

1677
01:06:06,350 --> 01:06:08,210
a while if you put smaller transistors

1678
01:06:08,210 --> 01:06:10,670
you could raise the frequency until the

1679
01:06:10,670 --> 01:06:13,700
power caught up with you okay so that

1680
01:06:13,700 --> 01:06:15,980
was great by the way

1681
01:06:15,980 --> 01:06:19,430
is the so in 2002 the frequency Wars

1682
01:06:19,430 --> 01:06:22,550
stopped the manufacturers even didn't I

1683
01:06:22,550 --> 01:06:24,200
mean stop bothering indicating the

1684
01:06:24,200 --> 01:06:25,490
frequency for a processor and came up

1685
01:06:25,490 --> 01:06:28,210
with other gimmicks to sell them right

1686
01:06:28,210 --> 01:06:31,040
so what happens in 2002 where they have

1687
01:06:31,040 --> 01:06:33,890
a decade well the number of transistors

1688
01:06:33,890 --> 01:06:35,930
kept on going up now for a while they

1689
01:06:35,930 --> 01:06:37,160
try to use the transistors to make the

1690
01:06:37,160 --> 01:06:38,900
processor smarter that gives you

1691
01:06:38,900 --> 01:06:41,270
marginal improvement right you make the

1692
01:06:41,270 --> 01:06:42,830
logic much more complicated you get two

1693
01:06:42,830 --> 01:06:44,390
percent performance improvement nobody

1694
01:06:44,390 --> 01:06:45,800
will gonna know it's gonna notice who's

1695
01:06:45,800 --> 01:06:47,000
gonna buy a processor because there's

1696
01:06:47,000 --> 01:06:48,230
two percent faster than another one

1697
01:06:48,230 --> 01:06:51,110
forget about it so then they started a

1698
01:06:51,110 --> 01:06:53,090
new game which is let's put multiple

1699
01:06:53,090 --> 01:06:55,660
brains multiple cores on the same die

1700
01:06:55,660 --> 01:06:58,460
now we got to the level where on a

1701
01:06:58,460 --> 01:07:00,890
single die well I think Intel can

1702
01:07:00,890 --> 01:07:04,310
squeeze now 10 right 10 cores AMD likes

1703
01:07:04,310 --> 01:07:05,990
to put 8 but likes to put two of those

1704
01:07:05,990 --> 01:07:07,730
dies in a single packaging right so you

1705
01:07:07,730 --> 01:07:09,170
can buy an hour processor that has 16

1706
01:07:09,170 --> 01:07:10,430
cores

1707
01:07:10,430 --> 01:07:12,470
this is for the high-performance

1708
01:07:12,470 --> 01:07:14,080
general-purpose processors if you go to

1709
01:07:14,080 --> 01:07:17,210
Intel 5 they already have a 50 and that

1710
01:07:17,210 --> 01:07:18,470
is a company as somebody mentioned last

1711
01:07:18,470 --> 01:07:21,950
time somebody around MIT that put 64 and

1712
01:07:21,950 --> 01:07:24,500
wants to put some 1024 24 something at

1713
01:07:24,500 --> 01:07:27,500
that or not on a single core okay great

1714
01:07:27,500 --> 01:07:29,300
but what can you do with all of those

1715
01:07:29,300 --> 01:07:33,800
course right so if you took another

1716
01:07:33,800 --> 01:07:35,300
class with me I'm sorry about this joke

1717
01:07:35,300 --> 01:07:40,910
but I in about 2004 5 2006 when the

1718
01:07:40,910 --> 01:07:42,620
multi-core started to pop up dual cores

1719
01:07:42,620 --> 01:07:44,210
quad-core so I heard this commercial on

1720
01:07:44,210 --> 01:07:47,150
radio for why you should buy a four core

1721
01:07:47,150 --> 01:07:50,030
desktop because you can run a web

1722
01:07:50,030 --> 01:07:51,109
browser

1723
01:07:51,109 --> 01:07:54,200
email and something else at the same

1724
01:07:54,200 --> 01:07:55,489
time if that's only three things and you

1725
01:07:55,489 --> 01:07:59,690
have four course actually my my perfect

1726
01:07:59,690 --> 01:08:01,069
application for why you need multiple

1727
01:08:01,069 --> 01:08:04,489
course is because you can have if you

1728
01:08:04,489 --> 01:08:06,170
have any course you can have n minus one

1729
01:08:06,170 --> 01:08:08,180
infinite loops going on in parallel and

1730
01:08:08,180 --> 01:08:09,650
that's one is left to do some real work

1731
01:08:09,650 --> 01:08:11,269
of course you don't need multiple course

1732
01:08:11,269 --> 01:08:13,190
to do concurrency and we're going to

1733
01:08:13,190 --> 01:08:15,229
talk about that right but the fact of

1734
01:08:15,229 --> 01:08:17,210
the matter is very few applications use

1735
01:08:17,210 --> 01:08:20,299
multiple cores in any reasonable way you

1736
01:08:20,299 --> 01:08:21,770
can buy an eight core processor and it

1737
01:08:21,770 --> 01:08:23,658
still doesn't run faster than one core

1738
01:08:23,658 --> 01:08:25,069
processor when it runs the word

1739
01:08:25,069 --> 01:08:27,500
processor or something it still slow

1740
01:08:27,500 --> 01:08:29,920
when it does some of the things right

1741
01:08:29,920 --> 01:08:32,270
that so why is that the case

1742
01:08:32,270 --> 01:08:34,279
we don't have smart programmers well the

1743
01:08:34,279 --> 01:08:38,080
problem is the abstraction okay we had

1744
01:08:38,080 --> 01:08:41,988
multi-threading since the 70s but it's

1745
01:08:41,988 --> 01:08:43,189
very hard to program against the

1746
01:08:43,189 --> 01:08:44,719
multi-threading now something happened

1747
01:08:44,719 --> 01:08:46,580
and something that happened by the way

1748
01:08:46,580 --> 01:08:48,738
this is an adult's law and it's famous

1749
01:08:48,738 --> 01:08:51,408
for a limiting factor on how much speed

1750
01:08:51,408 --> 01:08:52,849
up you can get depending on how many

1751
01:08:52,849 --> 01:08:54,408
processors you have right so these

1752
01:08:54,408 --> 01:08:56,929
curves are percent how big the speed-up

1753
01:08:56,929 --> 01:08:59,479
is the theoretical speed-up in terms of

1754
01:08:59,479 --> 01:09:01,969
the number of processors in terms of how

1755
01:09:01,969 --> 01:09:04,130
much parallelism do you have in your

1756
01:09:04,130 --> 01:09:05,899
code so if you have only 50% parallelism

1757
01:09:05,899 --> 01:09:09,189
in your code then you reach maximum

1758
01:09:09,189 --> 01:09:11,238
performance at eight cores anything

1759
01:09:11,238 --> 01:09:12,738
beyond a course even if you have perfect

1760
01:09:12,738 --> 01:09:14,299
communication it's out of the window

1761
01:09:14,299 --> 01:09:17,868
right even at 95% parallel code you're

1762
01:09:17,868 --> 01:09:19,429
still limited at the factor of twenty

1763
01:09:19,429 --> 01:09:21,549
twenty in scalability even if you have

1764
01:09:21,549 --> 01:09:24,020
65,000 course so it's really really bad

1765
01:09:24,020 --> 01:09:25,939
okay so you need to achieve much higher

1766
01:09:25,939 --> 01:09:30,429
than 95% parallelism in your code okay

1767
01:09:30,429 --> 01:09:32,738
all right

1768
01:09:32,738 --> 01:09:35,330
parallelism is hard in any language race

1769
01:09:35,330 --> 01:09:36,920
conditions are truly awful it doesn't

1770
01:09:36,920 --> 01:09:38,089
matter how many gizmos you add to the

1771
01:09:38,089 --> 01:09:39,469
language is still very very hard and

1772
01:09:39,469 --> 01:09:41,330
it's very hard because it's very hard to

1773
01:09:41,330 --> 01:09:44,118
reason about it if you write a homework

1774
01:09:44,118 --> 01:09:45,979
assignment in which you say hey I have

1775
01:09:45,979 --> 01:09:47,420
three threads and we do something with

1776
01:09:47,420 --> 01:09:49,488
three friends hey that looks nice but a

1777
01:09:49,488 --> 01:09:50,929
real application might have something

1778
01:09:50,929 --> 01:09:52,368
like 400 threads I have for example

1779
01:09:52,368 --> 01:09:54,109
database engine that likes to throw out

1780
01:09:54,109 --> 01:09:56,540
400 threads now how do you think about

1781
01:09:56,540 --> 01:09:59,030
the interaction between 400 friends what

1782
01:09:59,030 --> 01:10:00,980
do those guys do if you're looking at

1783
01:10:00,980 --> 01:10:02,540
mutual exclusion regions in a normal

1784
01:10:02,540 --> 01:10:03,349
sense and so on

1785
01:10:03,349 --> 01:10:06,170
you can't tell what the system does the

1786
01:10:06,170 --> 01:10:07,610
actual model makes it much much much

1787
01:10:07,610 --> 01:10:12,710
better all right how well these are the

1788
01:10:12,710 --> 01:10:18,349
pitfalls well it allows you to have a

1789
01:10:18,349 --> 01:10:20,230
very intuitive way to think about things

1790
01:10:20,230 --> 01:10:23,869
you pretend that multiple pieces of the

1791
01:10:23,869 --> 01:10:25,310
code cannot talk directly to each other

1792
01:10:25,310 --> 01:10:26,989
they have to do it through a formal

1793
01:10:26,989 --> 01:10:27,590
mechanism

1794
01:10:27,590 --> 01:10:30,590
it's called messaging right and each of

1795
01:10:30,590 --> 01:10:32,690
them have a life of their own they live

1796
01:10:32,690 --> 01:10:34,699
on their own and they interact on their

1797
01:10:34,699 --> 01:10:36,739
own and all you have to do is specify

1798
01:10:36,739 --> 01:10:38,540
what is it that they do it's as if you

1799
01:10:38,540 --> 01:10:40,760
write the script for actors right you

1800
01:10:40,760 --> 01:10:43,369
say hey you only have to worry about

1801
01:10:43,369 --> 01:10:44,929
these are the two actors and if you get

1802
01:10:44,929 --> 01:10:46,670
a message from this one you do this and

1803
01:10:46,670 --> 01:10:48,290
maybe you send a message to this guy and

1804
01:10:48,290 --> 01:10:50,000
you don't hear about all the other 400

1805
01:10:50,000 --> 01:10:52,639
actors do you just watch your own little

1806
01:10:52,639 --> 01:10:54,739
world and then it's much much easier to

1807
01:10:54,739 --> 01:10:56,540
write very complex software in this way

1808
01:10:56,540 --> 01:10:57,710
because you just write small

1809
01:10:57,710 --> 01:10:59,420
compartments you're going to write the

1810
01:10:59,420 --> 01:11:01,040
behavior of all of these separate actors

1811
01:11:01,040 --> 01:11:05,929
okay the actor model helps another thing

1812
01:11:05,929 --> 01:11:07,070
that helps in a big way is this

1813
01:11:07,070 --> 01:11:08,650
immutable data structures

1814
01:11:08,650 --> 01:11:11,570
okay so immutable data structures as I

1815
01:11:11,570 --> 01:11:12,980
mentioned before is involved in the

1816
01:11:12,980 --> 01:11:14,300
language in Scala means you're not

1817
01:11:14,300 --> 01:11:15,860
allowed to change the data structure you

1818
01:11:15,860 --> 01:11:17,210
can build out the data structures out of

1819
01:11:17,210 --> 01:11:19,310
it but they cannot be changed you can

1820
01:11:19,310 --> 01:11:20,420
actually change them but you have to

1821
01:11:20,420 --> 01:11:22,190
then break the part and reconstruct it

1822
01:11:22,190 --> 01:11:24,260
for some for somebody also to change it

1823
01:11:24,260 --> 01:11:26,690
these immutable data types

1824
01:11:26,690 --> 01:11:29,570
by the way appeared about at the same

1825
01:11:29,570 --> 01:11:32,540
time as the mutable ones right two of

1826
01:11:32,540 --> 01:11:36,050
the first languages were Fortran which

1827
01:11:36,050 --> 01:11:40,040
is the the the parent language of all

1828
01:11:40,040 --> 01:11:42,320
imperative languages and Lisp which is a

1829
01:11:42,320 --> 01:11:43,610
parent language with all the functional

1830
01:11:43,610 --> 01:11:46,610
languages lease only had immutable data

1831
01:11:46,610 --> 01:11:48,949
structures initially and then they

1832
01:11:48,949 --> 01:11:50,329
started to add a little bit of mutable

1833
01:11:50,329 --> 01:11:51,940
there because people started to cry

1834
01:11:51,940 --> 01:11:54,380
fortune had only mutable data structure

1835
01:11:54,380 --> 01:11:55,849
everything was a location in memory and

1836
01:11:55,849 --> 01:11:57,020
could do whatever you wanted with it

1837
01:11:57,020 --> 01:11:59,570
right so very very different approaches

1838
01:11:59,570 --> 01:12:02,150
but immutable means you don't have to

1839
01:12:02,150 --> 01:12:02,810
protect them when you have

1840
01:12:02,810 --> 01:12:05,060
multi-threading now you do want to allow

1841
01:12:05,060 --> 01:12:06,980
mutable but in an actor/model

1842
01:12:06,980 --> 01:12:10,849
to do so you can do so cleanly by saying

1843
01:12:10,849 --> 01:12:12,349
anything that's mutable belongs to a

1844
01:12:12,349 --> 01:12:14,389
single actor now there is a variation on

1845
01:12:14,389 --> 01:12:16,080
the actor model that I use in my Odette

1846
01:12:16,080 --> 01:12:17,840
engine in which you add something called

1847
01:12:17,840 --> 01:12:20,240
swapping paradigm which I will describe

1848
01:12:20,240 --> 01:12:22,320
maybe next time just to give you an idea

1849
01:12:22,320 --> 01:12:24,570
of what can be done in which you can

1850
01:12:24,570 --> 01:12:26,550
have mutable data structures and still

1851
01:12:26,550 --> 01:12:30,720
not have concurrency issues right but

1852
01:12:30,720 --> 01:12:33,000
I'm gonna go into it right now but it

1853
01:12:33,000 --> 01:12:35,010
creates other limitations but not big

1854
01:12:35,010 --> 01:12:36,060
limitation none of them are big

1855
01:12:36,060 --> 01:12:37,380
limitations but it takes a certain

1856
01:12:37,380 --> 01:12:41,160
discipline okay right so actor model was

1857
01:12:41,160 --> 01:12:46,050
formalized in 1973 but I mean this is

1858
01:12:46,050 --> 01:12:47,370
the problem with mathematicians I'm

1859
01:12:47,370 --> 01:12:50,130
sorry but when they formalize something

1860
01:12:50,130 --> 01:12:51,390
they're really formalized something so

1861
01:12:51,390 --> 01:12:53,040
the original actor model essentially

1862
01:12:53,040 --> 01:12:55,560
said hey if you have actors you don't

1863
01:12:55,560 --> 01:12:58,070
need anything else including for loops

1864
01:12:58,070 --> 01:13:00,510
so then an interesting question was how

1865
01:13:00,510 --> 01:13:02,340
do you do for loops using a pure actor

1866
01:13:02,340 --> 01:13:06,210
model well you do this you have an actor

1867
01:13:06,210 --> 01:13:09,390
that runs the current iteration and then

1868
01:13:09,390 --> 01:13:11,160
creates another actor that's gonna run

1869
01:13:11,160 --> 01:13:12,420
the next iteration which creates another

1870
01:13:12,420 --> 01:13:14,100
actor that runs the next iteration this

1871
01:13:14,100 --> 01:13:16,200
is insane this is not the way to use the

1872
01:13:16,200 --> 01:13:17,760
actor model so what's good about our

1873
01:13:17,760 --> 01:13:19,350
model is the fact that it's a nice

1874
01:13:19,350 --> 01:13:21,090
abstraction you have this independent

1875
01:13:21,090 --> 01:13:23,280
entity that can exchange messages with

1876
01:13:23,280 --> 01:13:25,170
other things what's bad about the

1877
01:13:25,170 --> 01:13:27,150
original abstract model is that it went

1878
01:13:27,150 --> 01:13:29,580
all the way and it can't be implemented

1879
01:13:29,580 --> 01:13:31,890
in an efficient way so you want to have

1880
01:13:31,890 --> 01:13:34,560
imperative behavior within the actor but

1881
01:13:34,560 --> 01:13:37,140
only this message exchanges for the

1882
01:13:37,140 --> 01:13:38,670
internet the actors to interact with

1883
01:13:38,670 --> 01:13:42,210
each other okay well our people came

1884
01:13:42,210 --> 01:13:44,700
along right girl aha right in mid 80s

1885
01:13:44,700 --> 01:13:46,050
and said that's insane let's do a more

1886
01:13:46,050 --> 01:13:48,750
reasonable language and the a design or

1887
01:13:48,750 --> 01:13:51,690
lank okay our link was designed in 1980s

1888
01:13:51,690 --> 01:13:56,340
and by the way our land is heavily based

1889
01:13:56,340 --> 01:13:57,120
on Prolog

1890
01:13:57,120 --> 01:14:00,600
it's basically Prolog cross actors and

1891
01:14:00,600 --> 01:14:01,650
introduce is very very lightweight

1892
01:14:01,650 --> 01:14:04,890
actors and Erickson right at the

1893
01:14:04,890 --> 01:14:06,150
research group at Ericsson designed this

1894
01:14:06,150 --> 01:14:08,670
Erickson said hey let's use this to

1895
01:14:08,670 --> 01:14:12,840
write telecom code well they pull of

1896
01:14:12,840 --> 01:14:14,850
something amazing right so they design a

1897
01:14:14,850 --> 01:14:20,850
system that has 99.999999% uptime with

1898
01:14:20,850 --> 01:14:24,210
actors now if that doesn't show to the

1899
01:14:24,210 --> 01:14:25,920
world that actors are valuable I don't

1900
01:14:25,920 --> 01:14:28,770
know what is how did they achieve that

1901
01:14:28,770 --> 01:14:30,570
well there are two ways to achieve that

1902
01:14:30,570 --> 01:14:33,270
is to have people that write code that

1903
01:14:33,270 --> 01:14:36,180
has absolutely no bugs

1904
01:14:36,180 --> 01:14:38,760
well they don't exist so you can dismiss

1905
01:14:38,760 --> 01:14:41,550
that or to write code that fixes itself

1906
01:14:41,550 --> 01:14:44,520
now imagine

1907
01:14:44,520 --> 01:14:47,340
what does fixes itself mean well in

1908
01:14:47,340 --> 01:14:48,690
general to write code that repairs

1909
01:14:48,690 --> 01:14:53,310
itself it's such a complex thing you

1910
01:14:53,310 --> 01:14:54,570
don't even know where to start but it

1911
01:14:54,570 --> 01:14:55,800
turns out that it's relatively easy with

1912
01:14:55,800 --> 01:14:58,140
actors so the approach with Ericsson

1913
01:14:58,140 --> 01:14:59,940
took with actors is the following hey

1914
01:14:59,940 --> 01:15:02,700
we're gonna create low-level actors that

1915
01:15:02,700 --> 01:15:05,340
do the work then supervisors actors are

1916
01:15:05,340 --> 01:15:06,989
just watch how these guys do the work

1917
01:15:06,989 --> 01:15:09,630
and in particular monitor these guys if

1918
01:15:09,630 --> 01:15:12,230
one of them notice is that an actor died

1919
01:15:12,230 --> 01:15:15,170
creates another actor in place right

1920
01:15:15,170 --> 01:15:18,900
in fact this mimics the way the humans

1921
01:15:18,900 --> 01:15:20,340
organized and in large-scale activity

1922
01:15:20,340 --> 01:15:24,239
right writing perfect code is equivalent

1923
01:15:24,239 --> 01:15:25,800
to just having workers in a factory

1924
01:15:25,800 --> 01:15:27,330
nobody's supervising them and doing

1925
01:15:27,330 --> 01:15:28,710
anything and none of them ever getting

1926
01:15:28,710 --> 01:15:30,270
sick and everything just working it's

1927
01:15:30,270 --> 01:15:31,830
not happening it's not happening for

1928
01:15:31,830 --> 01:15:33,660
many many reasons has nothing to do with

1929
01:15:33,660 --> 01:15:36,150
how perfect the code is the machine can

1930
01:15:36,150 --> 01:15:38,100
run amok because an alpha particle hits

1931
01:15:38,100 --> 01:15:40,170
a memory location and flipped a bit I

1932
01:15:40,170 --> 01:15:42,030
mean things happen power goes on all

1933
01:15:42,030 --> 01:15:44,160
kinds of things like this right so the

1934
01:15:44,160 --> 01:15:46,620
actor models approach is really do this

1935
01:15:46,620 --> 01:15:48,960
kind of do work and monitor but you can

1936
01:15:48,960 --> 01:15:51,270
have Watchers for the Watchers right so

1937
01:15:51,270 --> 01:15:53,219
supervisors supervise Mahler groups that

1938
01:15:53,219 --> 01:15:54,660
have supervises large groups either

1939
01:15:54,660 --> 01:15:56,190
groups and maybe a committee at the top

1940
01:15:56,190 --> 01:15:59,700
so that even if half the members of the

1941
01:15:59,700 --> 01:16:01,230
committee die at the same time the

1942
01:16:01,230 --> 01:16:02,969
committee is still working by the way

1943
01:16:02,969 --> 01:16:04,230
this tribute systems are a lot about

1944
01:16:04,230 --> 01:16:06,930
these things so the core principles and

1945
01:16:06,930 --> 01:16:08,640
the core ideas in distributed systems

1946
01:16:08,640 --> 01:16:10,560
how you make things failure resistant

1947
01:16:10,560 --> 01:16:12,960
and so on are very similar to what

1948
01:16:12,960 --> 01:16:14,550
Erikson did with the actors so in a

1949
01:16:14,550 --> 01:16:16,290
natural way this actors match any

1950
01:16:16,290 --> 01:16:19,140
endeavor in distributed systems you want

1951
01:16:19,140 --> 01:16:21,090
to have somebody watching what

1952
01:16:21,090 --> 01:16:22,620
particular processes do hey create an

1953
01:16:22,620 --> 01:16:24,210
attractor that just watches them what

1954
01:16:24,210 --> 01:16:25,980
does it mean to watch another actor how

1955
01:16:25,980 --> 01:16:30,000
do you watch another actor you could ask

1956
01:16:30,000 --> 01:16:33,600
the actor to report regularly and if it

1957
01:16:33,600 --> 01:16:36,660
doesn't you say send some more brutal

1958
01:16:36,660 --> 01:16:39,480
messages and in the end you say hey it

1959
01:16:39,480 --> 01:16:41,739
died or something right

1960
01:16:41,739 --> 01:16:43,210
I'm talking sensitive all these kind of

1961
01:16:43,210 --> 01:16:44,499
techniques but the important thing is

1962
01:16:44,499 --> 01:16:46,059
the active model has the means to do so

1963
01:16:46,059 --> 01:16:49,510
right so for example if a supervisor you

1964
01:16:49,510 --> 01:16:52,539
keep track I supervised ten actors and I

1965
01:16:52,539 --> 01:16:53,679
keep track what's the last time I heard

1966
01:16:53,679 --> 01:16:55,989
from them if I have more than half a

1967
01:16:55,989 --> 01:16:58,150
second since hearing from them I'm gonna

1968
01:16:58,150 --> 01:17:01,079
panic and start doing something right

1969
01:17:01,079 --> 01:17:04,659
you then the actor that's supposed to do

1970
01:17:04,659 --> 01:17:06,130
some work well it's supposed to

1971
01:17:06,130 --> 01:17:09,309
regularly send messages right why half a

1972
01:17:09,309 --> 01:17:10,719
second because computers are fast

1973
01:17:10,719 --> 01:17:12,159
you must usually report only once a day

1974
01:17:12,159 --> 01:17:19,269
yes right so good so let's talk a little

1975
01:17:19,269 --> 01:17:22,139
bit about this even at the expense of

1976
01:17:22,139 --> 01:17:24,429
yeah I will finish this it's not too

1977
01:17:24,429 --> 01:17:26,650
many slides to go from right so whenever

1978
01:17:26,650 --> 01:17:27,670
you're talking about fancy features

1979
01:17:27,670 --> 01:17:29,139
abstractions or whatever you want to

1980
01:17:29,139 --> 01:17:30,699
call them you say whoa whoa whoa is

1981
01:17:30,699 --> 01:17:35,380
gonna be slow okay that's always a

1982
01:17:35,380 --> 01:17:37,719
legitimate concern and definitely if

1983
01:17:37,719 --> 01:17:39,610
you're writing for loops using actors

1984
01:17:39,610 --> 01:17:40,719
then you should be worried about

1985
01:17:40,719 --> 01:17:44,949
slowness but in your first assignment

1986
01:17:44,949 --> 01:17:47,769
I'm gonna essentially ask you to

1987
01:17:47,769 --> 01:17:50,110
determine what the good break point is

1988
01:17:50,110 --> 01:17:51,789
so let's imagine the following situation

1989
01:17:51,789 --> 01:17:54,789
that I have workers and this really

1990
01:17:54,789 --> 01:17:56,440
describes your first time you have

1991
01:17:56,440 --> 01:17:58,239
workers and I have the supervisor the

1992
01:17:58,239 --> 01:18:00,460
supervisor basically the role of the

1993
01:18:00,460 --> 01:18:02,949
supervisor is going to be just to give

1994
01:18:02,949 --> 01:18:05,380
work to the workers maybe monitor them

1995
01:18:05,380 --> 01:18:06,909
but definitely say right we need to

1996
01:18:06,909 --> 01:18:08,230
accomplish something big for example

1997
01:18:08,230 --> 01:18:10,119
let's suppose we want to break

1998
01:18:10,119 --> 01:18:14,349
encryption keys right if we want to

1999
01:18:14,349 --> 01:18:16,179
break encryption keys let's think about

2000
01:18:16,179 --> 01:18:16,599
this

2001
01:18:16,599 --> 01:18:18,820
we want to break encryption keys well we

2002
01:18:18,820 --> 01:18:20,650
first need some workers that when given

2003
01:18:20,650 --> 01:18:22,840
a candidate key and maybe some kind of

2004
01:18:22,840 --> 01:18:27,460
hash tries to figure out if this key

2005
01:18:27,460 --> 01:18:29,440
corresponds to this hash and if yes hey

2006
01:18:29,440 --> 01:18:30,519
we have a match that's a brute-force

2007
01:18:30,519 --> 01:18:34,449
attack on security all right so one

2008
01:18:34,449 --> 01:18:35,949
specific thing you can encode in one of

2009
01:18:35,949 --> 01:18:37,539
these actors is hey I'll send you a

2010
01:18:37,539 --> 01:18:40,119
message with a key and a hash and you

2011
01:18:40,119 --> 01:18:43,019
compute whether the key matches the hash

2012
01:18:43,019 --> 01:18:47,559
now if that's the case what do you think

2013
01:18:47,559 --> 01:18:50,619
the code is gonna run fast or not well

2014
01:18:50,619 --> 01:18:52,150
let's put some numbers in there so

2015
01:18:52,150 --> 01:18:54,460
suppose in order to determine if a key

2016
01:18:54,460 --> 01:18:58,860
matches a house I need a microsecond

2017
01:18:58,920 --> 01:19:02,170
right now then you start having asking

2018
01:19:02,170 --> 01:19:03,760
questions like this right if it takes a

2019
01:19:03,760 --> 01:19:06,940
microsecond that means every worker can

2020
01:19:06,940 --> 01:19:08,680
do a million of them in a second

2021
01:19:08,680 --> 01:19:12,070
right now let's say we have 16 workers I

2022
01:19:12,070 --> 01:19:14,350
have 16 cores it better be the case that

2023
01:19:14,350 --> 01:19:15,730
you can believe are 16 million messages

2024
01:19:15,730 --> 01:19:17,980
per second even not given any other

2025
01:19:17,980 --> 01:19:20,110
overhead because otherwise the

2026
01:19:20,110 --> 01:19:21,610
bottleneck is going to be distributing

2027
01:19:21,610 --> 01:19:23,920
messages so it's going to start looking

2028
01:19:23,920 --> 01:19:25,630
not so good if you go to low-level and

2029
01:19:25,630 --> 01:19:27,100
by the way a microsecond is very

2030
01:19:27,100 --> 01:19:28,630
generous for breaking one of those kids

2031
01:19:28,630 --> 01:19:31,150
you can actually computes of those

2032
01:19:31,150 --> 01:19:33,130
hashes much much much faster and then

2033
01:19:33,130 --> 01:19:36,460
suddenly you're in trouble right but to

2034
01:19:36,460 --> 01:19:38,320
a large extent that's a very naive way

2035
01:19:38,320 --> 01:19:40,719
to do to do the work I mean imagine as

2036
01:19:40,719 --> 01:19:43,780
if right so this is the the best way to

2037
01:19:43,780 --> 01:19:45,370
realize whether you're doing the right

2038
01:19:45,370 --> 01:19:48,010
thing with the actual model or not how

2039
01:19:48,010 --> 01:19:49,840
many of you know about how the logarithm

2040
01:19:49,840 --> 01:19:53,080
tables were computed in 1600 swen knob

2041
01:19:53,080 --> 01:19:54,520
here came up with the idea that you can

2042
01:19:54,520 --> 01:19:58,920
do multiplication using addition right

2043
01:19:58,920 --> 01:20:02,469
well they were done by hand right I mean

2044
01:20:02,469 --> 01:20:05,950
they had people to compute the tables in

2045
01:20:05,950 --> 01:20:07,750
the 19th century they literally were

2046
01:20:07,750 --> 01:20:10,840
employing up to 10,000 people computing

2047
01:20:10,840 --> 01:20:13,180
by hand and with sliding not sliding

2048
01:20:13,180 --> 01:20:14,620
rules that was already an amazing

2049
01:20:14,620 --> 01:20:16,420
invention right and that's a 20th

2050
01:20:16,420 --> 01:20:18,969
century invention as abacus and by hand

2051
01:20:18,969 --> 01:20:21,880
right so imagine now you have 10,000

2052
01:20:21,880 --> 01:20:24,880
people computing by hand and but each of

2053
01:20:24,880 --> 01:20:28,989
them for example they were employing the

2054
01:20:28,989 --> 01:20:30,850
so called savants right people that had

2055
01:20:30,850 --> 01:20:32,980
very bad social skills or nonexistence

2056
01:20:32,980 --> 01:20:34,210
horses kids but they could do

2057
01:20:34,210 --> 01:20:36,430
mathematical computations right an app

2058
01:20:36,430 --> 01:20:38,170
here actually use of a famous event to

2059
01:20:38,170 --> 01:20:39,310
actually compute these tables it took

2060
01:20:39,310 --> 01:20:41,020
only 20 years for nap here for computing

2061
01:20:41,020 --> 01:20:42,460
tables because he only had one seven

2062
01:20:42,460 --> 01:20:47,560
right so you're the boss of all of these

2063
01:20:47,560 --> 01:20:50,800
people and you have to say okay when

2064
01:20:50,800 --> 01:20:53,020
you're done with your computation raise

2065
01:20:53,020 --> 01:20:54,460
your hand let me in send me a message

2066
01:20:54,460 --> 01:20:56,860
and say I want one more you have to send

2067
01:20:56,860 --> 01:20:58,510
me back the previous result because I'm

2068
01:20:58,510 --> 01:21:00,340
gonna put it nicely in a table right and

2069
01:21:00,340 --> 01:21:01,750
then you raise your hand and I'll send

2070
01:21:01,750 --> 01:21:02,680
you not another one

2071
01:21:02,680 --> 01:21:04,420
now if everybody finishes in one second

2072
01:21:04,420 --> 01:21:06,219
human scale right not one second let's

2073
01:21:06,219 --> 01:21:07,540
say five seconds

2074
01:21:07,540 --> 01:21:09,280
you have fast people they compute one of

2075
01:21:09,280 --> 01:21:11,200
those entries in five seconds you have

2076
01:21:11,200 --> 01:21:13,900
10,000 of them are you go insane by just

2077
01:21:13,900 --> 01:21:15,489
distributing a message to those guys I

2078
01:21:15,489 --> 01:21:16,750
mean it'd have to be a really fast human

2079
01:21:16,750 --> 01:21:18,010
being to deal with it even if you put a

2080
01:21:18,010 --> 01:21:19,660
minute 20,000 people it's gonna be a

2081
01:21:19,660 --> 01:21:21,790
disaster right so how would you organize

2082
01:21:21,790 --> 01:21:23,950
is at the level of accomplishing this

2083
01:21:23,950 --> 01:21:26,410
with human beings well first of all you

2084
01:21:26,410 --> 01:21:28,840
wouldn't have an overall boss right you

2085
01:21:28,840 --> 01:21:31,480
would have separate departments maybe or

2086
01:21:31,480 --> 01:21:34,000
separate groups a boss over each group

2087
01:21:34,000 --> 01:21:35,920
but even a boss itself even if you're

2088
01:21:35,920 --> 01:21:37,870
watching only ten people you say hey are

2089
01:21:37,870 --> 01:21:39,760
not going to give you a computation to

2090
01:21:39,760 --> 01:21:41,380
do I'll give you a block of computations

2091
01:21:41,380 --> 01:21:44,830
is hey you're responsible for whatever

2092
01:21:44,830 --> 01:21:48,160
number of million 25 to a million 105

2093
01:21:48,160 --> 01:21:52,540
right when you do that bother me that

2094
01:21:52,540 --> 01:21:55,270
way more work gets done messages are

2095
01:21:55,270 --> 01:21:58,510
bigger overhead gets cut cut down so the

2096
01:21:58,510 --> 01:22:00,100
answer model is not the solution for

2097
01:22:00,100 --> 01:22:02,350
everything but by sizing things in the

2098
01:22:02,350 --> 01:22:04,239
right way and then using the actor model

2099
01:22:04,239 --> 01:22:06,100
you can find a sweet spot so that

2100
01:22:06,100 --> 01:22:07,690
actually things were very very nicely

2101
01:22:07,690 --> 01:22:11,739
so the idea maybe would be if you ask an

2102
01:22:11,739 --> 01:22:14,739
actor an actor to do some work think

2103
01:22:14,739 --> 01:22:16,870
that it has to do about a millisecond

2104
01:22:16,870 --> 01:22:18,880
worth of work and if you do so then you

2105
01:22:18,880 --> 01:22:20,440
will probably keep up with the messages

2106
01:22:20,440 --> 01:22:21,940
in fact you can send in the order of

2107
01:22:21,940 --> 01:22:24,250
about 10 million messages in a in a

2108
01:22:24,250 --> 01:22:25,330
system with a very good implementation

2109
01:22:25,330 --> 01:22:30,970
of the actor model now actors are more

2110
01:22:30,970 --> 01:22:33,100
than this right it's not clear at all

2111
01:22:33,100 --> 01:22:34,989
that you need a separate thread for

2112
01:22:34,989 --> 01:22:37,540
every actor and it's probably a bad idea

2113
01:22:37,540 --> 01:22:39,310
to have a thread per actor why are

2114
01:22:39,310 --> 01:22:41,739
threads bad they require of a different

2115
01:22:41,739 --> 01:22:43,390
stack and it's not clear at all that

2116
01:22:43,390 --> 01:22:45,280
these actors will require a full stack

2117
01:22:45,280 --> 01:22:47,530
if you get away without the fools like a

2118
01:22:47,530 --> 01:22:49,510
stack it's a very complex thing when we

2119
01:22:49,510 --> 01:22:51,130
usually have to reserve at least four

2120
01:22:51,130 --> 01:22:52,630
kilo four stack because of the hardware

2121
01:22:52,630 --> 01:22:54,100
and all kinds of complications managing

2122
01:22:54,100 --> 01:22:56,020
the stack is very problematic switching

2123
01:22:56,020 --> 01:22:57,700
frets is very problematic the act of

2124
01:22:57,700 --> 01:22:59,200
switching two frets cannot be done too

2125
01:22:59,200 --> 01:23:01,180
fast you cannot have million actives and

2126
01:23:01,180 --> 01:23:02,050
actually have a fret based

2127
01:23:02,050 --> 01:23:04,320
implementations there is no pure Fred

2128
01:23:04,320 --> 01:23:06,250
implementation in any operating system

2129
01:23:06,250 --> 01:23:07,930
that can scale to millions right

2130
01:23:07,930 --> 01:23:10,810
thousands yes tens of thousands okay but

2131
01:23:10,810 --> 01:23:14,200
not millions okay so but all of all of

2132
01:23:14,200 --> 01:23:15,580
those are implementation issues as long

2133
01:23:15,580 --> 01:23:17,130
as you have the actor model

2134
01:23:17,130 --> 01:23:20,010
okay so what are actors is likely the

2135
01:23:20,010 --> 01:23:21,510
objects right there has some behavior

2136
01:23:21,510 --> 01:23:23,130
that means I see these meshes I send

2137
01:23:23,130 --> 01:23:24,330
these messages and I do some kind of

2138
01:23:24,330 --> 01:23:26,100
computations they have local state they

2139
01:23:26,100 --> 01:23:27,240
never shared the state with anybody

2140
01:23:27,240 --> 01:23:28,650
whatever they have to say goes in a

2141
01:23:28,650 --> 01:23:31,320
message right they have a mailbox now

2142
01:23:31,320 --> 01:23:32,910
this turns out to be extremely important

2143
01:23:32,910 --> 01:23:35,580
because for example especially for the

2144
01:23:35,580 --> 01:23:38,220
boss if it's not fast enough for a few

2145
01:23:38,220 --> 01:23:39,780
milliseconds to deal with the messages

2146
01:23:39,780 --> 01:23:41,190
the messages have to queue up you

2147
01:23:41,190 --> 01:23:43,740
definitely don't want whoever wanted to

2148
01:23:43,740 --> 01:23:47,250
do something to be now stuck because the

2149
01:23:47,250 --> 01:23:48,510
boss cannot take the message especially

2150
01:23:48,510 --> 01:23:51,090
they have more work to do and you can

2151
01:23:51,090 --> 01:23:53,010
design the actor model so it's like this

2152
01:23:53,010 --> 01:23:54,720
it's very important to actually have a

2153
01:23:54,720 --> 01:23:57,330
mailbox so that means there is no need

2154
01:23:57,330 --> 01:24:00,920
for a so-called rendezvous that other

2155
01:24:00,920 --> 01:24:03,300
language had around the who means two

2156
01:24:03,300 --> 01:24:05,100
friends at the same time agree to talk

2157
01:24:05,100 --> 01:24:06,420
and then they exchange some kind of a

2158
01:24:06,420 --> 01:24:07,890
message no these are asynchronous

2159
01:24:07,890 --> 01:24:09,420
messages if you want to send me a

2160
01:24:09,420 --> 01:24:11,280
message that's kind of something cued in

2161
01:24:11,280 --> 01:24:12,990
a mailbox and a local later at the

2162
01:24:12,990 --> 01:24:15,180
mailbox more like the email much faster

2163
01:24:15,180 --> 01:24:18,140
but like the email okay good

2164
01:24:18,140 --> 01:24:20,460
right so this is an example for example

2165
01:24:20,460 --> 01:24:23,910
right so this is one set of actors right

2166
01:24:23,910 --> 01:24:25,290
you can have classes for this actor this

2167
01:24:25,290 --> 01:24:27,320
is one set of actors and send messages

2168
01:24:27,320 --> 01:24:29,460
another message another message how you

2169
01:24:29,460 --> 01:24:30,720
deliver the message how you transport

2170
01:24:30,720 --> 01:24:32,370
them it's a different story now the nice

2171
01:24:32,370 --> 01:24:33,600
thing is once you have these actors

2172
01:24:33,600 --> 01:24:35,130
running on a single machine you can

2173
01:24:35,130 --> 01:24:36,270
actually put them on multiple machines

2174
01:24:36,270 --> 01:24:37,800
as long as you can transport messages

2175
01:24:37,800 --> 01:24:39,900
through some kind of a tcp/ip connection

2176
01:24:39,900 --> 01:24:42,120
you can have a truly distributed system

2177
01:24:42,120 --> 01:24:44,520
with actors and in fact the framework

2178
01:24:44,520 --> 01:24:46,670
I'm going to ask you to use akka akka

2179
01:24:46,670 --> 01:24:51,720
right not only has local actors but in a

2180
01:24:51,720 --> 01:24:53,880
very natural way has remote actors with

2181
01:24:53,880 --> 01:24:55,650
very simple syntax as a yes starting at

2182
01:24:55,650 --> 01:24:57,480
the server on the machine and hey I can

2183
01:24:57,480 --> 01:24:58,740
send message now to actors that are

2184
01:24:58,740 --> 01:25:00,510
remote actors by creating so-called

2185
01:25:00,510 --> 01:25:02,910
proxies right you create essentially a

2186
01:25:02,910 --> 01:25:05,160
local proxy that behaves like an actor

2187
01:25:05,160 --> 01:25:06,330
but in fact relays the message to

2188
01:25:06,330 --> 01:25:08,550
another remote actor so in a

2189
01:25:08,550 --> 01:25:10,470
straightforward way code you write to

2190
01:25:10,470 --> 01:25:12,840
run on your fork or desktop can be

2191
01:25:12,840 --> 01:25:15,060
placed on a hundred machine cluster to

2192
01:25:15,060 --> 01:25:17,670
run using the remote actors with almost

2193
01:25:17,670 --> 01:25:21,450
no change to your code in what languages

2194
01:25:21,450 --> 01:25:23,070
is possible except this kind of

2195
01:25:23,070 --> 01:25:27,290
languages like the Erlang or or Scala

2196
01:25:27,290 --> 01:25:29,360
right

2197
01:25:29,360 --> 01:25:31,730
so classic type of full tolerance as I

2198
01:25:31,730 --> 01:25:33,920
mentioned is supervisor and there is a

2199
01:25:33,920 --> 01:25:36,199
name for this right so a worker dies a

2200
01:25:36,199 --> 01:25:37,969
spawn off another worker you just watch

2201
01:25:37,969 --> 01:25:40,580
them right you can have one for one

2202
01:25:40,580 --> 01:25:42,469
restart strategy one worker dies I start

2203
01:25:42,469 --> 01:25:43,159
another worker

2204
01:25:43,159 --> 01:25:45,290
letting the code die it's a much easier

2205
01:25:45,290 --> 01:25:46,880
way to deal with the problem and this is

2206
01:25:46,880 --> 01:25:48,860
a very very big issue is if you find an

2207
01:25:48,860 --> 01:25:51,170
error in your code or everybody says hey

2208
01:25:51,170 --> 01:25:53,270
you write code that detects errors fine

2209
01:25:53,270 --> 01:25:57,139
you detect errors then what right so you

2210
01:25:57,139 --> 01:25:58,580
try to read the file in you cannot read

2211
01:25:58,580 --> 01:26:01,250
the file what you do now you're part of

2212
01:26:01,250 --> 01:26:03,440
a large database system with the

2213
01:26:03,440 --> 01:26:04,639
favorite way is you crash the whole

2214
01:26:04,639 --> 01:26:06,530
program and say I can't do this right

2215
01:26:06,530 --> 01:26:08,090
it's not gonna work I mean not in a

2216
01:26:08,090 --> 01:26:11,030
telco system that has 99.99999 uptight

2217
01:26:11,030 --> 01:26:17,090
right i so i mention akka actually this

2218
01:26:17,090 --> 01:26:19,400
is done commercially but the code is

2219
01:26:19,400 --> 01:26:20,960
available as open source

2220
01:26:20,960 --> 01:26:22,639
they sell support and this is how they

2221
01:26:22,639 --> 01:26:24,380
survive its type its partner of

2222
01:26:24,380 --> 01:26:26,900
something called type safe stack they

2223
01:26:26,900 --> 01:26:28,070
actually have done much cooler stuff

2224
01:26:28,070 --> 01:26:30,530
right which I'm using them they design

2225
01:26:30,530 --> 01:26:32,929
an entire web design platform is called

2226
01:26:32,929 --> 01:26:34,489
the Play Framework it's written in Scala

2227
01:26:34,489 --> 01:26:36,949
uses ARCA actors and you can design

2228
01:26:36,949 --> 01:26:40,159
large website installations using the

2229
01:26:40,159 --> 01:26:41,750
actors and that would be a nice thing to

2230
01:26:41,750 --> 01:26:43,639
do because when you get an HTTP request

2231
01:26:43,639 --> 01:26:45,409
you can use this remote actors to ask

2232
01:26:45,409 --> 01:26:46,969
different machines to do some work and

2233
01:26:46,969 --> 01:26:48,230
then send the result that all kinds of

2234
01:26:48,230 --> 01:26:52,010
things I guess okay now it's

2235
01:26:52,010 --> 01:26:53,480
implementing in Scala but has Java

2236
01:26:53,480 --> 01:26:55,580
hookups so it's a good implementation

2237
01:26:55,580 --> 01:26:58,820
for Java actors as well alright and they

2238
01:26:58,820 --> 01:26:59,960
have something else called software

2239
01:26:59,960 --> 01:27:01,280
transactional memory which I'm not gonna

2240
01:27:01,280 --> 01:27:04,300
go into and here is some code for this

2241
01:27:04,300 --> 01:27:09,349
okay and this also shows another

2242
01:27:09,349 --> 01:27:11,330
interesting example of this let me see

2243
01:27:11,330 --> 01:27:14,570
if I can magnify this of this case

2244
01:27:14,570 --> 01:27:19,130
classes so I have something like this

2245
01:27:19,130 --> 01:27:22,429
going in my code in C++ but it's a much

2246
01:27:22,429 --> 01:27:24,679
much heavier mechanism so this case

2247
01:27:24,679 --> 01:27:28,429
object increase essentially says there

2248
01:27:28,429 --> 01:27:32,960
is a class called increase I haven't

2249
01:27:32,960 --> 01:27:35,380
specified more about it but I could

2250
01:27:35,380 --> 01:27:38,599
write but this is so if you say case

2251
01:27:38,599 --> 01:27:41,540
that already has default constructor

2252
01:27:41,540 --> 01:27:42,469
generated

2253
01:27:42,469 --> 01:27:44,510
and if all the way to access any member

2254
01:27:44,510 --> 01:27:46,790
that's inside in this case has no memory

2255
01:27:46,790 --> 01:27:50,540
inside by the way classes themselves

2256
01:27:50,540 --> 01:27:52,340
carry information if I send you a

2257
01:27:52,340 --> 01:27:54,530
message and the type of the message is

2258
01:27:54,530 --> 01:27:56,360
increased I already told you what I want

2259
01:27:56,360 --> 01:27:57,710
I want you to increase whatever and this

2260
01:27:57,710 --> 01:27:59,090
is happening with the counter so it

2261
01:27:59,090 --> 01:28:01,130
really need no content for that object I

2262
01:28:01,130 --> 01:28:03,110
just the type of the con the type of the

2263
01:28:03,110 --> 01:28:05,060
class is the message itself in this case

2264
01:28:05,060 --> 01:28:06,800
so I'm going to have two classes

2265
01:28:06,800 --> 01:28:08,540
increase and get count

2266
01:28:08,540 --> 01:28:10,370
I mean it's implementing some sort of a

2267
01:28:10,370 --> 01:28:12,350
counter actor and this is the definition

2268
01:28:12,350 --> 01:28:14,120
of the actor and I have class counter

2269
01:28:14,120 --> 01:28:15,949
extends actor which means has all the

2270
01:28:15,949 --> 01:28:18,560
facilities in actor in particular it's

2271
01:28:18,560 --> 01:28:22,420
gonna can redefine this receive method

2272
01:28:22,420 --> 01:28:25,100
right and this receive is going to be

2273
01:28:25,100 --> 01:28:27,920
called every time a message is delivered

2274
01:28:27,920 --> 01:28:29,570
to the actor and that's what they are

2275
01:28:29,570 --> 01:28:31,460
Khafre more does it's magic happening

2276
01:28:31,460 --> 01:28:34,370
behind the scenes right so what is this

2277
01:28:34,370 --> 01:28:36,260
well it's actually you can define it to

2278
01:28:36,260 --> 01:28:37,820
be a closer and in particularly

2279
01:28:37,820 --> 01:28:40,520
immediately do one of those magic so

2280
01:28:40,520 --> 01:28:42,320
there is a some kind of a magic match

2281
01:28:42,320 --> 01:28:44,480
there right because this is a closed

2282
01:28:44,480 --> 01:28:46,100
receive it's always written like this

2283
01:28:46,100 --> 01:28:47,680
and it's plugged in inside the match

2284
01:28:47,680 --> 01:28:49,550
remember that cold composition will be

2285
01:28:49,550 --> 01:28:53,810
seen with a try so receive it's a closer

2286
01:28:53,810 --> 01:28:55,310
or gets plugged in into a match

2287
01:28:55,310 --> 01:28:57,350
statement and puts this case is inside

2288
01:28:57,350 --> 01:28:59,440
the match so when a message is delivered

2289
01:28:59,440 --> 01:29:02,480
this closure gets to be executed and

2290
01:29:02,480 --> 01:29:04,760
then you have this powerful matching

2291
01:29:04,760 --> 01:29:06,350
that says if the type of the message is

2292
01:29:06,350 --> 01:29:08,630
increased then oh I just increment the

2293
01:29:08,630 --> 01:29:09,860
counter by the way the country is a

2294
01:29:09,860 --> 01:29:13,370
private variable so it's mutable of this

2295
01:29:13,370 --> 01:29:16,400
class then got initialized at 0 if the

2296
01:29:16,400 --> 01:29:19,370
message is get count then I need to

2297
01:29:19,370 --> 01:29:22,100
reply there a nicer way to reply right

2298
01:29:22,100 --> 01:29:23,420
then there's but you could call the

2299
01:29:23,420 --> 01:29:26,540
reply method okay reply it's a built-in

2300
01:29:26,540 --> 01:29:28,610
thing into the ARCA framework that

2301
01:29:28,610 --> 01:29:31,489
essentially says send a message back to

2302
01:29:31,489 --> 01:29:33,380
whoever send me a message so implicitly

2303
01:29:33,380 --> 01:29:35,360
you know who send your message and we

2304
01:29:35,360 --> 01:29:36,500
are going to get back to that in a

2305
01:29:36,500 --> 01:29:40,640
second right and that's it that's my

2306
01:29:40,640 --> 01:29:45,890
counter actor by the way this contract

2307
01:29:45,890 --> 01:29:48,860
now plays to replace the role of a safe

2308
01:29:48,860 --> 01:29:53,300
counter right as long as delivering

2309
01:29:53,300 --> 01:29:55,200
messages

2310
01:29:55,200 --> 01:29:58,230
is tight is Fredd safe so all you need

2311
01:29:58,230 --> 01:30:00,480
to worry about in your in your in your

2312
01:30:00,480 --> 01:30:04,140
actor framework is that delivering

2313
01:30:04,140 --> 01:30:08,730
messages to an actor is Fredd safe you

2314
01:30:08,730 --> 01:30:10,500
don't need them to have shared variables

2315
01:30:10,500 --> 01:30:11,670
that are protected by a mutex

2316
01:30:11,670 --> 01:30:13,380
you simply make an act of The Guardian

2317
01:30:13,380 --> 01:30:14,910
on one of these variables and if you

2318
01:30:14,910 --> 01:30:16,020
need to read the value hey send me a

2319
01:30:16,020 --> 01:30:18,180
message with value now it's not as now

2320
01:30:18,180 --> 01:30:19,830
this is not going to be fantastic if you

2321
01:30:19,830 --> 01:30:21,900
want to do many many many such small

2322
01:30:21,900 --> 01:30:23,670
things so you might want for example the

2323
01:30:23,670 --> 01:30:25,020
actor to be responsible for the large

2324
01:30:25,020 --> 01:30:26,580
hash and not for each entry in the hair

2325
01:30:26,580 --> 01:30:28,590
should be front actor right but it

2326
01:30:28,590 --> 01:30:30,500
provides this interesting separation as

2327
01:30:30,500 --> 01:30:33,900
long as this guy keeps a variable count

2328
01:30:33,900 --> 01:30:35,520
of private nobody has access to it

2329
01:30:35,520 --> 01:30:36,960
except if that sends a message and then

2330
01:30:36,960 --> 01:30:38,400
gets a value you're perfectly fine

2331
01:30:38,400 --> 01:30:40,500
because you have Fred safety on the

2332
01:30:40,500 --> 01:30:42,360
message delivery okay doesn't matter how

2333
01:30:42,360 --> 01:30:46,140
the actor is implemented okay okay so

2334
01:30:46,140 --> 01:30:49,320
how do you create these guys right this

2335
01:30:49,320 --> 01:30:51,090
time the library is import akka actor

2336
01:30:51,090 --> 01:30:54,090
actor this is fancy stuff with libraries

2337
01:30:54,090 --> 01:30:55,650
in Scala which I'm sure you're going to

2338
01:30:55,650 --> 01:30:58,590
enjoy when you read about it right you

2339
01:30:58,590 --> 01:31:00,420
have things like actor references now

2340
01:31:00,420 --> 01:31:02,580
what would you need actor references how

2341
01:31:02,580 --> 01:31:04,620
else would you know where to send a

2342
01:31:04,620 --> 01:31:09,030
message to right now if you create an

2343
01:31:09,030 --> 01:31:10,620
actor you implicitly have some sort of a

2344
01:31:10,620 --> 01:31:12,000
reference to it what would be nice if

2345
01:31:12,000 --> 01:31:13,800
actors can find out about other actors

2346
01:31:13,800 --> 01:31:15,690
from other actors and then you can just

2347
01:31:15,690 --> 01:31:17,340
let the word go around an interesting

2348
01:31:17,340 --> 01:31:20,490
thing would be to ask a boss which actor

2349
01:31:20,490 --> 01:31:22,080
could answer my question we do this all

2350
01:31:22,080 --> 01:31:24,270
the time when we try to accomplish

2351
01:31:24,270 --> 01:31:25,920
things as humans right you go to a

2352
01:31:25,920 --> 01:31:27,960
librarian and ask I mean all right

2353
01:31:27,960 --> 01:31:29,460
maybe to somebody in the department say

2354
01:31:29,460 --> 01:31:32,940
who's the best number theorist right say

2355
01:31:32,940 --> 01:31:36,660
ah that person could I got an actor

2356
01:31:36,660 --> 01:31:39,870
reference right talking face-to-face

2357
01:31:39,870 --> 01:31:41,460
with a person in fact it's a way of

2358
01:31:41,460 --> 01:31:42,840
sending messages I mean your brains are

2359
01:31:42,840 --> 01:31:44,730
not connected really it's just some sort

2360
01:31:44,730 --> 01:31:46,110
of messages or maybe you send an email

2361
01:31:46,110 --> 01:31:48,900
that's really a message right same here

2362
01:31:48,900 --> 01:31:51,570
right so reference is this reference can

2363
01:31:51,570 --> 01:31:54,420
actually be copied which is very

2364
01:31:54,420 --> 01:31:56,580
important that I can keep a reference

2365
01:31:56,580 --> 01:31:58,170
myself but if somebody asks about an a

2366
01:31:58,170 --> 01:31:59,640
Dracula I can say hey yeah this is the

2367
01:31:59,640 --> 01:32:05,190
reference now here comes the fun part

2368
01:32:05,190 --> 01:32:07,050
special operator to send a message to an

2369
01:32:07,050 --> 01:32:10,050
actor you say counter bang

2370
01:32:10,050 --> 01:32:12,330
increase this is the operator to send a

2371
01:32:12,330 --> 01:32:14,160
message behind the scenes it just cause

2372
01:32:14,160 --> 01:32:15,750
some kind of a sign message but this is

2373
01:32:15,750 --> 01:32:18,780
nice nice syntax now it's very important

2374
01:32:18,780 --> 01:32:21,210
to understand what these dots okay so if

2375
01:32:21,210 --> 01:32:23,010
you use just the bank operator remember

2376
01:32:23,010 --> 01:32:25,410
counter it's an actor or a reference to

2377
01:32:25,410 --> 01:32:27,360
an actor right in this case yes we say

2378
01:32:27,360 --> 01:32:30,540
counter it's an actor reference and any

2379
01:32:30,540 --> 01:32:32,040
framework that has actors will have a

2380
01:32:32,040 --> 01:32:34,350
way for you to find out what an actor is

2381
01:32:34,350 --> 01:32:36,270
might be some global registry or maybe

2382
01:32:36,270 --> 01:32:37,710
you know from another actor what it is

2383
01:32:37,710 --> 01:32:40,530
or whatever whatnot okay so this send a

2384
01:32:40,530 --> 01:32:41,940
message but this sends an asynchronous

2385
01:32:41,940 --> 01:32:43,830
message we're going to go through the

2386
01:32:43,830 --> 01:32:44,820
synchronous versus asynchronous

2387
01:32:44,820 --> 01:32:46,620
throughout the class because it's one of

2388
01:32:46,620 --> 01:32:48,270
the central issues in distributed

2389
01:32:48,270 --> 01:32:51,120
systems asynchronous means something

2390
01:32:51,120 --> 01:32:54,540
like send and forget to send a message

2391
01:32:54,540 --> 01:32:56,700
to a synchronously means you place the

2392
01:32:56,700 --> 01:32:58,170
message in the mailbox of the other

2393
01:32:58,170 --> 01:32:59,910
actor but you're not waiting for that

2394
01:32:59,910 --> 01:33:01,200
actor to read the message and do

2395
01:33:01,200 --> 01:33:03,860
anything about it

2396
01:33:03,920 --> 01:33:05,970
depending on the framework the message

2397
01:33:05,970 --> 01:33:07,830
might get delivered instantaneously or

2398
01:33:07,830 --> 01:33:10,740
not or never right you don't care the

2399
01:33:10,740 --> 01:33:13,130
bank says send it and forget about it

2400
01:33:13,130 --> 01:33:16,530
now this is really one of the best ways

2401
01:33:16,530 --> 01:33:18,720
to get interesting stuff going because

2402
01:33:18,720 --> 01:33:21,180
nobody waits for anybody I might check

2403
01:33:21,180 --> 01:33:21,810
later

2404
01:33:21,810 --> 01:33:23,880
or am I get a message later whether

2405
01:33:23,880 --> 01:33:25,740
something happened or not I should be

2406
01:33:25,740 --> 01:33:27,000
concerned if the message gets lost

2407
01:33:27,000 --> 01:33:28,620
because that's problematic but if I'm

2408
01:33:28,620 --> 01:33:29,970
guarantee that the message is gone get

2409
01:33:29,970 --> 01:33:32,250
lost I don't really care when the attack

2410
01:33:32,250 --> 01:33:34,230
to necessarily picks it up for example

2411
01:33:34,230 --> 01:33:36,330
imagine that you have workers and the

2412
01:33:36,330 --> 01:33:37,950
workers compute this logarithm tables

2413
01:33:37,950 --> 01:33:39,900
you can actually do some very

2414
01:33:39,900 --> 01:33:41,220
interesting thing for example if you

2415
01:33:41,220 --> 01:33:43,860
want to take a vacation right you can

2416
01:33:43,860 --> 01:33:48,330
basically say hey why not send work in

2417
01:33:48,330 --> 01:33:50,250
advance to the to the to this guy so

2418
01:33:50,250 --> 01:33:52,860
what I'm gonna say is I know I'm not

2419
01:33:52,860 --> 01:33:54,630
supposed to give them more than let's

2420
01:33:54,630 --> 01:33:56,190
say 10 entries in the table because

2421
01:33:56,190 --> 01:33:57,690
they're gonna be upset we agreed on 10

2422
01:33:57,690 --> 01:33:59,280
entries in the table per message fine

2423
01:33:59,280 --> 01:34:01,860
but nobody prevents me from sending 100

2424
01:34:01,860 --> 01:34:03,330
messages and they're gonna kill very

2425
01:34:03,330 --> 01:34:05,310
nicely now since they have to do work

2426
01:34:05,310 --> 01:34:06,780
for every message they take the first

2427
01:34:06,780 --> 01:34:08,190
message where we work on it by the way

2428
01:34:08,190 --> 01:34:09,750
innaka framer I can't even tell how many

2429
01:34:09,750 --> 01:34:11,670
messages you have in the queue so to you

2430
01:34:11,670 --> 01:34:12,960
it looks like you just have one message

2431
01:34:12,960 --> 01:34:14,640
we have one man's did you process it and

2432
01:34:14,640 --> 01:34:15,900
then you say I'm done with the message

2433
01:34:15,900 --> 01:34:17,820
immediately the framework says hey hey

2434
01:34:17,820 --> 01:34:18,930
you have another

2435
01:34:18,930 --> 01:34:21,180
you even send back a result you can

2436
01:34:21,180 --> 01:34:22,590
still go on vacation because all the

2437
01:34:22,590 --> 01:34:24,510
results queue up nicely in your mailbox

2438
01:34:24,510 --> 01:34:28,200
right if I already gave work to

2439
01:34:28,200 --> 01:34:31,170
everybody and nobody is watching me - no

2440
01:34:31,170 --> 01:34:32,820
I don't really do any work

2441
01:34:32,820 --> 01:34:35,130
I could be sneaky and go on vacation

2442
01:34:35,130 --> 01:34:38,490
right as an actor so I could go to sleep

2443
01:34:38,490 --> 01:34:40,500
but by the way that's a big luxury

2444
01:34:40,500 --> 01:34:42,120
because then you say there's no reason

2445
01:34:42,120 --> 01:34:44,670
to keep this actor alive let's make him

2446
01:34:44,670 --> 01:34:46,680
sleep and come back once a second

2447
01:34:46,680 --> 01:34:49,110
because so what if piled up in the in

2448
01:34:49,110 --> 01:34:50,790
the mailbox I don't need to work that

2449
01:34:50,790 --> 01:34:53,400
fast on it the other guys have worked in

2450
01:34:53,400 --> 01:34:55,230
advance and you might say oh yeah that's

2451
01:34:55,230 --> 01:34:56,910
a cute example but is useless in

2452
01:34:56,910 --> 01:34:58,200
practice I actually use it in my own

2453
01:34:58,200 --> 01:35:01,170
database engine to keep the hard drives

2454
01:35:01,170 --> 01:35:04,110
going at maximum speed you queue up

2455
01:35:04,110 --> 01:35:05,970
requests in advance for the hard drive

2456
01:35:05,970 --> 01:35:09,090
so that immediately once they finished

2457
01:35:09,090 --> 01:35:10,800
one request they immediately make

2458
01:35:10,800 --> 01:35:12,060
another request for the hard drive so

2459
01:35:12,060 --> 01:35:15,120
there is no gap in not keeping the hard

2460
01:35:15,120 --> 01:35:16,770
drive busy all the time so it's a very

2461
01:35:16,770 --> 01:35:18,960
very nice feature to be able to queue up

2462
01:35:18,960 --> 01:35:20,730
requests for an actor and you do it

2463
01:35:20,730 --> 01:35:22,260
naturally just send the more stuff

2464
01:35:22,260 --> 01:35:24,570
before they ask for it right and the

2465
01:35:24,570 --> 01:35:26,730
results come and everybody's happy that

2466
01:35:26,730 --> 01:35:29,160
way you can do this kind of double

2467
01:35:29,160 --> 01:35:31,620
double buffering techniques in which you

2468
01:35:31,620 --> 01:35:33,870
overlap two operations at the same time

2469
01:35:33,870 --> 01:35:35,370
in the natural way you just send

2470
01:35:35,370 --> 01:35:36,690
messages before they are needed because

2471
01:35:36,690 --> 01:35:39,480
you know what's gonna happen there right

2472
01:35:39,480 --> 01:35:42,450
good so Bank asynchronous message don't

2473
01:35:42,450 --> 01:35:45,680
wait for it done double bank by the way

2474
01:35:45,680 --> 01:35:49,860
Scala has actors itself and akka it's a

2475
01:35:49,860 --> 01:35:52,650
different actor library the Scala actors

2476
01:35:52,650 --> 01:35:54,150
are a little bit different and they have

2477
01:35:54,150 --> 01:35:55,860
slightly different variations in the

2478
01:35:55,860 --> 01:35:57,930
index I strongly suggest you use akka

2479
01:35:57,930 --> 01:36:00,120
because of the remote actors and because

2480
01:36:00,120 --> 01:36:02,880
it's really scales - large number of

2481
01:36:02,880 --> 01:36:04,440
actors so don't even bother with the

2482
01:36:04,440 --> 01:36:05,940
normal actors use our characters my

2483
01:36:05,940 --> 01:36:08,760
advice okay last in the last time I

2484
01:36:08,760 --> 01:36:11,070
taught the class I asked people to use

2485
01:36:11,070 --> 01:36:12,870
either and all of them inevitably ended

2486
01:36:12,870 --> 01:36:14,970
up with akka right much much better

2487
01:36:14,970 --> 01:36:18,540
supported good so double back double bag

2488
01:36:18,540 --> 01:36:21,720
means send message but then wait for a

2489
01:36:21,720 --> 01:36:25,740
reply the reply then you can do some

2490
01:36:25,740 --> 01:36:27,300
manipulation in this case cast it to an

2491
01:36:27,300 --> 01:36:30,480
end okay and then you put it in this

2492
01:36:30,480 --> 01:36:31,800
value so the behavior for this is you

2493
01:36:31,800 --> 01:36:32,080
send

2494
01:36:32,080 --> 01:36:33,460
message on your blog there until the

2495
01:36:33,460 --> 01:36:34,660
reply comes back now how do you think

2496
01:36:34,660 --> 01:36:37,840
this is implemented well the message is

2497
01:36:37,840 --> 01:36:39,250
still asynchronous the akka framework

2498
01:36:39,250 --> 01:36:41,110
only supports a synchronous messages but

2499
01:36:41,110 --> 01:36:44,020
what you do is you immediately do do

2500
01:36:44,020 --> 01:36:45,730
some kind of a receive after you've done

2501
01:36:45,730 --> 01:36:47,590
the sand and you're waiting only for a

2502
01:36:47,590 --> 01:36:49,390
specific message that's a special

2503
01:36:49,390 --> 01:36:50,710
facility in the akka in the akka

2504
01:36:50,710 --> 01:36:52,480
framework right and you block yourself

2505
01:36:52,480 --> 01:36:54,280
exactly like you would block if you have

2506
01:36:54,280 --> 01:36:56,380
no messages in your own queue until the

2507
01:36:56,380 --> 01:36:58,030
answer comes back right so the double

2508
01:36:58,030 --> 01:37:00,550
bang means send a message but expect to

2509
01:37:00,550 --> 01:37:03,600
reply okay but it gets better than this

2510
01:37:03,600 --> 01:37:13,960
yes okay okay

2511
01:37:13,960 --> 01:37:16,920
so increase what

2512
01:37:16,920 --> 01:37:20,850
ah so anybody what's the value of

2513
01:37:20,850 --> 01:37:26,640
increase ha is the it's just the type

2514
01:37:26,640 --> 01:37:29,040
itself has no value it's just the type

2515
01:37:29,040 --> 01:37:35,160
itself I mean what so implicitly means

2516
01:37:35,160 --> 01:37:37,980
increase by one we could edit a value

2517
01:37:37,980 --> 01:37:40,530
increase by what number and then it

2518
01:37:40,530 --> 01:37:41,820
would take a constructor with what the

2519
01:37:41,820 --> 01:37:43,890
number is but in this case it needs no

2520
01:37:43,890 --> 01:37:46,500
value so this is a straight situation in

2521
01:37:46,500 --> 01:37:48,150
which you have a useful class that has

2522
01:37:48,150 --> 01:37:57,390
no members WC you don't care what's

2523
01:37:57,390 --> 01:37:59,100
inside increase you just care that you

2524
01:37:59,100 --> 01:38:02,030
got an increase versus a get value right

2525
01:38:02,030 --> 01:38:04,710
so this is an interesting situation in

2526
01:38:04,710 --> 01:38:07,070
which the type of the message is the

2527
01:38:07,070 --> 01:38:09,660
entire content of the message there is

2528
01:38:09,660 --> 01:38:11,280
nothing beyond the type of the message

2529
01:38:11,280 --> 01:38:13,410
when the message just increase now

2530
01:38:13,410 --> 01:38:15,330
remember you can send anything in a

2531
01:38:15,330 --> 01:38:17,250
message and you have somehow to encode

2532
01:38:17,250 --> 01:38:19,260
the type of any specific messages and

2533
01:38:19,260 --> 01:38:22,489
inside right so the information shipped

2534
01:38:22,489 --> 01:38:24,960
but information is only the type of the

2535
01:38:24,960 --> 01:38:26,699
content but not any content for the

2536
01:38:26,699 --> 01:38:31,830
content right two more on this I mean

2537
01:38:31,830 --> 01:38:33,780
look if it works it's great because when

2538
01:38:33,780 --> 01:38:35,250
types can serve as snippets of

2539
01:38:35,250 --> 01:38:37,949
information of themselves right now I

2540
01:38:37,949 --> 01:38:39,420
don't even think this is possible

2541
01:38:39,420 --> 01:38:41,400
actually in a language like c++ the

2542
01:38:41,400 --> 01:38:42,900
compiler is not gonna be particularly

2543
01:38:42,900 --> 01:38:44,880
happy if you have no member well maybe i

2544
01:38:44,880 --> 01:38:47,130
think the father compiler might be very

2545
01:38:47,130 --> 01:38:50,160
confused with these things you at least

2546
01:38:50,160 --> 01:38:51,810
need some virtual method or something to

2547
01:38:51,810 --> 01:38:53,580
get something the link write compile is

2548
01:38:53,580 --> 01:38:55,199
gonna say now what right I think Scala

2549
01:38:55,199 --> 01:38:59,160
is perfectly fine ok good so now three

2550
01:38:59,160 --> 01:39:03,179
bags ok so what could you possibly do a

2551
01:39:03,179 --> 01:39:05,790
bang asynchronous a send message double

2552
01:39:05,790 --> 01:39:08,460
banks means send a message wait for the

2553
01:39:08,460 --> 01:39:13,140
reply but here is the thing waiting for

2554
01:39:13,140 --> 01:39:14,610
the reply means I have that time for

2555
01:39:14,610 --> 01:39:15,900
that actor by the way that time is

2556
01:39:15,900 --> 01:39:17,100
really bad because that means I'm not

2557
01:39:17,100 --> 01:39:18,690
processing any other message until the

2558
01:39:18,690 --> 01:39:21,960
reply comes back so would be nice if I

2559
01:39:21,960 --> 01:39:23,699
have the ability to look at the reply

2560
01:39:23,699 --> 01:39:24,900
because sending a reply it's a nice

2561
01:39:24,900 --> 01:39:27,270
facility right otherwise I mean you can

2562
01:39:27,270 --> 01:39:28,680
always in code I mean

2563
01:39:28,680 --> 01:39:30,210
let's let's forget about this let's say

2564
01:39:30,210 --> 01:39:33,870
we have no wait for reply and none of

2565
01:39:33,870 --> 01:39:35,640
these triple bang whatever that means

2566
01:39:35,640 --> 01:39:37,620
how could you deal with that well what

2567
01:39:37,620 --> 01:39:39,180
you could do is basically but then it

2568
01:39:39,180 --> 01:39:41,490
becomes more cumbersome in the message

2569
01:39:41,490 --> 01:39:43,320
you send send the reference to yourself

2570
01:39:43,320 --> 01:39:45,870
and agree with the other actor to send

2571
01:39:45,870 --> 01:39:47,580
you some message back but then you have

2572
01:39:47,580 --> 01:39:49,080
to register another message and you have

2573
01:39:49,080 --> 01:39:50,220
to remember that you send the message

2574
01:39:50,220 --> 01:39:52,230
somehow to be able to check later that

2575
01:39:52,230 --> 01:39:53,400
you have the message so we have to do

2576
01:39:53,400 --> 01:39:55,950
your own management internally to

2577
01:39:55,950 --> 01:39:58,230
remember that your intent was to wait

2578
01:39:58,230 --> 01:40:00,120
for the reply of somebody somehow and

2579
01:40:00,120 --> 01:40:02,820
check it later this operators make your

2580
01:40:02,820 --> 01:40:05,540
life easier yes

2581
01:40:23,939 --> 01:40:25,949
right so I think we are running

2582
01:40:25,949 --> 01:40:29,699
completely over time but okay so the

2583
01:40:29,699 --> 01:40:31,380
order in which messages get queued is

2584
01:40:31,380 --> 01:40:33,019
not guaranteed by the akka framework

2585
01:40:33,019 --> 01:40:36,019
whatever

2586
01:40:43,309 --> 01:40:46,849
right so we're going to talk about this

2587
01:40:46,849 --> 01:40:48,530
extensively anyway because it's a core

2588
01:40:48,530 --> 01:40:49,849
issue in distributed systems how do you

2589
01:40:49,849 --> 01:40:52,280
implement these things look a cot has

2590
01:40:52,280 --> 01:40:54,139
nothing but asynchronous messages

2591
01:40:54,139 --> 01:40:56,090
everything else is built on top of it to

2592
01:40:56,090 --> 01:40:58,610
simulate it because you only have

2593
01:40:58,610 --> 01:41:00,710
asynchronous messages which ever ended

2594
01:41:00,710 --> 01:41:02,659
up first in the queue in the message

2595
01:41:02,659 --> 01:41:06,260
queue it's wins and that's it right the

2596
01:41:06,260 --> 01:41:07,670
system makes no guarantees whatsoever

2597
01:41:07,670 --> 01:41:09,619
about in what order it delivers

2598
01:41:09,619 --> 01:41:11,150
synchro's or not no so synchronous

2599
01:41:11,150 --> 01:41:13,880
messages this is a cold decision in the

2600
01:41:13,880 --> 01:41:15,380
implementation of vodka we are going to

2601
01:41:15,380 --> 01:41:17,059
talk about how you could do other things

2602
01:41:17,059 --> 01:41:19,849
and you can always simulate synchronous

2603
01:41:19,849 --> 01:41:21,409
messages with certain delivery

2604
01:41:21,409 --> 01:41:23,480
guarantees using a pure asynchronous

2605
01:41:23,480 --> 01:41:25,369
messaging delivery mechanism and this is

2606
01:41:25,369 --> 01:41:27,349
what disabilities are all about I'm

2607
01:41:27,349 --> 01:41:29,090
telling you now only what akka does not

2608
01:41:29,090 --> 01:41:32,900
what could be done yes good so the

2609
01:41:32,900 --> 01:41:35,389
triple bang returns a future a future is

2610
01:41:35,389 --> 01:41:37,940
a nice object that allows you later to

2611
01:41:37,940 --> 01:41:39,920
say okay so what your value so you block

2612
01:41:39,920 --> 01:41:41,750
only when you ask what the value is but

2613
01:41:41,750 --> 01:41:43,579
not at the time you get the future so

2614
01:41:43,579 --> 01:41:45,469
you can decide to do a lot more work or

2615
01:41:45,469 --> 01:41:47,090
take a delay say hey I know it's going

2616
01:41:47,090 --> 01:41:48,500
to take at least 10 milliseconds to get

2617
01:41:48,500 --> 01:41:50,420
an answer why bother maybe I can do some

2618
01:41:50,420 --> 01:41:51,889
other work in the meantime ah 10

2619
01:41:51,889 --> 01:41:53,239
milliseconds are up let's check the

2620
01:41:53,239 --> 01:41:55,789
future so these futures are very very

2621
01:41:55,789 --> 01:41:57,230
interesting an important kind of

2622
01:41:57,230 --> 01:41:59,030
mechanism in Scala in general and they

2623
01:41:59,030 --> 01:42:02,869
are used by the airframe oh okay good so

2624
01:42:02,869 --> 01:42:05,889
have fun with Scala start googling for

2625
01:42:05,889 --> 01:42:07,699
resources for it and so on

2626
01:42:07,699 --> 01:42:09,829
I gave you the brief introduction to the

2627
01:42:09,829 --> 01:42:12,230
2d actors seeing examples is the best

2628
01:42:12,230 --> 01:42:14,929
way to learn and after the first

2629
01:42:14,929 --> 01:42:16,460
assignment I guarantee you that you will

2630
01:42:16,460 --> 01:42:20,030
know what actor model does or not get a

2631
01:42:20,030 --> 01:42:21,289
reasonable grade for it

2632
01:42:21,289 --> 01:42:24,920
alright so now since we didn't take any

2633
01:42:24,920 --> 01:42:26,510
break I think anyway the class is

2634
01:42:26,510 --> 01:42:29,210
supposed to waste the class supposed to

2635
01:42:29,210 --> 01:42:35,480
end so am i about so we're done we're

2636
01:42:35,480 --> 01:42:37,250
toast okay good so I'll see you next

2637
01:42:37,250 --> 01:42:39,230
time and I'm gonna continue with what

2638
01:42:39,230 --> 01:42:40,909
disability systems are all about but

2639
01:42:40,909 --> 01:42:42,860
keep on coming back to this actors right

2640
01:42:42,860 --> 01:42:44,570
all the time you're gonna say with the

2641
01:42:44,570 --> 00:00:00,000
actors how do you do this