3 ms·
> In Scala we're forced to manually extract the tuple when the recipient function takes more than a single argument. Is there no `uncurry :: (a -> b -> c) -> (
by masklinn 12y ago
> In Scala we're forced to manually extract the tuple when the recipient function takes more than a single argument.
Is there no `uncurry :: (a -> b -> c) -> (a, b) -> c` in Scala?
Prelude> let ap = flip map
Prelude> [(1, 2), (3, 4)] `ap` (+)
<interactive>:11:3:
No instance for (Num t0) arising from the literal `1'
The type variable `t0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the expression: 1
In the expression: (1, 2)
In the first argument of `ap', namely `[(1, 2), (3, 4)]'
Prelude> [(1, 2), (3, 4)] `ap` (uncurry (+))
[3,7]
Or some sort of `apply` method aliasing an n-ary function to a 1-ary function taking an n-tuple?
edit: there's a Function.tupled method so something like this might work:
List((1,2),(3,4)) map plus.tupled
- ssmoot 12y agore: Your edit: Basically. Lift the method to a function first. def plus(a: Int, b: Int): Int = a + b List( (1,2), (3,4) ) map(plus _ tupled) > List(3, 7) ( I know there's already 4 or 5 answers... I just think mine's the prettiest. ;-) )