sometimes I write code like the following:
val item1 = list.find(_.source.name.equals("foo"))
if (item1.isDefined) doSomething1(item1)
else {
val item2 = list.find(_.dest.name.equals("bar"))
if (item2.isDefined) doSomething2(item2)
else doSomethingElse()
}
Does anyone have a nicer Scala syntax for laying this code out?
I’d write this as
but that is because I have added the method
foldtoOptionlike so:If you don’t want to add the
foldmethod, you can also use themap,getOrElsepair:which is not much more verbose.