Skip to content

Commit

Permalink
Merge pull request #3 from ryan-williams/ord
Browse files Browse the repository at this point in the history
add some orderings
  • Loading branch information
ryan-williams committed Dec 30, 2016
2 parents 430e46b + f93d8dd commit f8d2043
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ jdk:
scala:
- 2.11.8

script: sbt ++$TRAVIS_SCALA_VERSION clean coverage test
script: sbt ++$TRAVIS_SCALA_VERSION clean test

cache:
directories:
- $HOME/.ivy2/cache
- $HOME/.sbt/boot/
- $HOME/.zinc

after_success: sbt ++$TRAVIS_SCALA_VERSION coverageReport coveralls
after_success: sbt ++$TRAVIS_SCALA_VERSION travis-report

before_cache:
# Tricks to avoid unnecessary cache updates
Expand Down
13 changes: 5 additions & 8 deletions build.sbt
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)
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.13
2 changes: 1 addition & 1 deletion project/plugins.sbt
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 src/main/scala/org/hammerlab/iterator/IteratorOrdering.scala
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
}
}
}
}
45 changes: 45 additions & 0 deletions src/main/scala/org/hammerlab/math/PartiallyOrdered.scala
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''' &lt; that`
* - `x == 0` iff `'''this''' == that`
* - `x > 0` iff `'''this''' &gt; 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
}
}
11 changes: 11 additions & 0 deletions src/main/scala/org/hammerlab/seq/SeqOrdering.scala
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)
}
}
3 changes: 1 addition & 2 deletions src/main/scala/org/hammerlab/stats/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ case class Stats[K: Numeric, V: Integral](n: V,
sortedSamplesOpt: Option[Samples[K, V]],
percentiles: Seq[(Double, Double)]) {

def prettyDouble(d: Double): String = {
def prettyDouble(d: Double): String =
if (math.floor(d).toInt == math.ceil(d).toInt)
d.toInt.toString
else
"%.1f".format(d)
}

override def toString: String = {
if (n == 0)
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/org/hammerlab/iterator/IteratorOrderingTest.scala
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)
}
}
21 changes: 21 additions & 0 deletions src/test/scala/org/hammerlab/math/StepsTest.scala
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
)
)
}
}

0 comments on commit f8d2043

Please sign in to comment.