Let's define a Kleisli
on \/
:
abstract class MyError
case class NumericalError(msg: String) extends MyError
// Either is a Monad with two type parameters: M[A,B] which represent left and right respectively
// Let's create an ad-hoc type
type EEither[+T] = \/[MyError, T]
and one ad-hoc function for testing purposes:
def safeSqrtEither(t: Double): EEither[Double] =
safeSqrtOpt(t) match {
case Some(r) => r.right
case None => NumericalError("Sqrt on double is not define if _ < 0").left
}
val kSafeSqrtEither = Kleisli.kleisli( (x: Double) => safeSqrtEither(x) )
Function composition works smoothly:
val pipeEither = kSafeSqrtEither >>> kSafeSqrtEither
val r5b = pipeEither2 run 16.0
//which gives r5b: EEither[Double] = \/-(2.0)
I'd like to add logging:
type LoggedROCFun[I,O] = I => WriterT[EEither,scalaz.NonEmptyList[String],O]
val sqrtWithLog: LoggedROCFun[Double, Double] =
(t: Double) =>
WriterT.put(kSafeSqrtEither(t))(s"squared $t".wrapNel)
which seems having the desired behaviour:
val resA = sqrtWithLog(16.0)
// resA: scalaz.WriterT[EEither,scalaz.NonEmptyList[String],Double] = WriterT(\/-((NonEmpty[squared 16.0],4.0)))
Sleek. However, I am struggling to put together an operator which:
- combines the values in the
WriterT
applying>>>
- chains (appends) each log, keeping track of each step made
Desired output:
val combinedFunction = sqrtWithLog >>> sqrtWithLog
val r = combinedFunction run 16.0
// r: WriterT(\/-((NonEmpty[squared 16.0, squared 4.0],2.0)))
My best shot:
def myCompositionOp[I,A,B](f1: LoggedROCFun[I,A])(f2: LoggedROCFun[A,B]): LoggedROCFun[I,B] =
(x: I) => {
val e = f1.apply(x)
val v1: EEither[A] = e.value
v1 match {
case Right(v) => f2(v)
case Left(err) =>
val lastLog = e.written
val v2 = err.left[B]
WriterT.put(v2)(lastLog)
}
}
In the above I first apply f1
to x
, and then I pass along the result to f2
. Otherwise, I short-circuit to Left
.
This is wrong, because in the case Right
I am dropping the previous logging history.
One last Q
val safeDivWithLog: Kleisli[W, (Double,Double), Double] =
Kleisli.kleisli[W, (Double, Double), Double]( (t: (Double, Double)) => {
val (n,d) = t
WriterT.put(safeDivEither(t))(s"divided $n by $d".wrapNel)
}
)
val combinedFunction2 = safeDivWithLog >>> sqrtWithLog
val rAgain = combinedFunction2 run (-10.0,2.0)
// rAgain: W[Double] = WriterT(-\/(NumericalError(Sqrt on double is not define if _ < 0)))
Not sure why the logs are not carried through after a pipeline switches to Left
. Is it because:
- type MyMonad e w a = ErrorT e (Writer w) a is isomorphic to (Either e a, w)
- type MyMonad e w a = WriterT w (Either e) a is isomorphic to Either r (a, w)
therefore I have flipped the order?
Sources: here, scalaz, here, and real world haskell on transformers