Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //want my library functions to use these:
- def times[A](x: A, y: A*)
- (implicit ev: Numeric[A])
- : A = y.fold(x)(ev.times)
- 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 plus[A](x: A, y: A*)
- (implicit ev: Numeric[A])
- : A = y.fold(x)(ev.plus)
- 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 addTuples[A: Numeric,B: Numeric](x: (A, B), y: (A, B)): (A, B)
- = (numericOps.plus(x._1, y._1), numericOps.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 = combine.times(valueWithWeight.weight, valueWithWeight.value) //can already use plusAB(,)
- (
- combine.numericA.plus(accum.weight, valueWithWeight.weight), //ideally: plus(,) can't
- combine.numericAB.plus(accum.value, weightedValue) //ideally: plus(,) can't
- )
- }
- 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],
- //ideally: addTuples[A,AB]
- {
- case ((a1, ab1), (a2, ab2)) =>
- (
- combine.numericA.plus(a1, a2),
- combine.numericAB.plus(ab1, ab2)
- )
- }
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement