Monday, June 25, 2018

And then...all of a sudden... the path to implementing this appeared: Freestyle.io

Monads are great and I have been trying to get my head around them... going from tutorial to 
tutorial trying to absorb the different explanations.

Finally, I have broken out of the pattern of continuing to focus on the theory in fear that the implementation
was going to be too hard.

I found Cats FreeMonad and Free Applicative through the Freestyle project:

The CATs documentation lists how to use FreeMonad but FreeStyle uses macros to make 
it really simple

 
Implementing computations with parallel and sequential operations is as simple as:


@free trait X {
  def a: FS[Int]
  def b(i: Int): FS.Par[Int] = a.map( x => x + i)
  def c: FS.Par[Int] = (a,b(42)).mapN(_ + _)
  def d: FS.Seq[Int] = c.freeS.flatMap(x => b(x).freeS)


as per the docs:

The names follow the intuition that Applicative operations
 combine data-independent computations that can be run in parallel, 
and Monad operations combine data-dependent computations that need to be run in sequence
This really appears to put a layer of abstraction around using functional patterns. In essence,
 the Free Monad and Free applicatives provide the key abstractions that allow you to start
building functional systems.


No comments: