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

Add Cofree.ana and Cofree.anaEval #2325

Merged
merged 4 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 18 additions & 6 deletions free/src/main/scala/cats/free/Cofree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ final case class Cofree[S[_], A](head: A, tail: Eval[S[Cofree[S, A]]]) {

/** Transform the branching functor, using the T functor to perform the recursion. */
def mapBranchingT[T[_]](nat: S ~> T)(implicit T: Functor[T]): Cofree[T, A] =
Cofree[T, A](head, tail.map(v => T.map(nat(v))(_.mapBranchingT(nat))))
Cofree.anaEval(this)(_.tail.map(nat(_)), _.head)

/** Map `f` over each subtree of the computation. */
def coflatMap[B](f: Cofree[S, A] => B)(implicit S: Functor[S]): Cofree[S, B] =
Cofree[S, B](f(this), tail.map(S.map(_)(_.coflatMap(f))))
Cofree.anaEval(this)(_.tail, f)

/** Replace each node in the computation with the subtree from that node downwards */
def coflatten(implicit S: Functor[S]): Cofree[S, Cofree[S, A]] =
Cofree[S, Cofree[S, A]](this, tail.map(S.map(_)(_.coflatten)))
Cofree.anaEval(this)(_.tail, identity)

/** Alias for head. */
def extract: A = head
Expand All @@ -53,15 +53,28 @@ final case class Cofree[S[_], A](head: A, tail: Eval[S[Cofree[S, A]]]) {

/** Evaluate the entire Cofree tree. */
def forceAll(implicit S: Functor[S]): Cofree[S, A] =
Cofree[S, A](head, Eval.now(tail.map(S.map(_)(_.forceAll)).value))
Cofree.anaEval(this)(sa => Eval.now(sa.tail.value), _.head)

}

object Cofree extends CofreeInstances {

/** Cofree anamorphism, lazily evaluated. */
def unfold[F[_], A](a: A)(f: A => F[A])(implicit F: Functor[F]): Cofree[F, A] =
Cofree[F, A](a, Eval.later(F.map(f(a))(unfold(_)(f))))
ana(a)(f, identity)

/** Cofree anamorphism with a fused map, lazily evaluated. */
def ana[F[_], A, B](a: A)(coalg: A => F[A], f: A => B)(implicit F: Functor[F]): Cofree[F, B] =
anaEval(a)(a => Eval.later(coalg(a)), f)

/** Cofree anamorphism with a fused map. */
def anaEval[F[_], A, B](a: A)(coalg: A => Eval[F[A]], f: A => B)(implicit F: Functor[F]): Cofree[F, B] =
Cofree[F, B](f(a), mapSemilazy(coalg(a))(fa => F.map(fa)(anaEval(_)(coalg, f))))

private def mapSemilazy[A, B](fa: Eval[A])(f: A => B): Eval[B] = fa match {
case Now(a) => Now(f(a))
case other => other.map(f)
}

/**
* A stack-safe algebraic recursive fold out of the cofree comonad.
Expand Down Expand Up @@ -148,4 +161,3 @@ private trait CofreeTraverse[F[_]] extends Traverse[Cofree[F, ?]] with CofreeRed
G.map2(f(fa.head), F.traverse(fa.tailForced)(traverse(_)(f)))((h, t) => Cofree[F, B](h, Eval.now(t)))

}

6 changes: 6 additions & 0 deletions free/src/test/scala/cats/free/CofreeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class CofreeSuite extends CatsSuite {
cofNelToNel(unfoldedHundred) should ===(nelUnfoldedHundred)
}

test("Cofree.ana") {
val anaHundred: CofreeNel[Int] = Cofree.ana[Option, List[Int], Int](List.tabulate(101)(identity))(l => if (l.tail.isEmpty) None else Some(l.tail), _.head)
val nelUnfoldedHundred: NonEmptyList[Int] = NonEmptyList.fromListUnsafe(List.tabulate(101)(identity))
cofNelToNel(anaHundred) should ===(nelUnfoldedHundred)
}

test("Cofree.tailForced") {
val spooky = new Spooky
val incrementor =
Expand Down