How to lazy evaluate functions in a sequence in Scala -
let's have 3 functions in class can return option[file]. looking nice way execute them in order , stop when first function evaluates (similar pick() lazy evaluation), i.e. don't want use nested if-else statements.
abstract class foo() { def first(): option[file] def second(): option[file] def third(): option[file] def pick(): option[file] = { seq(first(), second(), third()).find(_.isdefined).map(_.get) ) }
the parameter option.orelse
passed name, can use chain methods:
def pick(): option[file] = first orelse second orelse third
Comments
Post a Comment