Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Breaking the entry barriers: Making the scary keywords accessible

Breaking the entry barriers: Making the scary keywords accessible

Most of the time, developers who decide to start learning Scala, Clojure or any other programming language based on the Functional Programming concepts find themselves frustrated in front of concepts such as Functors, Monads or Monoids to name a few, not knowing from where to begin and how to approach them. Those concepts are often used by the more experienced developers making the unexperienced ones wonder if they are ever going to write a Scala program in their life. I believe that those concepts are equally simple and important, and do not have to stop beginners from embracing a language like Scala.

Ayoub FAKIR

October 31, 2019
Tweet

More Decks by Ayoub FAKIR

Other Decks in Programming

Transcript

  1. The Beginning of a journey... • 4 years ago, I

    wrote my first lines of code in Scala • ... Then someone told me about this ”simple but yet powerful” concept called Monad 1
  2. Back to genesis! What is Why Functional Programming? • It’s

    pure • It’s readable • It’s maintainable • It’s breakable into pieces • Our little brain capacities can reason about it • When you use it, your mother becomes proud of you. 3
  3. What is a Category? It’s a simple concept (really). It

    consists of objects and links (arrows) going between them. 4
  4. The Basics of Composition Simply put, having an object O

    linked (arrow) with B, then B with C means that O and C are linked by composition. O ⇒ B ⇒ C O ⇒ C 5
  5. And what are these Arrows? Let’s be concrete: These arrows

    are what we call morphisms. These morphisms are nothing more than functions1 If we take a function F and apply the definition of Composition seen earlier: val f1: O => B val f2: B => C val f3: C => D f1 compose (f2 compose f3) = f1 compose f2 compose f3 and so on... 1Let’s call them pure functions. 6
  6. So we deduce that... Composition is and has: • Associative

    • An identity (The arrow goes from the object to itself), which we sometimes call the neutral element or zero2 2this is very important for the understanding of Functors, Monads, Monoids, etc... 7
  7. I can understand the Associativity... but why the zero? Getting

    back to history... • Romans didn’t have the ”zero” number, and thus: • It’s either you had 10 sheeps, or nobody was there. • Arabs were good in Algebra, partly thanks to the zero, Romans were not. • Without ”zero”, we have no way to draw a line between negative and positive numbers. • Absence of value or empty values. • I have zero money left. • I did not receive any items. • Got it? 8
  8. The Concept of Purity • Think small to solve big

    problems. • A pure function is one that has an input, and produces an output without altering the value of the input. • Also, a function does not have to do more than ONE thing. • Finally, it has no side effects, like writing to the console or writing to a file. 9
  9. Do you mean that I can’t write to a file

    in functional program- ming? Pretty useful! 10
  10. Don’t touch me, I’m immutable! • When I assign a

    value to a variable, it takes it forever! • Concurrent programming at will! • Working with Big Data and running programs in parallel using a lot of machines? No problem, we take it! 12
  11. Functor... What the hell is that? OK we’ll talk about

    functors... But first, what is a map? ⇒ Take a list of objects, then a function or transformation to apply to it, and map will take care of the rest. 13
  12. OK can you please tell me what is a Functor

    now? map + the container(so called context) = Functor 14
  13. And guess what? The Functor obeys the laws of composition

    we’ve seen earlier... Elle n’est pas belle la vie ? Oh and wait... Option, List, Try, Either... They’re all Functors! Hurray! And by the way, since the containers are from the same category, what we were describing here is... wait for it... an ENDOFUNCTOR! 15
  14. Going forward with Functors An excellent talk about Functors was

    made yesterday by Martin, check it out: shorturl.at/fptAW 18
  15. Functors have limits Now I realized that I needed to

    paint two cars, and asked my old Functor friend to use his map property and help me paint them and put them in the container for me... 19
  16. Monads! Help! Good news! Monads come with another property, the

    so called flatten, implemented through the flatMap... So with the help of the flatMap, the cars would’ve been stored inside the same container 21
  17. Monads - Wrap up “A Monad is like a Functor

    that knows how to flatten.” Abraham Lincoln 22
  18. Monoids, Monoids, Monoids! A Monoid has the same properties as

    Composition (WHAT? AGAIN?) Yes Sir! It is associative AND has an identity or neutral element Then, what makes it a Monoid is that it needs: • A Type [A]. • An Associative Binary Operation over the type [A]. 24
  19. Monoids in Practice 4 def combineAll[A: Monoid](as: List[A]): A =

    as.foldLeft(Monoid[A].empty)(Monoid[A].combine) combineAll(List(1, 2, 3)) //result ==> 6 What’s the use of the identity (Monoid.empty) here? Simply returning empty when we have nothing to give to the list. Thanks Mono! 4examples taken from Cats Doc. 25
  20. What about Side Effects? Writing to the console... Purely: val

    printPurely: IOResult[Unit] = for { _ <- println("I’m pure I swear!") _ <- println("Are you sure?") } yield () We wrapped our side effects (prints) a Monad (with its properties, it allows us to use for comprehensions) 26
  21. Wrapping side effects is pure... Really? I can’t tell... It’s

    more of a way to explicitely say that we’re talking to the outside world and that we take responsibility of the fact that we’re writing a non-pure function. 27
  22. Inch by inch, anything is a cinch. But it’s okay,

    for now, print your logs and write to your files, the learning journey is long enough. 28