Reputation: 1747
I have the following expression and was hoping to learn Scala more by knowing how to simplify it.
val r : Either[Exception, Long] = Right(100)
r fold (_ => (), uuid => account.setAccountUuid(uuid.toString))
Is it possible to make it even less verbose than this?
Thanks!
Upvotes: 0
Views: 181
Reputation: 15690
Forgive my troglodytism:
if ( r.isRight ) account.setAccountUuid(r.right.get.toString)
Doesn't readability count for anything??? Sometimes the Scala community seems to be all zealous hammer-holders -- sorry, I meant monad-holders.
Updated to correct TravisBrown's catch (missing "get").
Upvotes: 1
Reputation: 32719
My initial answer was r.right foreach (account.setAccountUuid(_.toString))
but it turns out that it triggers the dreade "missing parameter type for expanded function" error. I should have ssen this one coming.
How about:
for (i <- r.right) account.setAccountUuid(i.toString)
Upvotes: 6