Created
April 7, 2019 15:15
-
-
Save yasuabe/f8cdba366fe4d2355bd2a094bdbcae79 to your computer and use it in GitHub Desktop.
sample code for Cats MTL MonadChronicle
This file contains hidden or 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.Monad | |
import cats.data.{Chain, Ior} | |
import cats.mtl.MonadChronicle | |
import cats.syntax.applicative._ | |
import cats.syntax.functor._ | |
import cats.mtl.instances.chronicle._ | |
import cats.syntax.traverse._ | |
import cats.instances.list._ | |
def func[F[_]: Monad](n: Int)(implicit F: MonadChronicle[F, Chain[Int]]): F[Int] = | |
if (n > 4) F.confess(Chain(-n)) // FunctorRaise#raise 相当 | |
else if (n > 2) F.dictate(Chain(n)) as n // FunctorTell#tell 相当 | |
else n.pure[F] | |
type IorC[A] = Ior[Chain[Int], A] | |
(1 to 1).toList.traverse(func[IorC]) // Right(List(1)) | |
(1 to 2).toList.traverse(func[IorC]) // Right(List(1, 2)) | |
(1 to 3).toList.traverse(func[IorC]) // Both(Chain(3),List(1, 2, 3)) | |
(1 to 4).toList.traverse(func[IorC]) // Both(Chain(3, 4),List(1, 2, 3, 4)) | |
(1 to 5).toList.traverse(func[IorC]) // Left(Chain(3, 4, -5)) | |
(1 to 6).toList.traverse(func[IorC]) // Left(Chain(3, 4, -5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment