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

Use poly lambda syntax in FreeApplicative test #2213

Merged
merged 3 commits into from
Mar 26, 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
28 changes: 8 additions & 20 deletions free/src/test/scala/cats/free/CofreeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ class CofreeSuite extends CatsSuite {

val unfoldedHundred: Cofree[Option, Int] = Cofree.unfold[Option, Int](0)(i => if (i == 100) None else Some(i + 1))
test("Cofree.mapBranchingS/T") {
val toList = new (Option ~> List) {
override def apply[A](lst: Option[A]): List[A] = lst.fold[List[A]](Nil)(_ :: Nil)
}
val toList = λ[Option ~> List](_.toList)
val toNelS = unfoldedHundred.mapBranchingS(toList)
val toNelT = unfoldedHundred.mapBranchingT(toList)
val nelUnfoldedOne: NonEmptyList[Int] = NonEmptyList.fromListUnsafe(List.tabulate(101)(identity))
Expand All @@ -100,9 +98,7 @@ class CofreeSuite extends CatsSuite {

val folder: (Int, Option[NonEmptyList[Int]]) => EvalOption[NonEmptyList[Int]] =
(i, lb) => if (i > 100) OptionT.none else OptionT.some(NonEmptyList(i, lb.fold[List[Int]](Nil)(_.toList)))
val inclusion = new (Eval ~> EvalOption) {
override def apply[A](fa: Eval[A]): EvalOption[A] = OptionT.liftF(fa)
}
val inclusion = OptionT.liftK[Eval]

val cataHundred =
Cofree.cataM[Option, EvalOption, Int, NonEmptyList[Int]](unfoldedHundred)(folder)(inclusion).value.value
Expand Down Expand Up @@ -151,20 +147,12 @@ sealed trait CofreeSuiteInstances {
}
}

val nelToCofNel = new (NonEmptyList ~> CofreeNel) {
override def apply[A](fa: NonEmptyList[A]): CofreeNel[A] =
Cofree[Option, A](fa.head, Eval.later(fa.tail.toNel.map(apply)))
}

val cofNelToNel = new (CofreeNel ~> NonEmptyList) {
override def apply[A](fa: CofreeNel[A]): NonEmptyList[A] =
NonEmptyList[A](fa.head, fa.tailForced.fold[List[A]](Nil)(apply(_).toList))
}

val cofRoseTreeToNel = new (CofreeRoseTree ~> NonEmptyList) {
override def apply[A](fa: CofreeRoseTree[A]): NonEmptyList[A] =
NonEmptyList[A](fa.head, fa.tailForced.flatMap(apply(_).toList))
}
val nelToCofNel = λ[NonEmptyList ~> CofreeNel](fa =>
Cofree(fa.head, Eval.later(fa.tail.toNel.map(apply))))

val cofNelToNel = λ[CofreeNel ~> NonEmptyList](fa =>
NonEmptyList(fa.head, fa.tailForced.map(apply(_).toList).getOrElse(Nil)))

val cofRoseTreeToNel = λ[CofreeRoseTree ~> NonEmptyList](fa =>
NonEmptyList(fa.head, fa.tailForced.flatMap(apply(_).toList)))
}
5 changes: 2 additions & 3 deletions free/src/test/scala/cats/free/FreeApplicativeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ class FreeApplicativeSuite extends CatsSuite {

test("FreeApplicative#flatCompile") {
forAll { (x: FreeApplicative[Option, Int]) =>
val nt: Option ~> FreeApplicative[Option, ?] = new FunctionK[Option, FreeApplicative[Option, ?]] {
def apply[A](a: Option[A]): FreeApplicative[Option, A] = FreeApplicative.lift(a)
}
val nt = λ[FunctionK[Option, FreeApplicative[Option, ?]]](FreeApplicative.lift(_))

x.foldMap[FreeApplicative[Option, ?]](nt).fold should === (x.flatCompile[Option](nt).fold)
}
}
Expand Down