I'm trying to use Kleisli to compose functions returning a monad. It works for an Option:
import cats.data.Kleisli
import cats.implicits._
object KleisliOptionEx extends App {
case class Failure(msg: String)
sealed trait Context
case class Initial(age: Int) extends Context
case class AgeCategory(cagetory: String, t: Int) extends Context
case class AgeSquared(s: String, t: Int, u: Int) extends Context
type Result[A, B] = Kleisli[Option, A, B]
val ageCategory: Result[Initial,AgeCategory] =
Kleisli {
case Initial(age) if age < 18 => {
Some(AgeCategory("Teen", age))
}
}
val ageSquared: Result[AgeCategory, AgeSquared] = Kleisli {
case AgeCategory(category, age) => Some(AgeSquared(category, age, age * age))
}
val ageTotal = ageCategory andThen ageSquared
val x = ageTotal.run(Initial(5))
println(x)
}
But I can't make it to work with an Either... :
import cats.data.Kleisli
import cats.implicits._
object KleisliEx extends App {
case class Failure(msg: String)
sealed trait Context
case class Initial(age: Int) extends Context
case class AgeCategory(cagetory: String, t: Int) extends Context
case class AgeSquared(s: String, t: Int, u: Int) extends Context
type Result[A, B] = Kleisli[Either, A, B]
val ageCategory: Result[Initial,AgeCategory] =
Kleisli {
case Initial(age) if age < 18 => Either.right(AgeCategory("Teen", age))
}
val ageSquared : Result[AgeCategory,AgeSquared] = Kleisli {
case AgeCategory(category, age) => Either.right(AgeSquared(category, age, age * age))
}
val ageTotal = ageCategory andThen ageSquared
val x = ageTotal.run(Initial(5))
println(x)
}
I guess Either has two type parameters, and a Kleisle wrapper needs one input and one output type parameters. I don't how could I hide the left type from the Either...