I have two (or more) functions defined as:
val functionM: String => Option[Int] = s => Some(s.length)
val functionM2: Int => Option[String] = i => Some(i.toString)
I also have some data defined as:
val data: List[Option[String]] = List(Option("abc"))
My question is how to compose (in a nice way) the functions to get a result like:
data.map(_.flatMap(functionM).flatMap(functionM2))
res0: List[Option[String]] = List(Some(3))
I don't like the syntax of the above function calls. If i have many places like this then the code is very unreadable.
I tried to play with OptionT scalaz monad transformer, but it still has nested maps and also generates nested Options like:
OptionT(data).map(a => functionM(a).map(functionM2)).run
res2: List[Option[Option[Option[String]]]] = List(Some(Some(Some(3))))
What I want to achieve is something more or less like this:
Something(data).map(functionM).map(functionM2)
or even better:
val functions = functionM andThenSomething functionM2
Something(data).map(functions)
It would be nice if it could work with Try. As I know scalaz doesn't have TryT monad transformer, so is there any way to nicely compose functions which operates on Try?
val functions = functionM andThenSomething functionM2
withKleisli(functionM) >=> Kleisli(functionM2)
but I am not sure what do to next with this... – Bowknot