Skip to content

Commit

Permalink
use additive to get good algebra compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-stripe committed Jun 6, 2016
1 parent 31b5736 commit 053fd81
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
11 changes: 6 additions & 5 deletions algebird-core/src/main/scala/com/twitter/algebird/Group.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package com.twitter.algebird

import algebra.{ Group => AGroup }
import algebra.ring.AdditiveGroup
import java.lang.{ Integer => JInt, Short => JShort, Long => JLong, Float => JFloat, Double => JDouble, Boolean => JBool }
import java.util.{ List => JList, Map => JMap }

Expand All @@ -29,11 +30,11 @@ import scala.math.Equiv
*/

@implicitNotFound(msg = "Cannot find Group type class for ${T}")
trait Group[@specialized(Int, Long, Float, Double) T] extends AGroup[T] with Monoid[T] {
// must override negate or minus (or both)
def negate(v: T): T = minus(zero, v)
def minus(l: T, r: T): T = plus(l, negate(r))

trait Group[@specialized(Int, Long, Float, Double) T] extends AGroup[T] with Monoid[T] with AdditiveGroup[T] {
/*
* This are from algebra.Group
*/
override def additive: AGroup[T] = this
override def remove(l: T, r: T): T = minus(l, r)
override def inverse(v: T): T = negate(v)
}
Expand Down
15 changes: 9 additions & 6 deletions algebird-core/src/main/scala/com/twitter/algebird/Monoid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package com.twitter.algebird

import algebra.{ Monoid => AMonoid }
import algebra.ring.{ AdditiveMonoid }
import scala.annotation.implicitNotFound
import scala.math.Equiv
import scala.reflect.ClassTag
Expand All @@ -32,8 +33,7 @@ import scala.collection.{ Map => ScMap }
*/

@implicitNotFound(msg = "Cannot find Monoid type class for ${T}")
trait Monoid[@specialized(Int, Long, Float, Double) T] extends Semigroup[T] with AMonoid[T] {
def zero: T //additive identity
trait Monoid[@specialized(Int, Long, Float, Double) T] extends Semigroup[T] with AMonoid[T] with AdditiveMonoid[T] {
def isNonZero(v: T): Boolean = (v != zero)
def assertNotZero(v: T) {
if (!isNonZero(v)) {
Expand All @@ -48,11 +48,14 @@ trait Monoid[@specialized(Int, Long, Float, Double) T] extends Semigroup[T] with
None
}
}
// Override this if there is a more efficient means to implement this
def sum(vs: TraversableOnce[T]): T = sumOption(vs).getOrElse(zero)
override def sum(vs: TraversableOnce[T]): T = sumOption(vs).getOrElse(zero)

final override def empty: T = zero
final override def combineAll(t: TraversableOnce[T]): T = sum(t)
/**
* These are from algebra.Monoid
*/
override def additive: AMonoid[T] = this
override def empty: T = zero
override def combineAll(t: TraversableOnce[T]): T = sum(t)
}

// For Java interop so they get the default methods
Expand Down
5 changes: 3 additions & 2 deletions algebird-core/src/main/scala/com/twitter/algebird/Ring.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package com.twitter.algebird

import java.lang.{ Integer => JInt, Short => JShort, Long => JLong, Float => JFloat, Double => JDouble, Boolean => JBool }
import algebra.ring.{ Ring => ARing }

import scala.annotation.implicitNotFound
/**
Expand All @@ -42,10 +43,10 @@ import scala.annotation.implicitNotFound
*/

@implicitNotFound(msg = "Cannot find Ring type class for ${T}")
trait Ring[@specialized(Int, Long, Float, Double) T] extends Group[T] {
trait Ring[@specialized(Int, Long, Float, Double) T] extends Group[T] with ARing[T] {
def one: T
def times(a: T, b: T): T
def product(iter: TraversableOnce[T]): T =
override def product(iter: TraversableOnce[T]): T =
if (iter.isEmpty) one // avoid hitting one as some have abused Ring for Rng
else iter.reduce(times)
}
Expand Down
14 changes: 10 additions & 4 deletions algebird-core/src/main/scala/com/twitter/algebird/Semigroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package com.twitter.algebird

import algebra.{ Semigroup => ASemigroup }
import algebra.ring.{ AdditiveSemigroup }
import java.lang.{ Integer => JInt, Short => JShort, Long => JLong, Float => JFloat, Double => JDouble, Boolean => JBool }
import java.util.{ List => JList, Map => JMap }

Expand All @@ -30,16 +31,21 @@ import macros.caseclass._
* This is a class with a plus method that is associative: a+(b+c) = (a+b)+c
*/
@implicitNotFound(msg = "Cannot find Semigroup type class for ${T}")
trait Semigroup[@specialized(Int, Long, Float, Double) T] extends ASemigroup[T] {
def plus(l: T, r: T): T
trait Semigroup[@specialized(Int, Long, Float, Double) T] extends ASemigroup[T] with AdditiveSemigroup[T] {
/**
* override this if there is a faster way to do this sum than reduceLeftOption on plus
*/
def sumOption(iter: TraversableOnce[T]): Option[T] =
iter.reduceLeftOption { plus(_, _) }

final override def combine(l: T, r: T): T = plus(l, r)
final override def combineAllOption(iter: TraversableOnce[T]): Option[T] = sumOption(iter)
/*
* These are methods from algebra
*/
override def trySum(iter: TraversableOnce[T]): Option[T] = sumOption(iter)

override def additive: ASemigroup[T] = this
override def combine(l: T, r: T): T = plus(l, r)
override def combineAllOption(iter: TraversableOnce[T]): Option[T] = sumOption(iter)
}

// For Java interop so they get the default sumOption
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ lazy val algebirdCore = module("core").settings(
libraryDependencies <++= (scalaVersion) { scalaVersion =>
Seq("com.googlecode.javaewah" % "JavaEWAH" % javaEwahVersion,
"org.spire-math" %% "algebra" % algebraVersion,
"org.spire-math" %% "algebra-ring" % algebraVersion,
"org.scala-lang" % "scala-reflect" % scalaVersion) ++ {
if (isScala210x(scalaVersion))
Seq("org.scalamacros" %% "quasiquotes" % quasiquotesVersion)
Expand Down

0 comments on commit 053fd81

Please sign in to comment.