Skip to content

Improved Max/Min algebra #146

Merged
merged 1 commit into from Mar 17, 2013

2 participants

@johnynek

No description provided.

@sritchie sritchie commented on the diff Mar 17, 2013
...ain/scala/com/twitter/algebird/OrderedSemigroup.scala
+ 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
@sritchie
Twitter, Inc. member
sritchie added a note Mar 17, 2013

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

@johnynek
johnynek added a note Mar 17, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sritchie sritchie merged commit 6fd1351 into develop Mar 17, 2013

1 check passed

Details default The Travis build passed
@sritchie sritchie deleted the feature/better-max-min branch Mar 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.