scala - Best FP idioms for AnyVals in collections -
i have mapper function defined such:
def foo(x:int) = if (x>2) x*2
the type signature of method beingness int => anyval
. if map function on list of integers:
scala> list(-1,3,-4,0,5).map(foo) res0: list[anyval] = list((), 6, (), (), 10)
i need way of filtering out unit
s int
s so:
scala> res0.filter(_.isinstanceof[int]).map(_.asinstanceof[int]) res1: list[int] = list(6, 10)
everything seems concisely expressed until have filter-map on res0
extract values care about. utilize matchers or if-else in foo
ensure homecoming int
i'd still need filter unwanted values resulting map operation.
can of well-seasoned scala developers reading provide additional insight what's or bad approach collection grows big (e.g. maybe collection distributed spark rdd
)? there more idiomatic ways of doing functionally?
in case suggest utilize collect partialfunction, if need drop ints smaller 2
val foo: partialfunction[int, int] = { case x if x > 2 => x*2 } println(list(-1,3,-4,0,5).collect(foo))
your original foo has type int => anyval, because scalac transforms in like
def foo(x: int) = if (x > 2) x*2 else () // () === unit
and mutual super type int , unit anyval
scala functional-programming apache-spark scala-collections
No comments:
Post a Comment