Created
March 14, 2017 22:33
-
-
Save travisbrown/46a7801f3d2adc1a09bea25f15829519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.{ Applicative, Eval, Foldable, Monoid }, cats.implicits._ | |
def foldMapM[F[_]: Foldable, G[_]: Applicative, A, B: Monoid](fa: F[A])(f: A => G[B]): G[B] = | |
fa.foldRight(Eval.always(Monoid[B].empty.pure[G]))((a, acc) => f(a).map2Eval(acc)(_ |+| _)).value | |
def sumIfSmall(s: Stream[Int]) = foldMapM(s) { | |
case n if n < 100 => Right(n) | |
case n => Left(n) | |
} | |
sumIfSmall(Stream.from(0)) // Left(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
newb question: Left and Right are from?