Shams Tabraiz Alam - 1 month ago 7x

Scala Question

What is the difference between reduceByKey(*+*) and reduceByKey (*++*). I know the internal working of reduceByKey(*+*). But how reduceByKey (*++*) works internally. I have google reduceByKey (*++*) but not found any resource everyone is talking about reduceByKey (*+*).

Answer

`reduceByKey`

on a collection of elements of type `T`

expects a **binary operation of type Fucntion2[T, T, U]**, which means it expects a function from two arguments with type

`T`

to a value with type `U`

. - If
`+`

is defined for type`T`

(e.g. if`T`

is`Int`

,`Double`

...), then`_ + _`

adheres to this expected type. - If
`++`

is defined for type`T`

(e.g. when`T`

is`List`

), then`_ ++ _`

adheres to this type. - If neither is correct (e.g.
`T`

is your own class not defining any of these operators) then neither would compile.

Source (Stackoverflow)

Comments