scala - How can I reverse of flow of Option Monad? -
say, have bunch of "validation" functions return none if there no error, otherwise return some(string) specifying error message. following ...
def validate1:option[string] def validate2:option[string] def validate3:option[string]
i going call them in sequence , 1 returns some(string), stop , return same. if returns none, go next until sequence over. if of them return none, return none.
i glue them in "for expression". ...
for( <- validate1; b <- validate2; c <- validate3) yield none;
however, option flows opposite want here. stops @ none , follows some(string).
how can achieve that?
you chain calls orelse method on option
validate1 orelse validate2 orelse validate3
or run fold on collection of validate methods converted functions
val vlist= list(validate1 _, validate2 _, validate3 _) vlist.foldleft(none: option[string]) {(a, b) => if (a == none) b() else a}
Comments
Post a Comment