scala cats - How to transform a Kleisli[Try, I, A] into I => A -
is there existing combinator or better way write ?
def combine[i, a](k: kleisli[try, i, a], default: a) : => = k.run.andthen(_.getorelse(default))
it possible mapping on monadic value using identity:
def combine[i, a](k: kleisli[try, i, a], default: a): => = k.mapf[id, a](_.getorelse(default)).run
example:
scala> val div = kleisli[try, int, string](x => try(1000 / x).map(_.tostring)) div: cats.data.kleisli[scala.util.try,int,string] = kleisli(<function1>) scala> combine(div, "good")(10) res0: string = 100 scala> combine(div, "division zero")(0) res1: string = division 0
Comments
Post a Comment