我正在玩基本的Kleisli example
import cats.data.Kleisli
import cats.FlatMap
val parse = Kleisli[Option, String, Int](
(s: String) => try {
Some(s.toInt)
} catch {
case _: NumberFormatException => None
}
)
val reciprocal = Kleisli[Option, Int, Double](
(i: Int) =>
if (i == 0)
None
else
Some(1.0 / i))
val c = reciprocal.compose(parse)
两件事让我感到高兴
c
之类的c("5")
是否真的需要手动提供FlatMap[Option]
或者我是否缺少隐含的导入?我可以添加
implicit val optionFlatmap = new FlatMap[Option] {
override def flatMap[A, B](fa: Option[A])(f: (A) => Option[B]): Option[B] = fa.flatMap(f)
override def map[A, B](fa: Option[A])(f: (A) => B): Option[B] = fa.map(f)
}
评估c("5")
,但是没有针对此事的默认实现?