class: center, middle # Functional Programming Concepts in Scala Thomas Kaliakos • [@tkaliakos] • [Monoid] --- ## Agenda - What does Functional Programming even mean? - Functional Programming Concepts - Let's see it in practice - Recap --- ## What does Functional Programming even mean? It's all about programming with functions! --- ## What does Functional Programming even mean? It's all about programming with functions! But what is a function?! --- ## What does Functional Programming even mean? Let's think of mathematical functions \\(f(x) = x^2 + 3*x + 4\\) \\(e^{i\pi} - 1 = 0\\) --- ## Mathematical function properties --- ## Mathematical function properties * They always return the same result for the same input - Deterministic * For every input they return an output - Total * The only effect is the specific calculation - Pure --- ## What is x? \\(y=3\\) \\(z=2*y\\) \\(x=z^2 - z\\) --- ## Substitute and solve \\(y=3\\) \\(z=2*y\\) \\(x=z^2 - z\\)
\\(x = (2 \times y)^2 - 2 * y\\) \\(x = (2 \times 3)^2 - 2 \times 3\\) \\(x=30\\) --- ## Could you do the same with code? .center[] --- ## Could you do the same with code? ```scala val it = List(1, 2, 3).iterator val y = it.next() val x = y^2 + y ``` --- ## Could you do the same with code? ```scala val it = List(1, 2, 3).iterator val y = it.next() val x = y^2 + y ``` ```scala var x = y^2 + y x = it.next()^2 + it.next() x = 1^2 + 2 x = 3 ``` --- ## Could you do the same with code? ```scala val it = List(1, 2, 3).iterator val y = it.next() val x = y^2 + y ``` ```scala var x = y^2 + y x = it.next()^2 + it.next() x = 1^2 + 2 x = 3 ``` .center[
😒
] --- ## Twitter wisdom .center[] --- ## .center[] --- ## So what are the benefits of functional programming? * Better reasoning about our programs - equational reasoning * Easier to refactor * Correctness of the code can be inferred locally - local reasoning * Leverage the type system, writing more expressive code --- class: center, middle ## Let's try to do this in code! --- ## You should all know this game :) .center[] --- class: center, middle # Let's create a Tic Tac Toe game! --- ## Recap - How to write purely functional code * Make all your functions total * Use recursion where appropriate * Leverage the type system, narrow down your domain * Defer the execution of impure operations --- ## Recap - Is it for me? * Writing pure functional code adds restrictions to your code * Those restrictions make it easier to reason about your program * It takes some time to get used to it * The trade-off when writing something small and self contained, maybe is not worth it * If you decide to do it, stick to it --- ## Where to go next? * [Gabriel Volpe - Cats Effect: The IO Monad for Scala](https://youtu.be/8_TWM2t97r4) * [Functional Programming with Effects by Rob Norris](https://youtu.be/30q6BkBv5MY) * [John De Goes - FP to the Max](https://youtu.be/sxudIMiOo68) * [Creative Scala - free book](https://www.creativescala.org/) * [Functional Programming in Scala](https://www.manning.com/books/functional-programming-in-scala) --- class: center, middle # Questions? Thank you! [Monoid]:https://monoid.tech [@tkaliakos]:https://twitter.com/tkaliakos