-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1139 lines (1025 loc) · 30.1 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<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>Functional Programming in Rust</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/n.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/monokai.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>
<script src="js/Chart.bundle.min.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h3> Introduction to </h3> <h2>Functional Programming</h2> <h3> in Rust </h3>
<p>Amit Dev (<a href="https://github.com/amitdev">github.com/amitdev</a>
<a href="https://twitter.com/amitdevr">@amitdevr</a>)</p>
</section>
<section>
<h2> What is Functional Programming? </h2>
<img class="fragment" src="images/funprog.png">
</section>
<section>
<ul>
<li> Values + Pure functions</li>
<li class="fragment"> Composition </li>
<li class="fragment"> Reasoning </li>
</ul>
</section>
<section data-background="images/lego.jpg">
</section>
<section data-background="#ffffff">
<img src="images/sim.png" style="width:45%;height:45%;border:0;" />
<img class="fragment" src="images/sl.jpeg" style="width:45%;height:45%;border:0;" />
</section>
<section data-background="#ffffff">
<img src="images/rust.png" style="width:50%;height:50%;border:0;" />
</section>
<section>
<blockquote>
“A language that doesn't affect the way you think about programming, is not worth knowing”
</blockquote>
<small>Alan Perlis</small>
</section>
<section>
<h2> Why Rust </h2>
<img src="images/whyrust.png">
</section>
<section>
<blockquote>
“Zero-overhead principle:<br>
What you don’t use, you don’t pay for, And further:
What you do use, you couldn’t hand code any better.”
</blockquote>
<small>Bjarne Stroustrup - Foundations of C++</small>
</section>
<section>
<img src="images/typing.jpg" style="width:45%;height:45%;" >
<small> https://twitter.com/01k/status/1067788059989684224 </small>
</section>
<section>
<h2> Key Features </h2>
<ul>
<li> Strongly Static Typed </li>
<li class="fragment"> Type Inference </li>
<li class="fragment">Based on Linear Types </li>
<li class="fragment"> Region based memory management</li>
</ul>
</section>
<section data-background="images/dive.jpg">
</section>
<section>
<h2> Ownership model </h2>
</section>
<section>
<h2> Immutable by default</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="2-3">
fn main() {
let x = 3;
x = 5;
}
</code>
</pre>
<pre class="fragment">
<code class="html" data-trim>error[E0384]: cannot assign twice to immutable variable `x`
|
2 | let x = 3;
| -
| |
| first assignment to `x`
| help: make this binding mutable: `mut x`
3 | x = 5;
| ^^^^^ cannot assign twice to immutable variable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0384`.
</code>
</pre>
</section>
<section>
<h2> Move Semantics </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-5">
fn main() {
let x = String::from("hello");
let y = x;
println!("x={}, y={}", x, y);
}
</code>
</pre>
<pre class="fragment">
<code class="html">error[E0382]: borrow of moved value: `x`
|
2 | let x = String::from("hello");
| - move occurs because `x` has type `std::string::String`,
which does not implement the `Copy` trait
3 | let y = x;
| - value moved here
4 | println!("x={}, y={}", x, y);
| ^ value borrowed here after move
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.
</code></pre>
</section>
<section>
<h2> Clone </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-5">
fn main() {
let x = String::from("hello");
let y = x.clone();
println!("x={}, y={}", x, y);
}
</code>
</code>
</pre>
<pre class="fragment"><code>x=hello, y=hello</code></pre>
</section>
<section>
<h2> Copy Trait </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="2-4">
fn main() {
let x = 3;
let y = x;
println!("x={}, y={}", x, y);
}
</code>
</pre>
<pre class="fragment"><code>x=3, y=3</code></pre>
</section>
<section>
<h2> Mutable + Ownership</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-5">
fn append_newline(original: String) -> String {
let mut result = original;
result.push_str("\n");
result
}
fn main() {
let x = String::from("Hello");
let y = append_newline(x);
println!("{}", y);
}
</code>
</pre>
</section>
<section>
<h2> Mutable + Ownership</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-11">
fn append_newline(original: String) -> String {
let mut result = original;
result.push_str("\n");
result
}
fn main() {
let x = String::from("Hello");
let y = append_newline(x);
println!("{}", y);
}
</code>
</pre>
</section>
<section>
<h2> References </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-10">
fn len(s: String) -> usize {
s.len()
}
fn main() {
let x = String::from("hello");
println!("{}", len(x));
println!("{}", x);
}
</code>
</pre>
</section>
<section>
<h2> References </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-10">
fn len(s: &String) -> usize {
s.len()
}
fn main() {
let x = String::from("hello");
println!("{}", len(&x));
println!("{}", x);
}
</code>
</pre>
</section>
<section>
<h2> Mutable References </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-10">
fn len(s: &mut String) -> usize {
s.len()
}
fn main() {
let x = String::from("hello");
println!("{}", len(&mut x));
println!("{}", x);
}
</code>
</pre>
</section>
<section>
<h2> Ownership Overview </h2>
<ul>
<li> Every value has a single owner, when owner goes out of scope the value is dropped.</li>
<li class="fragment"> There can be <em>either</em> one mutable reference or any number of immutable references. </li>
<li class="fragment"> References have lifetimes. </li>
</ul>
</section>
<section>
<h2> Basic types </h2>
<table>
<thead>
<tr>
<th>Type</th>
<th>Builtin</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Integer</td>
<td><pre>u8 | u16 | u32 | u64 | u128 </pre></td>
<td><pre>2</pre></td>
</tr>
<tr>
<td><pre>i8 | i16 | i32 | i64 | i128 </pre></td>
<td><pre>-2, 0xff</pre></td>
</tr>
<tr>
<td>Floating Point</td>
<td><pre>f32 | f64</pre></td>
<td><pre>1.23</pre></td>
</tr>
<tr>
<td>Character</td>
<td><pre>char</pre></td>
<td><pre>'a','ℤ', '😻'</pre></td>
</tr>
<tr>
<td>Boolean</td>
<td><pre>bool</pre></td>
<td><pre>true, false</pre></td>
</tr>
</tbody>
</table>
</section>
<section>
<h2> Algebraic Data Types </h2>
</section>
<section>
<h2> 0 values</h2>
<style>
.grid {
display: flex;
}
.col {
flex: 1;
padding: 5px;
}
</style>
<div class="grid">
<div class="col">
Rust
<pre><code>enum Void {}</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data Void</code></pre>
</div>
</section>
<section>
<h2> 1 value </h2>
<div class="grid">
<div class="col">
Rust
<td><pre><code data-trim>
enum Unit {
Unit
}</code> or <code>()</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data Unit = Unit</code> or <code>()</code></pre>
</div>
</div>
</section>
<section>
<h2> 1 + 1</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
enum Direction {
Up,
Down
}</code> or <code>bool</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data Direction = Up | Down</code> or <code>Boolean</code></pre>
</div>
</div>
</section>
<section>
<h2> 1 + A</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
enum Option<A> {
Some(A),
None
}</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data Maybe a = Just a | Nothing</code></pre>
</div>
</div>
</section>
<section>
<h2> A + B</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
enum Result<A, B> {
Ok(A),
Err(B)
}</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data Either a b = Left a | Right b</code></pre>
</div>
</div>
</section>
<section>
<h2> A * B</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
(A, B)
//or
struct Tuple { a: A, b: B }
</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data (a, b) = (a, b)</code></pre>
</div>
</div>
</section>
<section>
<h2> L = 1 + AL</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
enum List<A> {
Nil,
Cons(A, List<A>)
}
</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data List a =
Nil |
Cons a (List a)</code></pre>
</div>
</div>
<pre class="fragment"><code class="text" data-trim>
error[E0072]: recursive type `List` has infinite size
--> src/main.rs:1:1
|
1 | enum List<A> {
| ^^^^^^^^^^^^ recursive type has infinite size
2 | Nil,
3 | Cons(A, List<A>)
| ------- recursive without indirection
|
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at
some point to make `List` representable
</code></pre>
</section>
<section>
<h2> L = 1 + AL</h2>
<div class="grid">
<div class="col">
Rust
<pre><code data-trim>
enum List<A> {
Nil,
Cons(A, Box<List<A>>)
}
</code></pre>
</div>
<div class="col">
Haskell
<pre><code>data List a =
Nil |
Cons a (List a)</code></pre>
</div>
</div>
</section>
<section>
<h2> Pattern Matching </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-12">
struct Person {
name: String,
address: Option<String>
}
//...
match person {
Person { name, address : Some(s) }
=> (name, s.len()),
Person { name, address : None}
=> (name, 0)
}
</code>
</pre>
</section>
<section>
<h2> Pattern Matching </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-10">
fn age_type(age: Option<i32>) -> &'static str {
match age {
Some(val) if val < 0 => "Negative",
Some(val @ 0 ... 18) => "Less than 18",
Some(val) if val > 150 => "Over 150",
Some(val) => "Adult",
None => "Empty"
}
}
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre class="fragment">
<code class="hljs" data-trim data-line-numbers="1-3">
pub trait Clone {
fn clone(&self) -> Self;
}
impl Clone for MyStruct {
fn clone(&self) -> Self {
...
}
}
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-9">
pub trait Clone {
fn clone(&self) -> Self;
}
impl Clone for MyStruct {
fn clone(&self) -> Self {
...
}
}
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-4">
use std::ops;
#[derive(Debug)]
struct Point(i64, i64);
impl ops::Add<Point> for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point(self.0 + other.0, self.1 + other.1)
}
}
fn main() {
println!("{:?}", Point(1, 2) + Point(2, 1)); // Point(3,3)
}
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-15">
use std::ops;
#[derive(Debug)]
struct Point(i64, i64);
impl ops::Add<Point> for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point(self.0 + other.0, self.1 + other.1)
}
}
fn main() {
println!("{:?}", Point(1, 2) + Point(2, 1)); // Point(3,3)
}
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-3">
pub trait TimeDuration {
fn days(&self) -> Duration;
}
impl TimeDuration for i64 {
fn days(&self) -> Duration {
Duration::days(*self)
}
}
date + 3.days();
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-9">
pub trait TimeDuration {
fn days(&self) -> Duration;
}
impl TimeDuration for i64 {
fn days(&self) -> Duration {
Duration::days(*self)
}
}
date + 3.days();
</code>
</pre>
</section>
<section>
<h2> Traits </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="11">
pub trait TimeDuration {
fn days(&self) -> Duration;
}
impl TimeDuration for i64 {
fn days(&self) -> Duration {
Duration::days(*self)
}
}
date + 3.days() // Add 3 days to the given date
</code>
</pre>
</section>
<section>
<h2> Factorial - Take 1 </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1">
fn factorial(n: u64) -> u64 {
match n {
0 | 1 => 1,
n => n * factorial(n - 1),
}
}
</code>
</pre>
</section>
<section>
<h2> Factorial - Take 1 </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-6">
fn factorial(n: u64) -> u64 {
match n {
0 | 1 => 1,
n => n * factorial(n - 1),
}
}
</code>
</pre>
</section>
<section>
<h2> Closures </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-2">
let square = |a| a * a;
square(3); // 9
let mul = |a, b| a * b;
mul(2, 3); // 6
let x = 5;
let add5 = |y| x + y;
add5(10); // 15
let side_effecty = || { ... };
</code>
</pre>
</section>
<section>
<h2> Closures </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="4-5">
let square = |a| a * a;
square(3); // 9
let mul = |a, b| a * b;
mul(2, 3); // 6
let x = 5;
let add5 = |y| x + y;
add5(10); // 15
let side_effecty = || { ... };
</code>
</pre>
</section>
<section>
<h2> Closures </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="7-9">
let square = |a| a * a;
square(3); // 9
let mul = |a, b| a * b;
mul(2, 3); // 6
let x = 5;
let add5 = |y| x + y;
add5(10); // 15
let side_effecty = || { ... };
</code>
</pre>
</section>
<section>
<h2> Closures </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="11">
let square = |a| a * a;
square(3); // 9
let mul = |a, b| a * b;
mul(2, 3); // 6
let x = 5;
let add5 = |y| x + y;
add5(10); // 15
let side_effecty = || { ... };
</code>
</pre>
</section>
<section>
<h2> Factorial - Take 2 </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-3">
fn factorial(n: u64) -> u64 {
(1..n+1).fold(1, |acc, i| acc * i)
}
</code>
</pre>
<fragment>
<pre class="fragment">
<code class="hljs" data-trim data-line-numbers="1-5">
use std::ops::Mul;
fn factorial(n: u64) -> u64 {
(1..n+1).fold(1, Mul::mul)
}
</code>
</pre>
</section>
<section>
<h2> Factorial - Take 3 </h2>
<div class="grid">
<div class="col">
<pre>
<code class="hljs" data-trim>
fn factorial(n: u64) -> u64 {
(1..n+1).product()
}
</code>
</pre>
</div>
<div class="col">
<span class="fragment">
<pre>
<code class="hljs" data-trim>
factorial n = product [1..n]
</code>
</pre>
</span>
</div></div>
</section>
<section>
<h2> Higher order functions</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-4">
fn apply<F, A, B>(f: F, arg: A) -> B
where F: Fn(A) -> B {
f(arg)
}
fn square(a: i32) -> i32 { a * a }
// apply can take another function as argument
let n: i32 = apply(square, 4); // n = 16
// apply can also take a closure as argument
let n: i32 = apply(|x| x * x, 4); // n = 16
</code>
</pre>
</section>
<section>
<h2> Higher order functions</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-11">
fn apply<F, A, B>(f: F, arg: A) -> B
where F: Fn(A) -> B {
f(arg)
}
fn square(a: i32) -> i32 { a * a }
// apply can take another function as argument
let n: i32 = apply(square, 4); // n = 16
// apply can also take a closure as argument
let n: i32 = apply(|x| x * x, 4); // n = 16
</code>
</pre>
</section>
<section>
<h2> Fn Trait Family </h2>
<ul>
<li class="fragment"><b>FnOnce</b><br>
Can be called only once. Closure takes ownership of captured variables</li>
<li class="fragment"><b>FnMut</b><br>
Mutable borrows values and can change the environment</li>
<li class="fragment"><b>Fn</b><br>
Immutably borrows values from the environment</li>
</ul>
</section>
<section>
<h2> Compose</h2>
f: X → Y, g: Y → Z <br> (g ∘ f): X → Z
<pre class="fragment">
<code class="rust" data-trim>
fn compose<X, Y, Z, F, G>(f: F, g: G) -> Fn(X) -> Z
where F: Fn(X) -> Y, G: Fn(Y) -> Z {
|x| g(f(x))
}
</code>
</pre>
<pre class="fragment"><code class="text" data-trim>
|
1 | fn compose<X, Y, Z, F, G>(f: F, g: G) -> Fn(X) -> Z
| ^^^^^^^^^^
| doesn't have a size known at compile-time
</code></pre>
</section>
<section>
<h2> Compose</h2>
f: X → Y, g: Y → Z <br> (g ∘ f): X → Z
<pre>
<code class="rust" data-trim>
fn compose<X, Y, Z, F, G>(f: F, g: G) -> impl Fn(X) -> Y
where F: Fn(X) -> Y, G: Fn(Y) -> Z {
|x| g(f(x))
}
</code>
</pre>
<pre class="fragment"><code class="text" data-trim>
error[E0373]: closure may outlive the current function, but it
borrows `g`, which is owned by the current function
|
3 | |x| g(f(x))
| ^^^ - `g` is borrowed here
| |
| may outlive borrowed value `g`
|
note: closure is returned here
</code></pre>
</section>
<section>
<h2> Compose</h2>
f: X → Y, g: Y → Z <br> (g ∘ f): X → Z
<pre>
<code class="rust" data-trim>
fn compose<X, Y, Z, F, G>(f: F, g: G) -> impl Fn(X) -> Y
where F: Fn(X) -> Y, G: Fn(Y) -> Z {
move |x| g(f(x))
}
</code>
</pre>
</section>
<section>
<h2> Iterator </h2>
<pre>
<code class="rust" data-trim>
data.lines()
.filter(|line| line.contains(query))
.map(str::to_lowercase)
.take(2)
.collect()
}
</code>
</pre>
</section>
<section>
<h2> Quick Sort </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1-10">
fn qsort<T>(lst: Vec<T>) -> Vec<T>
where T: PartialOrd {
if lst.len() == 0 {
return lst;
}
let pivot = lst[0];
let (lo, hi) = partition(lst, pivot);
let (left, right) = (qsort(lo), qsort(hi));
appended(left, pivot, right)
}
</code></pre>
</section>
<section>
<h2> Parallel Quick Sort </h2>
<pre>
<code class="hljs" data-trim data-line-numbers="2,8">
fn qsort<T>(lst: Vec<T>) -> Vec<T>
where T: PartialOrd + Send {
if lst.len() == 0 {
return lst;
}
let pivot = lst[0];
let (lo, hi) = partition(lst, pivot);
let (left, right) = rayon::join(|| qsort(lo), || qsort(hi));
appended(left, pivot, right)
}
</code></pre>
</section>
<section>
<h2> Is Rust a good functional programming language?</h2>
</section>
<section>
<table>
<tbody>
<tr>
<td>Safe/Total functions</td>
<td><span>✓</span></td>
</tr>
<tr class="fragment">
<td>Algebraic data types</td>
<td><span>✓</span></td>
</tr>
<tr class="fragment">
<td>Immutability/Values</td>
<td><span>✓</span></td>
</tr>
<tr class="fragment">
<td>Higher order functions</td>
<td><span>−</span></td>
</tr>
<tr class="fragment">
<td>Advanced functional abstractions</td>
<td><span>−</span></td>
</tr>
<tr class="fragment">
<td>Pure functions</td>
<td><span>✕</span></td>
</tr>
</tbody>
</table>