diff options
Diffstat (limited to 'core')
5 files changed, 31 insertions, 74 deletions
diff --git a/core/src/main/scala/com/codiff/fairstream/Fair.scala b/core/src/main/scala/com/codiff/fairstream/Fair.scala index 84255db..72a9ebe 100644 --- a/core/src/main/scala/com/codiff/fairstream/Fair.scala +++ b/core/src/main/scala/com/codiff/fairstream/Fair.scala @@ -36,14 +36,14 @@ object Fair { def guard(cond: Boolean): Fair[Unit] = if (cond) unit(()) else empty def mplus[A](left: Fair[A], right: => Fair[A]): Fair[A] = left match { - case Nil => Incomplete(right) - case One(a) => Choice(a, right) + case Nil => Incomplete(right) + case One(a) => Choice(a, right) case c: Choice[A @unchecked] => Choice(c.a, mplus(right, c.rest)) case inc: Incomplete[A @unchecked] => right match { case Nil => inc case One(b) => Choice(b, inc.step) - case Choice(b, r2) => Choice(b, Incomplete(mplus(inc.step, r2))) + case Choice(b, r2) => Choice(b, mplus(inc.step, r2)) case Incomplete(j) => Incomplete(mplus(inc.step, j)) } } @@ -58,8 +58,8 @@ object Fair { if (maxResults.exists(_ <= 0)) acc.reverse else stream match { - case Nil => acc.reverse - case One(a) => (a :: acc).reverse + case Nil => acc.reverse + case One(a) => (a :: acc).reverse case Choice(a, r) => runM(maxDepth, maxResults.map(_ - 1), r, a :: acc) case Incomplete(i) => @@ -76,8 +76,8 @@ object Fair { def pure[A](a: A): Fair[A] = Fair.unit(a) def flatMap[A, B](fa: Fair[A])(f: A => Fair[B]): Fair[B] = fa match { - case Nil => Nil - case One(a) => f(a) + case Nil => Nil + case One(a) => f(a) case c: Choice[A @unchecked] => combineK(f(c.a), Incomplete(flatMap(c.rest)(f))) case i: Incomplete[A @unchecked] => diff --git a/core/src/main/scala/com/codiff/fairstream/FairT.scala b/core/src/main/scala/com/codiff/fairstream/FairT.scala index d84234a..f77e783 100644 --- a/core/src/main/scala/com/codiff/fairstream/FairT.scala +++ b/core/src/main/scala/com/codiff/fairstream/FairT.scala @@ -12,16 +12,21 @@ object FairE { } object Choice { - def apply[M[_], A](a: A, expr: => FairT[M, A]): Choice[M, A] = new Choice(a, expr) + def apply[M[_], A](a: A, expr: => FairT[M, A]): Choice[M, A] = + new Choice(a, expr) - def unapply[M[_], A](s: Choice[M, A]): Some[(A, FairT[M, A])] = Some((s.a, s.rest)) + def unapply[M[_], A](s: Choice[M, A]): Some[(A, FairT[M, A])] = Some( + (s.a, s.rest) + ) } class Incomplete[M[_], A](expr: => FairT[M, A]) extends FairE[M, A] { lazy val rest: FairT[M, A] = expr } object Incomplete { - def apply[M[_], A](expr: => FairT[M, A]): Incomplete[M, A] = new Incomplete(expr) + def apply[M[_], A](expr: => FairT[M, A]): Incomplete[M, A] = new Incomplete( + expr + ) def unapply[M[_], A](s: Incomplete[M, A]): Some[FairT[M, A]] = Some(s.rest) } @@ -63,7 +68,7 @@ object FairT { case FairE.Nil() => M.pure[E](inc) case FairE.One(b) => M.pure[E](FairE.Choice(b, inc.rest)) case rc: FairE.Choice[M, A] @unchecked => - M.pure[E](FairE.Choice(rc.a, FairT(M.pure[E](FairE.Incomplete(mplus(inc.rest, rc.rest)))))) + M.pure[E](FairE.Choice(rc.a, mplus(inc.rest, rc.rest))) case rinc: FairE.Incomplete[M, A] @unchecked => M.pure[E](FairE.Incomplete(mplus(inc.rest, rinc.rest))) } @@ -75,8 +80,8 @@ object FairT { )(f: A => FairT[M, B])(implicit M: Monad[M]): FairT[M, B] = { type EB = FairE[M, B] FairT(M.flatMap[FairE[M, A], EB](fa.run) { - case FairE.Nil() => M.pure[EB](FairE.Nil()) - case FairE.One(a) => f(a).run + case FairE.Nil() => M.pure[EB](FairE.Nil()) + case FairE.One(a) => f(a).run case c: FairE.Choice[M, A] @unchecked => mplus(f(c.a), suspend(flatMap(c.rest)(f))).run case i: FairE.Incomplete[M, A] @unchecked => @@ -92,8 +97,8 @@ object FairT { if (maxResults.exists(_ <= 0)) M.pure(List.empty) else M.flatMap(stream.run) { - case FairE.Nil() => M.pure(List.empty) - case FairE.One(a) => M.pure(List(a)) + case FairE.Nil() => M.pure(List.empty) + case FairE.One(a) => M.pure(List(a)) case c: FairE.Choice[M, A] @unchecked => M.map(runM(maxDepth, maxResults.map(_ - 1), c.rest))(c.a :: _) case inc: FairE.Incomplete[M, A] @unchecked => @@ -125,7 +130,8 @@ object FairT { case FairE.One(Left(a)) => Left(a) case FairE.One(Right(b)) => Right(FairE.One(b)) case c: FairE.Choice[M, Either[A, B]] @unchecked => - val rest: FairT[M, B] = FairT.flatMap[M, Either[A, B], B](c.rest)(cont) + val rest: FairT[M, B] = + FairT.flatMap[M, Either[A, B], B](c.rest)(cont) c.a match { case Right(b) => Right(FairE.Choice(b, rest)) case Left(a) => @@ -133,7 +139,9 @@ object FairT { } case inc: FairE.Incomplete[M, Either[A, B]] @unchecked => Right( - FairE.Incomplete(FairT.flatMap[M, Either[A, B], B](inc.rest)(cont)) + FairE.Incomplete( + FairT.flatMap[M, Either[A, B], B](inc.rest)(cont) + ) ) } }) diff --git a/core/src/main/scala/com/codiff/fairstream/Main.scala b/core/src/main/scala/com/codiff/fairstream/Main.scala deleted file mode 100644 index 3c7c0b7..0000000 --- a/core/src/main/scala/com/codiff/fairstream/Main.scala +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2026 codiff - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.codiff.fairstream - -import cats.effect.IO -import cats.effect.IOApp - -object Main extends IOApp.Simple { - - def run: IO[Unit] = - IO.println("Hello sbt-typelevel!") -} diff --git a/core/src/test/scala/com/codiff/fairstream/MainSuite.scala b/core/src/test/scala/com/codiff/fairstream/MainSuite.scala deleted file mode 100644 index 869c583..0000000 --- a/core/src/test/scala/com/codiff/fairstream/MainSuite.scala +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2026 codiff - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.codiff.fairstream - -import munit.CatsEffectSuite - -class MainSuite extends CatsEffectSuite { - - test("Main should exit succesfully") { - val main = Main.run.attempt - assertIO(main, Right(())) - } - -} diff --git a/core/src/test/scala/com/codiff/fairstream/PythagoreanSuite.scala b/core/src/test/scala/com/codiff/fairstream/PythagoreanSuite.scala index 8ab73ba..e4cf406 100644 --- a/core/src/test/scala/com/codiff/fairstream/PythagoreanSuite.scala +++ b/core/src/test/scala/com/codiff/fairstream/PythagoreanSuite.scala @@ -36,7 +36,8 @@ class PythagoreanSuite extends FunSuite { test("Fair: pythagorean triples with left recursion") { import Fair._ - lazy val number: Fair[Int] = mplus((Incomplete(number): Fair[Int]).map(_ + 1), unit(0)) + lazy val number: Fair[Int] = + mplus((Incomplete(number): Fair[Int]).map(_ + 1), unit(0)) val triples = for { i <- number @@ -73,7 +74,8 @@ class PythagoreanSuite extends FunSuite { _ <- guardF(i * i + j * j == k * k) } yield (i, j, k) - val results = FairT.runM[Eval, (Int, Int, Int)](None, Some(7), triples).value + val results = + FairT.runM[Eval, (Int, Int, Int)](None, Some(7), triples).value assertEquals(results.length, 7) assert(results.forall(isPythagorean)) } @@ -92,7 +94,8 @@ class PythagoreanSuite extends FunSuite { _ <- guardF(i * i + j * j == k * k) } yield (i, j, k) - val results = FairT.runM[Eval, (Int, Int, Int)](None, Some(27), triples).value + val results = + FairT.runM[Eval, (Int, Int, Int)](None, Some(27), triples).value assertEquals(results.length, 27) assert(results.forall(isPythagorean)) } |
