-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ryan-williams/ord
add some orderings
- Loading branch information
Showing
10 changed files
with
123 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
|
||
name := "iterator" | ||
|
||
version := "1.1.0" | ||
version := "1.1.1" | ||
|
||
libraryDependencies ++= Seq( | ||
libraries.value('kryo), | ||
libraries.value('spire), | ||
"org.apache.commons" % "commons-math3" % "3.6.1" | ||
deps ++= Seq( | ||
libs.value('commons_math), | ||
libs.value('kryo), | ||
libs.value('spire) | ||
) | ||
|
||
testDeps += libraries.value('test_utils) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
addSbtPlugin("org.hammerlab" % "sbt-parent" % "1.2.9") | ||
addSbtPlugin("org.hammerlab" % "sbt-parent" % "1.5.2") |
18 changes: 18 additions & 0 deletions
18
src/main/scala/org/hammerlab/iterator/IteratorOrdering.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.hammerlab.iterator | ||
|
||
object IteratorOrdering { | ||
implicit def apply[T](implicit ord: Ordering[T]): Ordering[Iterator[T]] = | ||
new Ordering[Iterator[T]] { | ||
override def compare(x: Iterator[T], y: Iterator[T]): Int = | ||
(x.hasNext, y.hasNext) match { | ||
case (false, false) ⇒ 0 | ||
case (true, false) ⇒ 1 | ||
case (false, true) ⇒ -1 | ||
case _ ⇒ | ||
ord.compare(x.next, y.next) match { | ||
case 0 ⇒ compare(x, y) | ||
case x ⇒ x | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.hammerlab.math | ||
|
||
/** | ||
* Fork of [[scala.math.PartiallyOrdered]] trait that inherits [[Any]], making it a universal trait suitable for | ||
* inheritance by value-classes. See https://issues.scala-lang.org/browse/SI-10128. | ||
* | ||
* A class for partially ordered data. | ||
* | ||
* Forked from the Scala standard-lib in order to make it a universal trait, for mixing-in to value-classes. | ||
* | ||
* @author Martin Odersky | ||
* @version 1.0, 23/04/2004 | ||
*/ | ||
trait PartiallyOrdered[+A] extends Any { | ||
|
||
/** Result of comparing `'''this'''` with operand `that`. | ||
* Returns `None` if operands are not comparable. | ||
* If operands are comparable, returns `Some(x)` where | ||
* - `x < 0` iff `'''this''' < that` | ||
* - `x == 0` iff `'''this''' == that` | ||
* - `x > 0` iff `'''this''' > that` | ||
*/ | ||
def tryCompareTo [B >: A : PartiallyOrdered](that: B): Option[Int] | ||
|
||
def < [B >: A : PartiallyOrdered](that: B): Boolean = | ||
this tryCompareTo that match { | ||
case Some(x) if x < 0 => true | ||
case _ => false | ||
} | ||
def > [B >: A : PartiallyOrdered](that: B): Boolean = | ||
this tryCompareTo that match { | ||
case Some(x) if x > 0 => true | ||
case _ => false | ||
} | ||
def <= [B >: A : PartiallyOrdered](that: B): Boolean = | ||
this tryCompareTo that match { | ||
case Some(x) if x <= 0 => true | ||
case _ => false | ||
} | ||
def >= [B >: A : PartiallyOrdered](that: B): Boolean = | ||
this tryCompareTo that match { | ||
case Some(x) if x >= 0 => true | ||
case _ => false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.hammerlab.seq | ||
|
||
import org.hammerlab.iterator.IteratorOrdering | ||
|
||
object SeqOrdering { | ||
implicit def apply[T](implicit ord: Ordering[T]): Ordering[Seq[T]] = | ||
new Ordering[Seq[T]] { | ||
override def compare(x: Seq[T], y: Seq[T]): Int = | ||
IteratorOrdering(ord).compare(x.iterator, y.iterator) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/scala/org/hammerlab/iterator/IteratorOrderingTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.hammerlab.iterator | ||
|
||
import org.hammerlab.test.Suite | ||
|
||
class IteratorOrderingTest extends Suite { | ||
test("nums") { | ||
val ord = IteratorOrdering[Int] | ||
ord.compare(Iterator(), Iterator()) should be(0) | ||
ord.compare(Iterator(1), Iterator()) should be(1) | ||
ord.compare(Iterator(), Iterator(1)) should be(-1) | ||
ord.compare(Iterator(1), Iterator(1)) should be(0) | ||
ord.compare(Iterator(1, 2), Iterator(1)) should be(1) | ||
ord.compare(Iterator(1), Iterator(1, 2)) should be(-1) | ||
ord.compare(Iterator(1, 2), Iterator(1, 2)) should be(0) | ||
ord.compare(Iterator(1, 1), Iterator(1, 2)) should be(-1) | ||
ord.compare(Iterator(1, 2), Iterator(1, 1)) should be(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.hammerlab.math | ||
|
||
import org.hammerlab.math.Steps._ | ||
import org.hammerlab.test.Suite | ||
import org.hammerlab.test.matchers.seqs.SeqMatcher.seqMatch | ||
|
||
class StepsTest extends Suite { | ||
test("roundNumbers") { | ||
roundNumbers(200).toVector.sorted should seqMatch( | ||
(0 until 20) ++ (20 until 50 by 2) ++ (50 until 100 by 5) ++ (100 to 200 by 10) | ||
) | ||
} | ||
|
||
test("geometricEvenSteps") { | ||
geometricEvenSteps(1000, 20).toVector.sorted should ===( | ||
Vector( | ||
0, 1, 2, 3, 4, 5, 6, 9, 14, 21, 31, 46, 68, 99, 146, 215, 316, 464, 681, 999 | ||
) | ||
) | ||
} | ||
} |