val user = getUser();
val userName = user match {
case Some(u) => user.lastName
case _ => "No user found"
}
On Thu, Jul 21, 2011 at 10:19 AM, Garrett Rowe <gmrow...@gmail.com> wrote:
> val username = user map (_.lastName) getOrElse "No user found"
>
Greetings all,The method getUser() returns an Option[User] and I need to get a the lastName property or an alternative string value. Here is what I'm writing it as:val user = getUser();
val userName = user match {
case Some(u) => user.lastName
case _ => "No user found"
}
I was just wondering if this is idiomatic Scala and if we can achieve the same result in fewer characters!Thanks.ATTA
On 22/07/11 03:49, √iktor Ҡlang wrote:
> This is the exact reason I'd like to have "fold" on Option so you
> could do this:
>
> getUser.fold("No user found", _.lastName)
You do, it's just call map...getOrElse instead.
getUser map (_.lastName) getOrElse "No user found"
Remember, fold buys you nothing but syntax. It is no higher in
abstraction than pattern-matching. Its primary practical benefit would
be for its partial application. It is also useful for documentation --
reading a fold might give you the full algebra much easier than
reading case classes, etc.
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4ok/kACgkQmnpgrYe6r609jQCeKutm1tmiDp4x2ZPeIyxSQEed
0z4An292k/0fTb+T6HTsPEX7sXdsvw8s
=gJ6O
-----END PGP SIGNATURE-----
fold buys you nothing but syntax
An instance of an anonymous subclass (of Object, because no other
class was named after 'new') that has that fold[X](...) method.
> And whatever the type of object, it has another
> function in it called fold with parameterized as well and take two
> parameters! And I don't understad what's going on with none and some!
fold takes two parameters, what to do if `o` is None and what to do if
`o` is Some(a). They both must return the same type X, but it can be
any type. `none` is a lazy evaluation (that's the '=>') of a block,
and `some` is a function that takes a T -- the type of `a`, whatever
that is -- and returns an X. The implicit def means that fold can be
used as if it were a method of Option, so you can write
someOption.fold({ /* what to do if someOption is None */}, {a => /*
what to do with a if someOption is Some(a) */ })
The implementation -- o map some getOrElse none -- maps Some[T](a) to
Some[X](some(a)) and None[T] to None[X], then returns some(a) for the
Some case or none (the lazy evaluation finally occurs) for the None
case.
-- Jim
Thank you very much, Jim. That was very helpful.Two more questions while we're on the topic. Firstly, named parameters and lazy evaluation (denoted by =>) are the same thing?
Lastly, why do we need an anonymous object just for fold method? Couldn't we write a simple function -- the plain old implicit def a2b(...) -- to get the same effect?
Yeah, sorry for the misuse of terminology. I should have said
call-by-name, which is evaluated on each occurrence, not "lazy"
(call-by-need), which is evaluated at most once. (Of course, in this
example `none` occurs only once so there's no difference in effect.)
-- Jim
Yes, except that lazy evaluation (as opposed to call-by-name)
technically means only evaluating upon the first dereference -- sorry
for leading you astray on that. Scala doesn't (yet) have lazy
parameters.
-- Jim
2011/7/22 atta ur rehman <attaur...@gmail.com>:
> Makes perfect sense, Viktor. Thanks.Yes, except that lazy evaluation (as opposed to call-by-name)
> Just to make sure I have got it right on the named vs. call by name params:
> by default we can *always* call a function using it's parameters names
> instead of their order, like:
> scala> def bazz(one: Int, two: Int) = two + one
> bazz: (one: Int,two: Int)Int
> scala> bazz(two = 2, one = 1)
> res0: Int = 3
> I say always because you have to specify name and type for all the arguments
> to a function.
> Call-by-name, or lazy eval, on the other hand, forces evaluation on each
> dereferencing. And only difference between these two is => after the
> call-by-name argument. And the only reason I can think of having a
> call-by-name parameter is to let use pass a block of code that we want to
> execute only within our method. Lazily, that is.
> Have I finally nailed it?
technically means only evaluating upon the first dereference -- sorry
for leading you astray on that. Scala doesn't (yet) have lazy
parameters.
Yes, except that lazy evaluation (as opposed to call-by-name)
technically means only evaluating upon the first dereference -- sorry
for leading you astray on that. Scala doesn't (yet) have lazy
parameters.
-- Jim
If anyone wants to go vote for it and/or watch it, the ticket on this is
https://issues.scala-lang.org/browse/SI-240
--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
That's a great tip, and will eliminate a grip of needlessly verbose
`match` statements in my code, thanks!
I've been learning the XML library and want to find a shorter way to express
(node \ "Subnode") match { case Seq(subnode) => Some(subnode); case
Seq() => None }
More generally, trying to convert a sequence of zero or one elements
into an Option. Any tips?
Christopher
--
Jim
On Fri, Jul 22, 2011 at 09:26:57AM -0700, Christopher Currie wrote:
> Date: Fri, 22 Jul 2011 09:26:57 -0700
> From: Christopher Currie <chris...@currie.com>
> To: Kevin Wright <kev.lee...@gmail.com>
> Cc: john sullivan <sullym...@gmail.com>, Garrett Rowe
> <gmrow...@gmail.com>, scala...@googlegroups.com
> Subject: Re: [scala-user] Is there is shorter way to this code?