17. class Option[A]
class List[A]
object optionToList extends (Option ~> List) {
def apply[A](given: Option[A]): List[A] =
given.toList
}
Option to ListOption to List
30. I have a math problemI have a math problem
(there are several interpretations of that statement...)
(yes, I mean all of them at once...)
(cue the IDE already!)
31. What's 1 + 2?What's 1 + 2?
(why didn't you cut to the IDE?!)
(don't let the audience see this!)
32. You did what with that?You did what with that?
That's right, I interpreted the sh** out of that.(NSFW?)
34. case class Return[F[_], A](given: A)
extends Free[F, A]
case class FlatMap[F[_], A, B](given: Free[F, A],
fn: A => Free[F, B])
extends Free[F, B]
AST for monadsAST for monads
38. Which is really...Which is really...
FlatMap(Return("hope"),
hope => FlatMap(Return("it"),
it => FlatMap(Return("works"),
works => Return(s"$hope $it $works"))))
40. Fs all the way downFs all the way down
F[F[F[F[F[F[F[F[F[F[F[F[F[F[F[F[A]]]]]]]]]]]]]]]]
41. So what you're saying is...So what you're saying is...
We have a monad for monadsWe have a monad for monads
but it doesn't do monad-y things...but it doesn't do monad-y things...
44. How is it "free"?How is it "free"?
If F is a functor, we get a
monad without having
to do any extra work!
sealed trait Free[F[_], A]
45. Free as in beer?Free as in beer?
Free in this context means generated freely in the sense that [a
foo] itself doesn’t need to have any [foo] structure of its own.
- Functional Programming in Scala
46. Did he really just say "free foo"?Did he really just say "free foo"?
Yes.Yes.
48. Repeat after me...Repeat after me...
Free monad = AST + interpreterFree monad = AST + interpreter
49. Can I compose different Fs?Can I compose different Fs?
(Great question! You read my mind!)
50. What I mean is...What I mean is...
trait Foo[A]
trait Bar[A]
val foo: Free[Foo, A] = ???
val bar: Free[Bar, A] = ???
for {
_ <- foo
_ <- bar
} yield ()