Creating a Free monad
Free monad has two constructors,
Return, which represents the continuation of a calculation and the completion of one, respectively.
Return(a: A): Free[F[_], A] Suspend(f: F[Free[F,A]]): Free[F[_], A]
const a = Return(1) const sum = Suspend(Identity(Return(1))) // or const sum = Free.liftF(Identity(1))
As you may see,
Return wraps a value A, where as
Suspend, wraps a
Functor containing another
Free.liftF(f: F[A]): Free[F[_], A]
Lifts a Functor
F into a
Free[F[_], A].map(f: A => B): Free[F[_], B]
Performs a map across the value inside the functor.
Aliases: bind, chain
Free[F[_], A].flatMap(f: A => Free[F[_], B]): Free[F[_], B]
Performs a monadic bind over the
Free[F[_], A].resume(): Either[F[Free[F[_], A]], A]
Evalutates a single layer in the computation, returning either a suspension or a result.
Free[F[_], A].go(f: F[Free[F[_], A]] => Free[F[_], A]) : A
Runs the computation to the end, returning the final result, using the supplied functor
f to extract the next
Free from the suspension.