Created
April 7, 2019 15:19
-
-
Save yasuabe/bcb4aac044d1d4c3002eb65e4c18aeff to your computer and use it in GitHub Desktop.
sample code for Cats MTL FunctorRaise
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.mtl.FunctorRaise | |
import cats.syntax.applicative._ | |
import cats.instances.string._ | |
import cats.{Applicative, Semigroupal} | |
import cats.data.Validated | |
def parseNumber[F[_]: Applicative](in: String)(implicit F: FunctorRaise[F, String]): F[Double] = | |
if (in.matches("-?[0-9]+")) in.toDouble.pure[F] | |
else F.raise(in) // raise 構文 | |
import cats.mtl.instances.handle._ | |
val result: Validated[String, (Double, Double, Double)] = Semigroupal.tuple3( | |
parseNumber[Validated[String, ?]]("abc"), | |
parseNumber[Validated[String, ?]]("123"), | |
parseNumber[Validated[String, ?]]("xyz") | |
) | |
// result = Invalid(abcxyz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment