-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
534 lines (501 loc) · 15.9 KB
/
index.html
File metadata and controls
534 lines (501 loc) · 15.9 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>Functional programming in C#</title>
<link rel="icon" type="image/x-icon" href="/images/lambda_white.png" />
<link rel="stylesheet" href="dist/reset.css" />
<link rel="stylesheet" href="dist/reveal.css" />
<link rel="stylesheet" href="dist/theme/black.css" />
<link rel="stylesheet" href="plugin/highlight/monokai.css" />
<style>
.logo {
position: fixed;
left: 50%;
top: 25px;
transform: translateX(-50%);
transition: all linear 300ms;
}
.hide {
opacity: 0;
}
</style>
</head>
<body>
<img src="images/lambda.png" height="56" class="logo hide" id="logo" />
<div class="reveal">
<div class="slides">
<section id="first">
<img src="images/lambda.png" height="156" />
<h3>Functional programming in c#</h3>
<p>
<small
>By
<a href="https://github.com/Tomas-Juri">Tomáš Juřička</a></small
>
</p>
</section>
<section>
<h3>What is it?</h3>
<ul>
<p>Separating functions and Data</p>
<p>Expressions over statements</p>
<p>Immutable state</p>
</ul>
<aside class="notes">
Splitting data and logic <br />
Small reusable functions <br />
Pass functions around <br />
Functions = methods that return value <br /><br />
Expression is something that returns a value <br />
Chain functions on values <br />
Return a function as a value <br /><br />
State doesn't change <br />
Extreme benefits in UI libs like Blazor <br />
Fluent like API <br />
Thread safe-ish
</aside>
</section>
<section>
<h3>Why Functional in c# ?</h3>
<ul>
<p>It's already there</p>
<p>You're already using it</p>
<p>Not hard at all</p>
<p>More power</p>
<p>Cleaner code</p>
</ul>
<aside class="notes">
In c# since c# 2 <br />
Examples later <br />
Dont use math definitions - use your own names<br />
Some people say that learning functional first and OOP later made
more sense for them <br /><br />
You can write more robust and testable code, when you focus on more smaller functions, testing becomes easy and fast <br> <br>
You will also find yourself writing more reusable code, and it will be more easy to reuse, since you should split your data (types) and behaviour (functions) <br><br>
Smaller functions, less boilerplate, more reusable code, what more do you want from cleaner code
</aside>
</section>
<section>
<p style="color: gray">It's already there & you're using it</p>
<h3>LINQ</h3>
<pre class="language-cs">
<code>
list
.Select(number => number + " - Chickens")
.Where(word => word.Contains('1'))
.Where(string.IsNullOrEmpty)
.GroupBy(word => word.Length)
.ToList();
</code>
</pre>
<aside class="notes">
The whole library has functional design <br />
Saying what you want to be done <br />
One of the most powerfull features of the .net ecosystem
</aside>
</section>
<section>
<p style="color: gray">It's already there & you're using it</p>
<h3>Func type</h3>
<pre class="language-cs">
<code>
private static bool IsValid(int input)
...
list
.Where(IsValid)
.ToList();
</code>
</pre>
<pre class="language-cs">
<code>
private bool Validate(string input, params Func<string, bool>[] validations)
...
var isValid = Validate(value, IsNotEmpty, IsAllUppercase);
</code>
</pre>
<aside class="notes">
Since you're already using LINQ, youre already treating functions as values that can be passed around <br /><br>
You're therefore using something called higher order functions, which we will show later <br />
</aside>
</section>
<section>
<p style="color: gray">It's already there & you're using it</p>
<h3>Expressions over statements</h3>
<pre class="language-cs">
<code>
return a ? b : c
</code>
</pre>
<pre class="language-cs">
<code>
var message = number switch
{
> 5 => "Too much",
< 0 => "Cannot be negative",
< 1 => "Too low",
_ => "It's ok"
};
</code>
</pre>
</section>
<section>
<p style="color: gray">It's already there & you're using it</p>
<h3>Immutability</h3>
<pre class="language-cs">
<code>
int, string, Guid, DateTime, Tuple<> ...
</code>
</pre>
<aside class="notes">
All primitive value types: (Note: not all value types are immutable!)
</aside>
</section>
<section>
<p style="color: gray">It's already there & maybe you're using it</p>
<h3>Immutability</h3>
<pre class="language-cs">
<code>
public record Response
{
public DateOnly From { get; init; }
public DateOnly To { get; init; }
public int Duration { get; init; }
}
</code>
</pre>
</section>
<section>
<p style="color: gray">It's already there & maybe you're using it</p>
<h3>Immutability</h3>
<pre class="language-cs">
<code>
var x = response with
{
Duration = 20
};
</code>
</pre>
</section>
<section>
<h3>So what should we do more ?</h3>
<p style="color: gray">Embrace your inner lambda</p>
</section>
<section>
<h2>Use More LINQ</h2>
</section>
<section>
<h2>Start focusing on expressions and chaining</h2>
</section>
<section>
<small>From this ...</small>
<pre class="language-cs">
<code>
public static Risk CalculateRisk(int age, Gender gender)
{
var treshold = 0;
if (gender == Gender.Female)
treshold = 62;
else
treshold = 60;
if (treshold > age)
return Risk.Low;
return Risk.Medium;
}
</code>
</pre>
</section>
<section>
<small>... to this.</small>
<pre class="language-cs">
<code>
public static Risk CalculateRisk(int age, Gender gender) =>
Treshold(age, gender) > age ? Risk.Low : Risk.Medium;
private static int Treshold(int age, Gender gender) =>
gender == Gender.Female ? 62 : 60;
</code>
</pre>
</section>
<section>
<h2>Start writing your own "LINQ"</h2>
</section>
<section>
<small>From this ...</small>
<pre class="language-cs">
<code>
public static int? CalculateSalary()
{
var employee = GetEmployee();
if (employee == null)
return null;
return employee.Name == "Tom"
? 0
: int.MaxValue;
}
</code>
</pre>
</section>
<section>
<small>... to this ...</small>
<pre class="language-cs">
<code>
public static int? CalculateSalary() =>
GetEmployee()
.Map(employee => employee.Name == "Tom"
? 0
: int.MaxValue);
public static TOut? Map<TIn, TOut>(this TIn? input, Func<TIn, TOut> map) =>
input == null ? null : map(input);
</code>
</pre>
</section>
<section>
<small>... up to this.</small>
<pre class="language-cs">
<code>
public static int? CalculateSalary() =>
GetEmployee().Map(ToSalary);
public static int[] CalculateSalaries() =>
GetEmployee().Select(ToSalary).ToArray();
private static int ToSalary(Employee employee) =>
employee.Name == "Tom" ? 0 : int.MaxValue;
public static TOut? Map<TIn, TOut>(this TIn? input, Func<TIn, TOut> map) =>
input == null ? null : map(input);
</code>
</pre>
</section>
<section>
<small style="color: #883192">😱😱😱😱😱😱😱</small>
<h2>Will this compile?</h2>
<pre class="language-cs">
<code>
public static TOut? Map<TIn, TOut>(this TIn? input, Func<TIn, TOut> map) =>
input == null ? null : map(input);
</code>
</pre>
</section>
<section>
<small style="color: #883192">😡😡😡😡😡😡😡</small>
<h2>It won't</h2>
<pre class="language-cs">
<code>
public static TOut? Map<TIn, TOut>(this TIn? input, Func<TIn, TOut> map)
where TOut : struct
=> input == null ? null : map(input);
</code>
</pre>
</section>
<section>
<h2>Hey, but data fetching is async</h2>
</section>
<section>
<small>From this ...</small>
<pre class="language-cs">
<code>
public static async Task<int?> CalculateSalary()
{
var employee = await GetEmployee();
if (employee == null)
return null;
return employee.Name == "Tom"
? 0
: int.MaxValue;
}
</code>
</pre>
</section>
<section>
<small>... to this ??? ...</small>
<pre class="language-cs">
<code>
public static async Task<int?> CalculateSalary()
{
var employee = await GetEmployee();
return employee.Map(employee => employee.Name == "Tom" ? 0 : int.MaxValue);
}
</code>
</pre>
</section>
<section>
<small>... no, like this ! ...</small>
<pre class="language-cs">
<code>
public static async Task<int?> CalculateSalary() =>
await GetEmployee().Map(ToSalary);
public static async Task<TOut?> Map<TIn, TOut>(this Task<TIn?> input,
Func<TIn, TOut> map)
await input switch
{
null => null,
var value => map(value)
};
</code>
</pre>
</section>
<section>
<small>... but what if i want something else ...</small>
<pre class="language-cs">
<code>
public static async Task<(int, string)?> CalculateSalary()
{
var employee = await GetEmployee();
return employee == null
? null
: (ToSalary(employee), ToSomethingElse(employee));
}
</code>
</pre>
</section>
<section>
<small>... fasten your seatbelts ...</small>
</section>
<section data-transition="fade">
<small>... 🪄 Tada!</small>
<pre class="language-cs">
<code>
public static async Task<(int, string)?> CalculateSalary() =>
await
from employee in GetEmployee()
from salary in ToSalary(employee)
from somethingElse in ToSomethingElse(employee)
select (salary, somethingElse);
</code>
</pre>
</section>
<section data-transition="fade">
<small>🔮 Magic behind:</small>
<pre class="language-cs">
<code>
public static async Task<(int, string)?> CalculateSalary() =>
await
from employee in GetEmployee()
from salary in ToSalary(employee)
from somethingElse in ToSomethingElse(employee)
select (salary, somethingElse);
</code>
</pre>
<div class="fragment">
<pre class="language-cs">
<code>
public static async Task<T3> SelectMany<T1, T2, T3>(this Task<T1> source,
Func<T1, T2> bind,
Func<T1, T2, T3> project) =>
await source switch
{
var t1 => project(t1, bind(t1))
};
</code>
</pre>
</div>
</section>
<section>
<h2>
Data is nice <br />
but what if there's no data
</h2>
</section>
<section>
<small>Introducing</small>
<pre class="language-cs" style="width: fit-content">
<code>
void
</code>
</pre>
</section>
<section>
<small>From this ...</small>
<pre class="language-cs">
<code>
public static async Task Store(Employee employee)
{
if (employee.Id > 10)
await SaveToDatabase(employee);
else
await SaveToMemory(employee);
}
public static async Task SaveToDatabase(Employee employee)
public static async Task SaveToMemory(Employee employee)
</code>
</pre>
</section>
<section>
<small>... to this ? ...</small>
<pre class="language-cs">
<code>
public static async Task Store(Employee employee) =>
employee.Id > 10
? await SaveToDatabase(employee)
: await SaveToMemory(employee);
public static async Task SaveToDatabase(Employee employee)
public static async Task SaveToMemory(Employee employee)
</code>
</pre>
</section>
<section>
<small>... using this!</small>
<pre class="language-cs">
<code style="max-height: 550px;">
global using Unit = System.ValueTuple;
public static async Task<Unit> Store(Employee employee) =>
employee.Id > 10
? await SaveToDatabase(employee)
: await SaveToMemory(employee);
public static async Task<Unit> SaveToDatabase(Employee employee)
// Do something
return new Unit();
public static async Task<Unit> SaveToMemory(Employee employee)
// Do something
return new Unit();
</code>
</pre>
</section>
<section>
<h4>
Unit is extremely usefull later <br />
when writing more functional code.
</h4>
</section>
<section>
<h2>Conclusion</h2>
<p class="fragment">Be more functional</p>
</section>
<section>
<h2>Any Questions?</h2>
</section>
<section>
<a
href="https://github.com/Tomas-Juri/Functional-programming-in-csharp"
style="display: flex; flex-direction: column; align-items: center"
>
<img src="/images/github-link.png" width="300" alt="" />
<small>
https://github.com/Tomas-Juri/Functional-programming-in-csharp
</small>
</a>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
Reveal.initialize({
width: "70%",
height: "80%",
hash: true,
plugins: [RevealMarkdown, RevealHighlight, RevealNotes],
});
Reveal.on("slidechanged", (event) => {
if (event.previousSlide.id === "first")
document.getElementById("logo").classList.remove("hide");
if (event.currentSlide.id === "first")
document.getElementById("logo").classList.add("hide");
});
</script>
</body>
</html>