Skip to content

hermannhueck/little-monad-tutorial

Repository files navigation

Little Monad Tutorial in Scala

This is a little tutorial for Monads and functional programming techniques which just assumes basic Scala knowledge.

The tutorial is designed as a sequence of examples. The base package is the package tutorial. The examples are located in sub packages.

The description for all examples can be found in tutorial.docs.

Examples: Short Description:

  1. Example 01 contains the motivating example, where I implement a computeInts method three times using the same algorithm. The three compute methods differ only in their effect types, which are List, Option and Future.

  2. In Example 02 I abstract over the effect type. I provide my own Monad trait in libMyCats and Monad instances for List, Option and Future. This allows me to define a polymorphic compute method, which works with any effect type that has a Monad instance.

  3. In Example 03 this chapter I implement the Identity Monad.

  4. In this example implement the Either Monad. Either differs from the other effect types in the number of it's type parameters. Either has two type parameters, the other effect types only have one. This makes the implementation of the Monad instance a bit more complicated and requires the kind-projector compiler plugin to be included in the build.

  5. In this example we take a closer look at Scala functions.

  6. In this example I re-emphasize the fact that functions are values and can be treated like other values. Here we stuff a bunch of functions into a List and manipulate that list of functions with map and fold.

  7. In this example I implement a Monad instance for Function1.

  8. Here I implement the Reader Monad, which wraps a Function1.

  9. In this example I implement the IO Monad, which wraps a Function0.

  10. In this example I implement the type classes Semigroup and Monoid as well as instances for: Int and List[A].

  11. Semigroup and Monoid instances for various types: String, Boolean, Option[A]_ and Map[K, V].

  12. Three different Semigroup and Monoid instances for Function1