-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
440 lines (384 loc) · 14 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>FSharp presentation</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/night.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style type="text/css">
p { text-align: left; }
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>From "F-What ?" to F#</h2>
<br/>
<h3>Language intro + discovering powerful yet accessible features</h3>
<br/>
<h4>birdmod (GitHub)</h4>
</section>
<section>
<h3>Presentation</h3>
<section>
<p>CLI language of created by Microsoft Research</p>
<p>Maintained and improved by the F# Software Foundation</p>
</section>
<section>
<blockquote cite="http://fsharp.org/">"F# is a <span style="color:#e7ad52">mature</span>, open source, cross-platform, <span style="color:#e7ad52">functional-first</span> programming language.<br/>
It empowers users and organizations to tackle complex computing problems with simple,
maintainable and robust code"</blockquote>
</section>
<section>
<p>mature : released in 2005, latest version is F# 4.1. MSDN docs available F#. Microsoft supports it as C#. Available since VS 2012 (at least)</p>
<p>
functional-first : not limited to functional paradigm. Possible to mix to object oriented or procedural code
</p>
</section>
</section>
<section>
<h3>Syntax Basics</h3>
<section>
<pre>
<code class="fsharp">
(* binding *)
let amount = 10
(* declare function and conditional *)
let incrementWithLimit number limit =
if number < limit then
number + 1
else
limit
</code>
</pre>
</section>
<section>
<pre>
<code class="fsharp">
(* reference & array & loop *)
open System.IO
let folders = Directory.GetDirectories @"C:\"
for folder in folders do
printfn "Folder found in C:\\ : %s" folder
(* n-tuples *)
let filmAndScore = ("inception", 8.8)
let filmScoreYear = ("interstellar", 8.6, 2014)
</code>
</pre>
</section>
</section>
<section>
<h3>Type inference</h3>
<section>
<p>
Compiler deduces types.<br/>
This information is not hidden. IDEs integrating F# provide it<br/>
</p>
<pre>
<code class="fsharp">
let isAwesome = true (* bool *)
let increment n = n + 1 (* (int -> int) *)
let increment n = n + 1. (* (float -> float) *)
</code>
</pre>
</section>
<section>
<p>Compiler notifies of ambiguous cases. We fix this by clarifying the expected type : <span style="color:#e7ad52">type annotations</span></p>
<pre>
<code class="fsharp">
(* ('a -> 'b) *)
let getLength x = x.Length
(* (string -> int) *)
let getLength (x:string) = x.Length
(* (float[] -> int) *)
let getLength (x:float []) = x.Length
</code>
</pre>
</section>
</section>
<section>
<h3>FP: Immutability</h3>
<p>Value is bound to the label, <span class="fragment">forever</span></p>
<p class="fragment">Limits surprise effects by working with copies, thread safety</p>
<p class="fragment">BTW System.String is immutable... and you're still alive</p>
<p class="fragment">Do not see it as a negative point</p>
</section>
<section>
<h3>FP : higher order</h3>
<p>functions accepts or return other functions as parameters<p>
<pre>
<code class="fsharp">
let sendEmail recipient recipientCheck =
recipientCheck recipient
</code>
</pre>
</section>
<section>
<h3>FP : partial application</h3>
<p>The power to build "pre configured" functions<p>
<pre>
<code class="fsharp">
(* t_credentials -> t_query -> t_result *)
let connectToDbThenExecute credentials query =
DB.connect credentials
DB.exec query
connectToDbThenExecute creds query1
connectToDbThenExecute creds query2
let executeQuery = connectToDbThenExecute creds
executeQuery query1
executeQuery query2
</code>
</pre>
</section>
<section>
<section>
<h3>Designing powerful types to model your domain </h3>
<p>How do you represent a set of exclusive cases ?</p>
</section>
<section>
<pre>
<code class="csharp">
/// booleans
var isUserAvailable = true
/// 0 stands for rock, 1 for paper,
/// 2 for scissors.
var player1Choice = 1
///... until the days someone adds a dynamite case
/// and "explodes" in your code
/// Dynamite case introduced.
/// Effective against scissors and paper
var player2Choice = 4
</code>
</pre>
</section>
<section>
<pre>
<code class="csharp">
var isScissors = true;
var isRock = false;
var isPaper = false;
var isDynamite = true;
</code>
</pre>
</section>
<section>
<pre>
<code class="csharp">
public enum TurnChoice
{
Player1,
Player2
}
/// ... has to evolve into ...
public enum AdvancedTurnChoice
{
Player1Day,
Player1Night,
Player2Day,
Player2Night,
}
</code>
</pre>
</section>
<section>
<p>Sometimes, we have constraints or we change
the design as if nothing happened and we hold limited data</p>
</section>
</section>
<section>
<section>
<h3>Discriminated unions</h3>
<p>~ Union in C</p>
<ul>
<li>Exclusive set of cases</li>
<li>Holds data</li>
<li>Meaningful, understandable, IEasilyDebuggable</li>
</ul>
</section>
<section>
<pre>
<code class="fsharp">
type UserChoice =
| Paper
| Scissors
| Rock
let user1Choice = Paper
(* so enums ? no because I did not write: *)
type UserChoice =
| Paper = 0
| Scissors = 1
| Rock = 2
</code>
</pre>
</section>
<section>
<pre>
<code class="fsharp">
type FamousCaseOfShapeModelization =
| Circle of uint32 (* ( ͡° ͜ʖ ͡°) *)
| Square of Edge : uint32
| Rectangle of uint32 * uint32
let TimesSquare = Square(12)
</code>
</pre>
</section>
<section>
<p>Multiple DU usage</p>
<pre>
<code class="fsharp">
type PlayingCardKind =
| Clubs | Spades | Diamonds | Hearts
type PlayingCardNum =
| Ace | King | Queen | Jack | Two | Three | Four
| Five | Six | Seven | Eight | Nine | Ten
type PlayingCard =
| PlayingCardNum * PlayingCardKind
type Deck = PlayingCard []
</code>
</pre>
</section>
<section>
<p>We could use enums ... Now imagine card values as floats... </p>
<strong>wrong/illegal states are not possible by design</strong>
</section>
<section>
<h3>Recursive DUs</h3>
<pre>
<code class="fsharp">
type BinaryTree =
| Node of 'NodeType * Tree * Tree
| Leaf of 'NodeType
</code>
</pre>
</section>
</section>
<section>
<h3>Pattern matching</h3>
<section>
<p>if then else : conditional<br/> This construct lets you branch between multiple expressions to execute with the ability to decompose or match input data <p>
<pre>
<code class="fsharp">
match data with
| case 1 -> expression 1
| ...
| case N -> expression N
</code>
</pre>
</section>
<section>
<p>One benefit is that missing cases are detected by the compiler.<br/> DUs are supported
so you will know at compile time that your design is broken</p>
<pre>
<code class="fsharp">
match cardKind with
| Clubs -> "♣"
| Spades -> "♠"
| Diamonds -> "♦" (* no hearts, won't compile *)
</code>
</pre>
</section>
<section>
<p>Data extraction is possible by creating a "pattern"</p>
<pre>
<code class="fsharp">
let todayActivities = ["curling"; "swimming"; "rock climbing"]
match todayActivities with
| "curling"::remainingElements -> "Excellent activity !"
| ...
type Meal = Breakfast | Lunch | Dinner
let lunchAndPriceTuple = (Breakfast, 15) (* Meal*int *)
let mealGossip tpl =
match mealTuple with
| (Breakfast, price) when price > 8 -> "omg so pricey!"
| (Breakfast, price) -> "where did you buy it ?"
| (Lunch, price) ...
</code>
</pre>
</section>
<section>
<p>Another level than if/then/else (we could even replace them)</p>
<p>Improved readability</p>
</section>
</section>
<section>
<h3>Type providers</h3>
<section>
<p>What is the workflow when working with data of an external source ?</p>
<p class="fragment ">=>Read the doc, analyse response, create types, play with properties</p>
<p class="fragment">How to work with data from external source in short cycles (schema changes ?) and with a need to be operational quickly ?</p>
<p class="fragment ">=>By using the same workflow ?</p>
</section>
<section>
<ul>
<li>Read the doc</li>
<li>analyse response : documentation defines it</li>
<li>create type : if we know the structure, we know how to navigate inside</li>
</ul>
<p>The concept is to generate on the fly the structures and properties to explore and use them.<br/>
We may miss information in the doc. Which tool could let us discover it ? </p>
</section>
<section>
<h3>If the demo fails...</h3>
<pre>
<code class="fsharp">
[<LiteralAttribute>] (* compile time assignment *)
let url = "https://jsonplaceholder.typicode.com/users"
type LectureDataProvider = JsonProvider<url>
let allUsers = LectureDataProvider.Load(url)
</code>
</pre>
</section>
</section>
<section>
<h3>Conclusion</h3>
<section>
<p>Wrap up</p>
<p>The difficulty is to set the cursor between FP and F#
so some stuff is left</p>
</section>
<section>
<p>Not covered in this presentation because of time:</p>
<ul>
<li>options</li>
<li>Measures</li>
<li>RECORDS</li>
<li>list comprehensions</li>
<li>composition & pipe operator</li>
<li>communication with C#</li>
<li>...</li>
</ul>
</section>
</section>
<section>
<h2>Questions ?</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>