Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Max/Min algebra #146

Merged
merged 1 commit into from
Mar 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,39 @@ limitations under the License.
*/
package com.twitter.algebird

import scala.annotation.tailrec

// To use the MaxSemigroup wrap your item in a Max object
case class Max[@specialized(Int,Long,Float,Double) +T](get: T)

object Max {
implicit def semigroup[T](implicit ord:Ordering[T]) =
Semigroup.from[Max[T]] { (l,r) => if(ord.gteq(l.get, r.get)) l else r }

// Zero should have the property that it <= all T
def monoid[T](zero: => T)(implicit ord: Ordering[T]): Monoid[Max[T]] =
Monoid.from(Max(zero)) { (l,r) => if(ord.gteq(l.get, r.get)) l else r }

implicit def intMonoid: Monoid[Max[Int]] = monoid(Int.MinValue)
implicit def longMonoid: Monoid[Max[Long]] = monoid(Long.MinValue)
implicit def doubleMonoid: Monoid[Max[Double]] = monoid(Double.MinValue)
implicit def floatMonoid: Monoid[Max[Float]] = monoid(Float.MinValue)

// These have a lower bound, but not an upperbound, so the Max forms a monoid:
implicit def stringMonoid: Monoid[Max[String]] = monoid("")
implicit def listMonoid[T:Ordering]: Monoid[Max[List[T]]] = monoid[List[T]](Nil)(new Ordering[List[T]] {
@tailrec
final override def compare(left: List[T], right: List[T]): Int = {
(left, right) match {
case (Nil, Nil) => 0
case (Nil, _) => -1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be opposite? List[T] > Nil?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any non zero list is > Nil. -1 means Nil < _

case (_, Nil) => 1
case (lh::lt, rh::rt) =>
val c = Ordering[T].compare(lh, rh)
if(c == 0) compare(lt, rt) else c
}
}
})
}

// To use the MinSemigroup wrap your item in a Min object
Expand All @@ -29,6 +56,15 @@ case class Min[@specialized(Int,Long,Float,Double) +T](get: T)
object Min {
implicit def semigroup[T](implicit ord:Ordering[T]) =
Semigroup.from[Min[T]] { (l,r) => if(ord.lteq(l.get, r.get)) l else r }

// Zero should have the property that it >= all T
def monoid[T](zero: => T)(implicit ord: Ordering[T]): Monoid[Min[T]] =
Monoid.from(Min(zero)) { (l,r) => if(ord.lteq(l.get, r.get)) l else r }

implicit def intMonoid: Monoid[Min[Int]] = monoid(Int.MaxValue)
implicit def longMonoid: Monoid[Min[Long]] = monoid(Long.MaxValue)
implicit def doubleMonoid: Monoid[Min[Double]] = monoid(Double.MaxValue)
implicit def floatMonoid: Monoid[Min[Float]] = monoid(Float.MaxValue)
}

// Not ordered by type, but ordered by order in which we see them:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ object CollectionSpecification extends Properties("Collections") {

property("MinSemigroup is a commutative semigroup") = commutativeSemigroupLaws[Min[Int]]
property("MaxSemigroup is a commutative semigroup") = commutativeSemigroupLaws[Max[Int]]
property("Min[Int] is a monoid") = monoidLaws[Min[Int]]
property("Max[String] is a monoid") = monoidLaws[Max[String]]
property("Max[List[Int]] is a monoid") = monoidLaws[Max[List[Int]]]

property("Either is a Semigroup") = semigroupLaws[Either[String,Int]]
property("Either is a Semigroup, with a Right non-monoid semigroup") = semigroupLaws[Either[String,Max[Int]]]
Expand Down