tags - Canonical way to work on Option elements in Scala -
as example, want apply function f: (int,int) => int
2 elements of type option[int]
. thoughts (a,b).zipped.map(f), yields list, , want new option[int] result.
scala> def f(a:int,b:int) = a*b f: (a: int, b: int)int scala> val x = some(42) x: some[int] = some(42) scala> val y:option[int] = none y: option[int] = none scala> (x,y).zipped.map(f)//i want none result here res7: iterable[int] = list()
how can done without explicitly branching?
just many other operations in scala can done via comprehension:
def f(a:int,b:int) = a*b (x <- maybex; y <- maybey) yield f(x, y)
Comments
Post a Comment