Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def times[A](x: A, y: A*)
- (implicit ev: Numeric[A])
- : A = y.fold(x)(ev.times)
- def plus[A](x: A, y: A*)
- (implicit ev: Numeric[A])
- : A = y.fold(x)(ev.plus)
- def timesAB[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
- (x:A, y:B)(implicit combine: NumericCombine.Aux[A, B, AB]): AB = {
- combine.times(x,y)
- }
- def plusAB[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
- (x:A, y:B)(implicit combine: NumericCombine.Aux[A, B, AB]): AB = {
- combine.plus(x,y)
- }
- def plusTuple2[A: Numeric,B: Numeric](x: (A, B), y: (A, B)): (A, B)
- = (plus(x._1, y._1), plus(x._2, y._2))
- def accumulateWeightedValue[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
- (accum: (A, AB), valueWithWeight: (A, B))
- (implicit combine: NumericCombine.Aux[A, B, AB]): (A, AB) = {
- val weightedValue = timesAB(valueWithWeight.weight, valueWithWeight.value) // implicit numeric param: good
- (
- plus(accum.weight, valueWithWeight.weight)(combine.numericA), //explicit numeric param: bad
- plus(accum.value, weightedValue)(combine.numericAB) //explicit numeric param: bad
- )
- }
- def weightedSum[A <: AnyVal, B <: AnyVal, AB <: AnyVal]
- (valuesWithWeight: GenSeq[(A, B)])
- (implicit combine: NumericCombine.Aux[A, B, AB]):
- (A, AB) = {
- val z: (A, AB)= (combine.numericA.zero, combine.numericAB.zero)
- valuesWithWeight.aggregate(z)(
- accumulateWeightedValue[A,B,AB],
- dataOps.tuple2.plus[A,AB](_,_)(combine.numericA,combine.numericAB) //explicit numeric param: bad
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement