Scala collections: why do we need a case statement to extract values tuples in higher order functions? -
related tuple unpacking in map operations, don't understand why need case
(that looks partial function me) extract values tuple, that:
arrayoftuples map {case (e1, e2) => e1.tostring + e2}
instead of extracting in same way works in foldleft
, example
def sum(list: list[int]): int = list.foldleft(0)((r,c) => r+c)
anyway don't specify type of parameters in first case, why need case
statement?
because in scala function argument lists , tuples not unified concept in haskell , other functional languages. function:
(t: (int, int)) => ...
is not same thing function:
(e1: int, e2: int) => ...
in first case can use pattern matching extract tuple elements, , that's done using case
syntax. actually, expression:
{case (e1, e2) => ...}
is shorthand for:
t => t match {case (e1, e2) => ...}
there has been discussions unifying tuples , function argument lists, there complications regarding java overloading rules, , default/named arguments. so, think it's unlikely concepts ever unified in scala.
Comments
Post a Comment