Lift as something named "tryo" for that :
http://scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/util/ControlHelpers.html
Scalaz certainly have something to deal with that, but I don't know what
(I suspect it will imply I/O monad and applicative functors ;)
Hope it helps,
--
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com
-------- Original-Nachricht --------
> Datum: Fri, 23 Dec 2011 06:46:55 -0500
> Von: Rex Kerr <ich...@gmail.com>
> An: scala-user <scala...@googlegroups.com>
> Betreff: [scala-user] Option processing / exception handling tricks
Am Freitag, 23. Dezember 2011, 15:18:14 schrieb Rex Kerr:
> Maybe my use case wasn't clear, but Either doesn't help at all; it's
> exactly the same as Option.
>
> I do, in fact, use Either when I care why something went wrong (not just
> that something went wrong). It doesn't help propagate exceptional cases
> out of a local context, however, without creating stack traces, etc..
>
> --Rex
>
Maybe it's naive - but I always thought that control flow exceptions are a good tool for those cases.
I used it but I never measured their performance first place so it's just a guess.
Have you ever tried "Exception with ControlThrowable"?
Greetings
Bernd
No reason you can't write
Some(complicated stuff).flatMap(optionalStuff)
Or just use Scalaz's Validation, the gateway drug to Scalaz:
for {
result1 <- complicatedStuff.success
result2 <- stuffThatMayFail
} yield whatever(result1, result2)
Validation is just Either with a stricter convention for which part is
failure and with is success. There are some good tutorials online,
too.
N.
--
Noel Welsh
Untyped Ltd http://www.untyped.com/
+44 (0) 121 285 2285 in...@untyped.com
UK company number 06226466
If I've understood correctly, I've been doing something broadlysimilar
and using scalaz Validations.What I've found is that being very
specific about which arguments areOption[x] and which are necessary
leaves me with something like
type Validated[T] = Validation[String, T]
for { optX <- getOptionalX y <- getY} yield complicatedThing(optX, y)
def getX: Validated[Option[X]]def getY: Validated[Y]
Now it gets more complicated if you want to do things dependent
onoptX, but in that case I've maintained my visible control flow
byresolving the dependent optional thing in the get action. So if z
isoptional and depends on optX I do
for { optX <- getOptionalX optZ <- getOptionalZFromOptionalX(optX) y
<- getY} yield complicatedThing(optZ, y)
def getOptionalZFromOptionalX(optX: Option[X]): Validated[Option[Z]]
NB they are all using <- rather than = because I'm validating
againstmore catastrophic failures than the option not being there.
What I have found using this approach with Validation is that you
endup threading your Validation through everything which is fine if
youcan live with it.
PS besides tidying up the code the type Validated[T] =
Validation[String, T] (I actually use something other than String)
enables you to eliminate the rather ugly type lambda syntax when using
.sequence as Tony M. pointed out in a throw-away line a few weeks back
Tim
I guess I don't see what's wrong with:
for {
idx <- binarySearch(timeSlots, user.time)
record <- records(user.name)
} yield (record -> idx)
I write this everywhere, using Scalaz's Validations. My Validation
type is typically
type Validated[T] = Validation[FailCode, T]
Assume both binarySearch and records return Validated
(Using the terminology from Tim's post.)
A FailCode is something I can turn into a response to the user, and
into something I can log (they have different requirements for the
information they contain).
You seem to have an issue with this kind of code. I like have the type
system enforce error handling. If one wanted one could like lots of
operations (e.g. ->) to monads, so you could write
records(user.name) -> binarySearch(timeSlots, user.time)
and it would still return a Validation. This would be a lot of work
and would have odd interactions with Predef.
Have you seen scala.utility.control.Exceptions.catching ?
val optResult = catching(classOf[NumberFormatException]) opt doFun
On Fri, Dec 23, 2011 at 7:53 PM, Rex Kerr <ich...@gmail.com> wrote:I guess I don't see what's wrong with:
> Let's take an example of validating an input form. First, we have
> unvalidated code (and let's assume that the form itself makes sure the types
> are correct):
>
> import java.util.{Arrays => Jua}
>
> def userSlot(user: User) = {
> records(user.name) -> Jua.binarySearch(timeSlots, user.time)
> }
for {
idx <- binarySearch(timeSlots, user.time)
record <- records(user.name)
} yield (record -> idx)
Assume both binarySearch and records return Validated
On 12/27/2011 04:00 AM, Rex Kerr wrote:
> That is, when I have F[A,M[B]], how do I most effectively build M[F[A,B]]?
You use sequence.
This function has an entire article written on it.
http://etorreborre.blogspot.com/2011/06/essence-of-iterator-pattern.html
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJO+O6IAAoJEPxHMY3rBz0PTwQIAL/fsDSx2/MS9Qis54X/lyr7
87j5rqKQm/eiyT82QfRPocUNP/zGIYR71R3dcgKSkK+blniCbMtfxHTTMIo1VYra
Llj3GyZmJDB6BGH0Z1la5GIsu7ET3eOdGNzkS3uDJ1DDhLE7Xe8ZbTiA7JiXG0tb
zg8bN8xsXDkgvT5jmB9AfnJpYEsszWRa5y0PmoCpzSjwgwlXwQzaWE8inFSEiDp/
MlmC4r7PJQuQ9p2DZnnPnn07ImjW+XVWOdtAXSLefp5Z4OyrFYHxyqc+NpkXH1d7
nIMu/79BX+4E3VIUu/7N3q6QYyTPEcXmNHh57HXUBxFmqiq53TBwvGM1rJWCfkM=
=2RPl
-----END PGP SIGNATURE-----
On Dec 25, 2011 5:29 AM, "Noel Welsh" <no...@untyped.com> wrote:
>
> records(user.name) -> binarySearch(timeSlots, user.time)
records(user.name) <<*>> binarySearch(timeSlots, user.time)
<<*>> lifts pairing into an applicative functor: F[A] => F[B] => F[(A, B)]