Skip to content

Instantly share code, notes, and snippets.

@hochgi
Created February 24, 2022 15:09
Show Gist options
  • Save hochgi/13273f495295e87e5118c5dc7517c431 to your computer and use it in GitHub Desktop.
Save hochgi/13273f495295e87e5118c5dc7517c431 to your computer and use it in GitHub Desktop.
just a place to keep deleted stuff to ease my sentiment (better than leaving commented out in the code)
implicit val decodeResultMonad: Monad[DR] = new Monad[DR] {
override def flatMap[A, B](fa: DR[A])(f: A => DR[B]): DR[B] = fa.flatMap(f)
override def tailRecM[A, B](a: A)(f: A => DR[Either[A, B]]): DR[B] = f(a) match {
case DR.Value(Left(aa)) => tailRecM(aa)(f)
case DR.Value(Right(b)) => DR.Value(b)
case failedDecodeResult =>
// scalastyle:off null
failedDecodeResult.map(_.getOrElse(null.asInstanceOf[B])) // stays DR.failure - map is not applied
// scalastyle:on null
}
override def pure[A](x: A): DR[A] = DR.Value(x)
}
def failFastFold[A, B, C, F[_]: Monad](coll: IterableOnce[A], z: C)
(decd: A => F[B])
(isok: F[C] => Boolean)
(fold: (C, B) => C): F[C] = {
val m = implicitly[Monad[F]]
// scalastyle:off var.local
var out: F[C] = m.pure(z)
// scalastyle:on var.local
val it = coll.iterator
while(it.hasNext && isok(out)) {
out = m.flatMap(decd(it.next())) { b =>
m.map(out) { c =>
fold(c, b)
}
}
}
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment