Shams Tabraiz Alam Shams Tabraiz Alam - 6 months ago 27
Scala Question

Difference between reduceByKey(_+_) and reduceByKey (_++_) in Scala

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 (+).


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.