Browse Source

add 2018-07-04 functional rust talk

Simon Lackerbauer 5 months ago
parent
commit
4d2ef7fff8
Signed by: Simon Lackerbauer <simon@lackerbauer.com> GPG Key ID: 2B27C889039C0125

+ 342
- 0
2018-07-04_en__functional_rust.html View File

@@ -0,0 +1,342 @@
1
+<!doctype html>
2
+<html>
3
+	<head>
4
+		<meta charset="utf-8">
5
+		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
6
+
7
+		<title>functional rust</title>
8
+
9
+		<link rel="stylesheet" href="reveal.js/css/reveal.css">
10
+		<link rel="stylesheet" href="reveal.js/css/theme/black.css">
11
+
12
+		<!-- Theme used for syntax highlighting of code -->
13
+		<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
14
+
15
+		<!-- Printing and PDF exports -->
16
+		<script>
17
+			var link = document.createElement( 'link' );
18
+			link.rel = 'stylesheet';
19
+			link.type = 'text/css';
20
+			link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'revealjs/css/print/paper.css';
21
+			document.getElementsByTagName( 'head' )[0].appendChild( link );
22
+		</script>
23
+	</head>
24
+	<body>
25
+		<div class="reveal">
26
+			<div class="slides">
27
+                <section><h2>functional rust</h2>
28
+                </section>
29
+                <section><h3>about</h3>
30
+                    <p>simon lackerbauer
31
+                        <small>
32
+                            <ul>
33
+                                <li>day job: operations engineer @ <a href="https://github.com/mayflower">github.com/mayflower</a></li>
34
+                                <li>mostly: nix, python, haskell</li>
35
+                                <li><a href="https://github.com/ciil">github.com/ciil</a></li>
36
+                            </ul>
37
+						</small>
38
+                    </p>
39
+                </section>
40
+                <section>
41
+                    <section><h2>rust intro</h2></section>
42
+                    <section><h3>facts</h3>
43
+                        <ul>
44
+                            <li>multi-paradigm
45
+                                <ul>
46
+                                    <li>compiled</li>
47
+                                    <li>concurrent</li>
48
+                                    <li><span class="fragment highlight-current-blue">functional<span></li>
49
+                                    <li>imperative</li>
50
+                                    <li>generic</li>
51
+                                </ul>
52
+                            </li>
53
+                            <li>statically, strongly typed</li>
54
+                        </ul>
55
+                    </section>
56
+                    <section><h3>known for</h3>
57
+                        <ul>
58
+                            <li>high level abstractions with low level performance</li>
59
+                            <li>great compiler error messages</li>
60
+                            <li>memory safety and concurrency</li>
61
+                            <li><a href="https://github.com/uutils">uutils</a> (esp. <a href="https://github.com/uutils/coreutils">uutils/coreutils</a>)</li>
62
+                            <li>ripgrep, exa, ...</li>
63
+                            <li>Mozilla Firefox/Quantum engine</li>
64
+                            <li>not so much: being the go-to language for functional programmers</li>
65
+                        </ul>
66
+                    </section>
67
+                    <section><p><small>... and for winning Witch Weekly's Most Charming Smile Award* three years in a row**</small></p>
68
+                        <img src="../imgs/functional_rust/gilderoy_lockhart.jpg" width="50%" />
69
+                        <p class="fragment"><small>* first place for "most loved programming language" in the Stack Overflow Developer Survey in 2016, 2017, and 2018</small></p>
70
+                        <p class="fragment"><small>** Harry Potter would rather use Malbolge o.O</small></p>
71
+                    </section>
72
+                </section>
73
+                <section>
74
+                    <section><h2>examples</h2></section>
75
+                    <section><h3>hello world</h3>
76
+                        <pre><code class="hljs rust" data-trim>
77
+                            fn main() {
78
+                                println!("Hello World");
79
+                            }
80
+                        </code></pre>
81
+                    </section>
82
+                    <section><h3>immutability</h3>
83
+                        <pre><code class="hljs rust" data-trim>
84
+                            fn main() {
85
+                                let foo = "bar";
86
+                                foo = "baz";
87
+                            }   
88
+                        </code></pre>
89
+                        output: 
90
+                        <pre><code class="hljs rust" data-trim>
91
+							error[E0384]: cannot assign twice to immutable variable `foo`
92
+							 --> src/main.rs:3:5
93
+							  |
94
+							2 |     let foo = "bar";
95
+							  |         --- first assignment to `foo`
96
+							3 |     foo = "baz";
97
+							  |     ^^^^^^^^^^^ cannot assign twice to immutable variable
98
+                        </code></pre>
99
+                    </section>
100
+                    <section><h3>mutability</h3>
101
+                        <pre><code class="hljs rust" data-trim>
102
+                            fn main() {
103
+                                let mut foo = "bar";
104
+                                foo = "baz";
105
+                            } 
106
+                        </code></pre>
107
+                    </section>
108
+                    <section><h3>ownership</h3>
109
+                        <ul>
110
+                            <li>very important concept in Rust</li>
111
+                            <li>each value has a variable that's called its owner</li>
112
+                            <li>there can only be one owner</li>
113
+                            <li>when the owner goes out of scope, the value will be dropped</li>
114
+                        </ul>
115
+                    </section>
116
+                    <section><h3>scope</h3>
117
+                        <pre><code class="hljs rust" data-trim>
118
+                            fn main() {
119
+                                {
120
+                                    let mut foo = "bar";
121
+                                }
122
+                                foo = "baz";
123
+                            }   
124
+                        </code></pre>
125
+                        output: 
126
+                        <pre><code class="hljs rust" data-trim>
127
+							error[E0425]: cannot find value `foo` in this scope
128
+							  --> src/main.rs:23:5
129
+							   |
130
+							23 |     foo = "baz";
131
+							   |     ^^^ not found in this scope
132
+                        </code></pre>
133
+                    </section>
134
+                    <section><h3>factorial (imperative)</h3>
135
+                        <pre><code class="hljs rust" data-trim>
136
+                            fn factorial(i: u64) -> u64 {
137
+                                let mut acc = 1;
138
+                                for num in 2..i+1 {
139
+                                    acc *= num;
140
+                                }
141
+                                acc
142
+                            }
143
+                        </code></pre>
144
+                    </section>
145
+                    <section><h3>factorial (recursive)</h3>
146
+                        <pre><code class="hljs rust" data-trim>
147
+                            fn factorial(i: u64) -> u64 {
148
+                                match i {
149
+                                    0 => 1,
150
+                                    n => n * factorial(n-1)
151
+                                }
152
+                            }
153
+                        </code></pre>
154
+                    </section>
155
+                    <section><h3>factorial (tail recursive)</h3>
156
+                        <pre><code class="hljs rust" data-trim>
157
+                            fn factorial(i: u64) -> u64 {
158
+                                fn fact_tr(i: u64, acc: u64) -> u64 {
159
+                                    match i { 
160
+                                        0 => acc,
161
+                                        n => fact_tr(n-1, n*acc)
162
+                                    }   
163
+                                }   
164
+                                fact_tr(i, 1)
165
+                            }   
166
+                        </code></pre>
167
+                    </section>
168
+                    <section><h3>common types</h3>
169
+                        <pre><code class="hljs rust" data-trim>
170
+                            let i: i32 = 35;
171
+                            let j: f32 = 3.0;
172
+                            let heart_eyed_cat = '😻';
173
+                            let nope: Option&lt;i32&gt; = None;
174
+                            let konnichiwa = "こんにちは";
175
+                            let maybe = Some(5);
176
+                            let test = nope &lt; maybe;
177
+                            println!("{} {}", test, konnichiwa);
178
+                        </code></pre>
179
+                        output: 
180
+                        <pre><code class="hljs rust" data-trim>
181
+                            true こんにちは
182
+                        </code></pre>
183
+                    </section>
184
+                    <section><h3>structs</h3>
185
+                        <pre><code class="hljs rust" data-trim>
186
+                            struct Rectangle {
187
+                                width: u32,
188
+                                height: u32,
189
+                            }
190
+                            impl Rectangle {
191
+                                fn area(&amp;self) -> u32 {
192
+                                    self.width * self.height
193
+                                }
194
+                            }
195
+                            let rect1 = Rectangle { width: 30, height: 50 };
196
+                            println!("The area of the rectangle is {} square pixels.", rect1.area());
197
+                        </code></pre>
198
+                        output: 
199
+                        <pre><code class="hljs rust" data-trim>
200
+                            The area of the rectangle is 1500 square pixels.
201
+                        </code></pre>
202
+                    </section>
203
+                    <section><h3>vectors</h3>
204
+                        <pre><code class="hljs rust" data-trim>
205
+                            let mut vector: Vec&lt;i32&gt; = vec![1, 2, 3, 4];
206
+                            vector.push(5);
207
+                            println!("{}", &amp;vector[4]);
208
+                        </code></pre>
209
+                        output: 
210
+                        <pre><code class="hljs rust" data-trim>
211
+                            5
212
+                        </code></pre>
213
+                    </section>
214
+                    <section><h3>closures</h3>
215
+                        <pre><code class="hljs rust" data-trim>
216
+							fn  add_one_v1   (x: u32) -> u32 { x + 1 }
217
+							let add_one_v2 = |x: u32| -> u32 { x + 1 };
218
+							let add_one_v3 = |x|             { x + 1 };
219
+							let add_one_v4 = |x|               x + 1  ;
220
+                        </code></pre>
221
+                        <ul>
222
+                            <li>a little like anonymous functions</li>
223
+                            <li>with some strange limitations</li>
224
+                        </ul>
225
+                    </section>
226
+                    <section><h3>closures pt2</h3>
227
+                        <pre><code class="hljs rust" data-trim>
228
+                            let id = |x| x;
229
+                            println!("{} {}", id(5), id("foo"));
230
+                        </code></pre>
231
+                        expected output from a functional standpoint (like Haskell): 
232
+                        <pre><code class="hljs rust" data-trim>
233
+							Prelude> let id = (\x -> x)
234
+							id :: p -> p
235
+							Prelude> id 5
236
+							5
237
+							it :: Num p => p
238
+							Prelude> id "foo"
239
+							"foo"
240
+							it :: [Char]
241
+                        </code></pre>
242
+                    </section>
243
+                    <section><h3>closures pt3</h3>
244
+                        <pre><code class="hljs rust" data-trim>
245
+                            let id = |x| x;
246
+                            println!("{} {}", id(5), id("foo"));
247
+                        </code></pre>
248
+                        actual output: 
249
+                        <pre><code class="hljs rust" data-trim>
250
+							error[E0308]: mismatched types
251
+							  --> src/main.rs:56:30
252
+							   |
253
+							56 |     println!("{} {}", id(5), id("foo"));
254
+							   |                                 ^^^^^ expected integral variable, found reference
255
+							   |
256
+							   = note: expected type `{integer}`
257
+							              found type `&amp;'static str`
258
+                        </code></pre>
259
+                    </section>
260
+                    <section><h3>iterators</h3>
261
+                        <pre><code class="hljs rust" data-trim>
262
+                            let v = vec![1, 2, 3];
263
+                            let v_iter = v.iter().map(|x| x + 1);
264
+                            let v_iter2 = v.iter().fold(0, |acc, &amp;x| acc + x);
265
+                            println!("{} {}", v_iter, v_iter2)
266
+                        </code></pre>
267
+                        expected output from a functional standpoint (like Haskell): 
268
+                        <pre><code class="hljs rust" data-trim>
269
+                            Prelude> map (\x -> x+1) [1,2,3,4]
270
+                            [2,3,4,5]
271
+                        </code></pre>
272
+                    </section>
273
+                    <section><h3>iterators</h3>
274
+                        <pre><code class="hljs rust" data-trim>
275
+                            let v = vec![1, 2, 3];
276
+                            let v_iter = v.iter().map(|x| x + 1);
277
+                            let v_iter2 = v.iter().fold(0, |acc, &amp;x| acc + x);
278
+                            println!("{} {}", v_iter, v_iter2)
279
+                        </code></pre>
280
+                        actual output: 
281
+                        <pre><code class="hljs rust" data-trim>
282
+							error[E0277]: `std::slice::Iter<'_, {integer}>` doesn't implement `std::fmt::Display`
283
+							  --> src/main.rs:62:20
284
+							   |
285
+							62 |     println!("{} {}", v_iter, v_iter2)
286
+							   |                       ^^^^^^ `std::slice::Iter<'_, {integer}>` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string                   
287
+							   |
288
+							   = help: the trait `std::fmt::Display` is not implemented for `std::slice::Iter<'_, {integer}>`
289
+							   = note: required by `std::fmt::Display::fmt`
290
+                        </code></pre>
291
+                    </section>
292
+                    <section><h3>in conclusion: rust as a functional programming language</h3>
293
+                        <ul>
294
+                            <li class="fragment">Rust can be hard to use in a purely functional manner</li>
295
+                            <li class="fragment">some ideosyncracies for people with experience in other functional languages</li>
296
+                            <li class="fragment">augmenting systems programming with functional concepts in otherwise object-oriented/imperative code is still helpful</li>
297
+                        </ul>
298
+                    </section>
299
+                    <section><h2>extra slides: io monad: burrito</h2></section>
300
+                    <section>
301
+                        <ul>
302
+                            <li>these limitations don't discourage some people</li>
303
+                            <li><a href="https://github.com/withoutboats/burrito">withoutboats/burrito</a> implements an IO monad conceptually similar to Haskell's IO monad</li>
304
+                            <li>obviously more a joke/conceptual implementation that just wraps Rust's std::io</li>
305
+                        </ul>
306
+                    </section>
307
+                    <section><h3>use burrito</h3>
308
+                        <pre><code class="hljs rust" data-trim>
309
+                        extern crate burrito;
310
+                        fn main() {
311
+                            use burrito::burrito;
312
+                            burrito().read_line().and_then(|echo, burrito| burrito.print_line(&echo));
313
+                        }
314
+                        </code></pre>
315
+                    </section>
316
+                </section>
317
+                <section>
318
+                    <h2>thanks for your attention</h2>
319
+                    <p>inspired by Lisa Passing: <a href="https://www.youtube.com/watch?v=Ve8HUfU3ELQ">Functional Rust - An Exploration @ Lambda Days 2018</a></p>
320
+                    <p>slides: <a href="https://github.com/ciil/talks">github.com/ciil/talks</a></p>
321
+                </section>
322
+			</div>
323
+		</div>
324
+
325
+		<script src="reveal.js/lib/js/head.min.js"></script>
326
+		<script src="reveal.js/js/reveal.js"></script>
327
+
328
+		<script>
329
+			// More info about config & dependencies:
330
+			// - https://github.com/hakimel/reveal.js#configuration
331
+			// - https://github.com/hakimel/reveal.js#dependencies
332
+			Reveal.initialize({
333
+				dependencies: [
334
+					{ src: 'reveal.js/plugin/markdown/marked.js' },
335
+					{ src: 'reveal.js/plugin/markdown/markdown.js' },
336
+					{ src: 'reveal.js/plugin/notes/notes.js', async: true },
337
+					{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
338
+				]
339
+			});
340
+		</script>
341
+	</body>
342
+</html>

+ 2
- 0
examples/functional_rust/.gitignore View File

@@ -0,0 +1,2 @@
1
+target
2
+*.lock

+ 6
- 0
examples/functional_rust/Cargo.toml View File

@@ -0,0 +1,6 @@
1
+[package]
2
+name = "functional_rust"
3
+version = "0.1.0"
4
+authors = ["Simon Lackerbauer <simon@lackerbauer.com>"]
5
+
6
+[dependencies]

+ 66
- 0
examples/functional_rust/src/main.rs View File

@@ -0,0 +1,66 @@
1
+fn main() {
2
+    // factorial
3
+    //fn factorial(i: u64) -> u64 {
4
+    //    fn fact_tr(i: u64, acc: u64) -> u64 {
5
+    //        match i {
6
+    //            0 => acc,
7
+    //            n => fact_tr(n-1, n*acc)
8
+    //        }
9
+    //    }
10
+    //    fact_tr(i, 1)
11
+    //}
12
+	//println!("{}", factorial(3)); 
13
+
14
+    // immutability
15
+    //let mut foo = "bar";
16
+    //foo = "baz";
17
+	//println!("{}", foo);
18
+    
19
+    // ownership
20
+    //{
21
+    //    let mut foo = "bar";
22
+    //}
23
+    //foo = "baz";
24
+    //println!("{}", foo);
25
+
26
+    // types
27
+    //let i: i32 = 35;
28
+    //let j: f32 = 3.0;
29
+    //let heart_eyed_cat: char = '😻';
30
+    //let konnichiwa = "こんにちは";
31
+    //let nope: Option<i32> = None;
32
+    //let maybe = Some(5);
33
+    //let test = nope < maybe;
34
+    //let v = vec![1, 2, 3];
35
+    //println!("{} {}", test, konnichiwa);
36
+    
37
+    // structs
38
+    //struct Rectangle {
39
+    //    width: u32,
40
+    //    height: u32,
41
+    //}
42
+    //impl Rectangle {
43
+    //    fn area(&self) -> u32 {
44
+    //        self.width * self.height
45
+    //    }
46
+    //}
47
+    //let rect1 = Rectangle { width: 30, height: 50 };
48
+    //println!("The area of the rectangle is {} square pixels.", rect1.area());
49
+
50
+	// vectors
51
+	//let mut vector: Vec<i32> = vec![1, 2, 3, 4];
52
+	//vector.push(5);
53
+	//println!("{}", &vector[4]);
54
+
55
+	// closures
56
+	//let id = |x| x;
57
+	//println!("{} {}", id(5), id("foo"));
58
+
59
+	// iterators
60
+    //let v = vec![1, 2, 3];
61
+	//let v_iter = v.iter().map(|x| x + 1);
62
+	//let v_iter2 = v.iter().fold(0, |acc, &x| acc + x);
63
+	//println!("{} {}", v_iter, v_iter2)
64
+
65
+	println!("Just uncomment any lines you want to try and exectute :)");
66
+}

BIN
imgs/functional_rust/gilderoy_lockhart.jpg View File