Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added FreeT basic example #2819

Merged
merged 2 commits into from
May 21, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion docs/src/main/tut/datatypes/freemonad.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ In the following example a basic console application is shown.
When the user inputs some text we use a separate `State` monad to track what the user
typed.

As we can observe in this case `FreeT` offers us a the alternative to delegate denotations to `State`
As we can observe in this case `FreeT` offers us the alternative to delegate denotations to `State`
monad with stronger equational guarantees than if we were emulating the `State` ops in our own ADT.

```tut:book
Expand Down Expand Up @@ -560,6 +560,67 @@ val initialState = Nil
val (stored, _) = state.run(initialState).value
```

Another example is more basic usage of `FreeT` with some context `Ctx` for which we provide `Future` interpreter,
combined with `OptionT` for reducing boilerplate.

```tut:book
import cats.free._
import cats._
import cats.data._
import cats.implicits._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

sealed trait Ctx[A]

case class Action(value: Int) extends Ctx[Int]

def op1: FreeT[Ctx, Option, Int] =
FreeT.liftF[Ctx, Option, Int](Action(7))

def op2: FreeT[Ctx, Option, Int] =
FreeT.liftT[Ctx, Option, Int](Some(4))

def op3: FreeT[Ctx, Option, Int] =
FreeT.pure[Ctx, Option, Int](1)

val opComplete: FreeT[Ctx, Option, Int] =
for {
a <- op1
b <- op2
c <- op3
} yield a + b + c


/* Our interpreters e.g. for Future, DBIO, Id */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say let's not use Future in our documentation. Just don't want to give people the impression that it's not a bad idea to use Future. For the purpose of your example, we could use Either or something

Copy link
Contributor Author

@mtsokol mtsokol Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up with Try instead of Future.

You have mentioned adding FAQ item about moving away from Future.
So e.g. short paragraph about crucial reasons providing some sources for further reading?
Something like cats effect docs comparison?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the jobs has failed somehow with "GC overhead limit exceeded" and I'm not able to rerun it. Could you rerun it? Or should I trigger it (e.g. close and open pull)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the last response. Yeah, that cats effect docs is great as a further reading!


type OptFut[A] = OptionT[Future, A]

def futureInterpreter: Ctx ~> OptFut = new (Ctx ~> OptFut) {
def apply[A](fa: Ctx[A]): OptFut[A] = {
fa match {
case Action(value) => OptionT.liftF(Future(value))
}
}
}

def optFutLift: Option ~> OptFut = new (Option ~> OptFut) {
def apply[A](fa: Option[A]): OptFut[A] = {
fa match {
case Some(value) =>
OptionT(Future(Option(value)))
case None =>
OptionT.none
}
}
}

val hoisted = opComplete.hoist(optFutLift)
val evaluated = hoisted.foldMap(futureInterpreter)
val future = evaluated.value
```

## Future Work (TODO)

There are many remarkable uses of `Free[_]`. In the future, we will
Expand Down