Dan L.
Dan L.

Reputation: 1747

how to simplify this expression in Scala?

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

Answers (3)

Ed Staub
Ed Staub

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

Jim Collins
Jim Collins

Reputation: 280

r fold (_ => (), account.setAccountUuid(_.toString))

Upvotes: 0

Régis Jean-Gilles
Régis Jean-Gilles

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

Related Questions