Simplify expression

45 views
Skip to first unread message

Chris Marshall

unread,
Jun 1, 2012, 12:09:46 PM6/1/12
to sca...@googlegroups.com
suppose I have 

type VS[A] = Validation[String, A]

val v: VS[Option[A]]
val f: A => VS[B]

I want to get a result of type VS[Option[B]] but if v is of type Success(none), the result should also be a success(None). Here's an example:

scala> val v: VS[Option[String]] = some("4.5").success
v: VS[Option[String]] = Success(Some(4.5))

scala> val f = (s : String) => (try { s.toInt.success } catch { case x => x.getMessage.fail }): VS[Int]
f: String => VS[Int] = <function1>

Then:

scala> import Validation.Monad._
import Validation.Monad._

scala> (v map2 f map (_.sequence)).join
res4: scalaz.Validation[String,Option[Int]] = Failure(For input string: "4.5")

The success case is:

scala> val v: VS[Option[String]]= some("5").success
v: VS[Option[String]] = Success(Some(5))

scala> (v map2 f map (_.sequence)).join
res7: scalaz.Validation[String,Option[Int]] = Success(Some(5))

And the empty case is:

scala> val v: VS[Option[String]]= none[String].success
v: VS[Option[String]] = Success(None)

scala> (v map2 f map (_.sequence)).join
res6: scalaz.Validation[String,Option[Int]] = Success(None)

Is there a "nicer" way of doing this (possibly involving kleisli composition or monad transformers)?


Chris

Tony Morris

unread,
Jun 1, 2012, 12:19:54 PM6/1/12
to sca...@googlegroups.com
Take a look at OptionT.
--
Tony Morris
http://tmorris.net/


Chris Marshall

unread,
Jun 1, 2012, 12:44:50 PM6/1/12
to sca...@googlegroups.com
is this:

OptionT(v) flatMap f

Sorry - I'm still on v6, so I'm not very familiar with it.

Chris

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To post to this group, send email to sca...@googlegroups.com.
To unsubscribe from this group, send email to scalaz+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/scalaz?hl=en.


Lars Hupel

unread,
Jun 1, 2012, 12:53:29 PM6/1/12
to sca...@googlegroups.com
`OptionT[F, T]` is basically equivalent to `F[Option[T]]`. If `F` is a
monad, then so is `OptionT[F, _]`.

However, it is not as easy as a simple `flatMap`, because for that, your
`f` has to have the type

String => OptionT[VS, Int]

Your `f` has the type

String => VS[Int]

Thus, you have to `lift` the result type of `f` into `OptionT` (or you
can rewrite `f`. `OptionT` has a `Pure` instance, so this should be easy).

Lars Hupel

unread,
Jun 1, 2012, 12:55:26 PM6/1/12
to sca...@googlegroups.com
> Thus, you have to `lift` the result type of `f` into `OptionT` (or you
> can rewrite `f`. `OptionT` has a `Pure` instance, so this should be easy).

Sorry, I meant to write "lift" instead of `lift`. I'm not sure whether
such a function already exists.

eric thul

unread,
Jun 2, 2012, 12:51:19 PM6/2/12
to sca...@googlegroups.com
hi Chris,

Using scalaz7, the following looks like it might do what you want:

scala> :paste
// Entering paste mode (ctrl-D to finish)


import scalaz._, Scalaz._

type VS[A] = Validation[String, A]

val f = (s : String) => (try { s.toInt.success } catch { case x => x.getMessage.fail }): VS[Int]

val u: VS[Option[String]] = some("4.5").success
val ru = OptionT(u).flatMapF((s:String) => f(s))(Validation.validationMonad).run

val v: VS[Option[String]]= some("5").success
val rv = OptionT(v).flatMapF((s:String) => f(s))(Validation.validationMonad).run

val w: VS[Option[String]]= none[String].success
val rw = OptionT(w).flatMapF((s:String) => f(s))(Validation.validationMonad).run


// Exiting paste mode, now interpreting.

import scalaz._
import Scalaz._
defined type alias VS
f: String => VS[Int] = <function1>
u: VS[Option[String]] = Success(Some(4.5))
ru: VS[Option[Int]] = Failure(For input string: "4.5")
v: VS[Option[String]] = Success(Some(5))
rv: VS[Option[Int]] = Success(Some(5))
w: VS[Option[String]] = Success(None)
rw: VS[Option[Int]] = Success(None)


Best,
 -Eric

eric thul

unread,
Jun 2, 2012, 12:56:25 PM6/2/12
to sca...@googlegroups.com
ps - Sorry, should be: OptionT(u).flatMapF(f)(Validation.validationMonad).run

Chris Marshall

unread,
Jun 3, 2012, 5:11:26 AM6/3/12
to sca...@googlegroups.com
Thanks Eric

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To view this discussion on the web visit https://groups.google.com/d/msg/scalaz/-/XhAYcPGoCRwJ.

eric thul

unread,
Jun 3, 2012, 8:42:58 AM6/3/12
to sca...@googlegroups.com
welcome
Thanks Eric

To unsubscribe from this group, send email to scalaz+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages