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

Simplify bindUntilTrue, evalFilter #644

Merged
merged 2 commits into from
Jul 1, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ package object util {
* res1: List[Int] = List(0, 1, 2)
* }}}
*/
def bindUntilTrue[G[_]: Foldable, F[_]: Monad](gfb: G[F[Boolean]]): F[Boolean] = {
def loop(list: List[F[Boolean]]): F[Boolean] =
list match {
case x :: xs => x.ifM(true.pure[F], loop(xs))
case Nil => false.pure[F]
}
loop(gfb.toList)
}
def bindUntilTrue[G[_]: Foldable, F[_]: Monad](gfb: G[F[Boolean]]): F[Boolean] =
gfb.existsM(identity)
Copy link
Member

Choose a reason for hiding this comment

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

Nice! 👍


def divideOnError[F[_], G[_], A, B, E](a: A)(f: A => F[B])(divide: A => G[A])(
handleError: (A, E) => F[B]
Expand All @@ -78,7 +72,7 @@ package object util {
}

def evalFilter[F[_]: Functor, A](p: A => F[Boolean]): Pipe[F, A, A] =
_.evalMap(a => p(a).map(b => (a, b))).collect { case (a, true) => a }
_.evalMap(a => p(a).tupleLeft(a)).collect { case (a, true) => a }

def halve[C](c: C)(implicit ev: C => TraversableLike[_, C]): Either[C, (C, C)] = {
val size = c.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class utilTest extends FunSuite with Matchers with ScalaCheckPropertyChecks {
}
}

test("halve: first halve is at most one element longer than the second") {
test("halve: first half is at most one element longer than the second") {
forAll { l: List[Int] =>
val (fst, snd) = halve(l).leftMap(lst => (lst, List.empty[Int])).merge
(fst.size - snd.size) should equal(0).or(equal(1))
Expand Down