Advertisement
Guest User

anyway of getting rid of the explicit implicit params?

a guest
Apr 18th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.49 KB | None | 0 0
  1. def times[A](x: A, y: A*)
  2. (implicit ev: Numeric[A])
  3. : A = y.fold(x)(ev.times)
  4.  
  5. def plus[A](x: A, y: A*)
  6.            (implicit ev: Numeric[A])
  7. : A = y.fold(x)(ev.plus)
  8.  
  9. def timesAB[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
  10. (x:A, y:B)(implicit combine: NumericCombine.Aux[A, B, AB]): AB = {
  11.     combine.times(x,y)
  12. }
  13.  
  14. def plusAB[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
  15. (x:A, y:B)(implicit combine: NumericCombine.Aux[A, B, AB]): AB = {
  16.     combine.plus(x,y)
  17. }
  18.  
  19. def plusTuple2[A: Numeric,B: Numeric](x: (A, B), y: (A, B)): (A, B)
  20.                 = (plus(x._1, y._1), plus(x._2, y._2))
  21.  
  22. def accumulateWeightedValue[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
  23. (accum: (A, AB), valueWithWeight: (A, B))
  24. (implicit combine: NumericCombine.Aux[A, B, AB]): (A, AB) = {
  25.     val weightedValue = timesAB(valueWithWeight.weight, valueWithWeight.value) // implicit numeric param: good
  26.     (
  27.         plus(accum.weight, valueWithWeight.weight)(combine.numericA), //explicit numeric param: bad
  28.         plus(accum.value, weightedValue)(combine.numericAB) //explicit numeric param: bad
  29.     )
  30. }
  31.  
  32. def weightedSum[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
  33. (valuesWithWeight: GenSeq[(A, B)])
  34. (implicit combine: NumericCombine.Aux[A, B, AB]):
  35. (A, AB) = {
  36.     val z: (A, AB)= (combine.numericA.zero, combine.numericAB.zero)
  37.  
  38.     valuesWithWeight.aggregate(z)(
  39.         accumulateWeightedValue[A,B,AB],
  40.         dataOps.tuple2.plus[A,AB](_,_)(combine.numericA,combine.numericAB) //explicit numeric param: bad
  41.     )
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement