No Description

2018-07-04_en__functional_rust.html 16KB

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