-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding FiniteDuration instances (#2544)
* Adding FiniteDuration instances * Adding finiteDuration instances to cats core * not duplicating, just re-using an instance * Adding instances where they need to go * Adding tests to LawTests and a FiniteDurationSuite * Fixing regex error * Fixing bin incompatibility
- Loading branch information
Calvin Brown
authored and
Luka Jacobowitz
committed
Oct 7, 2018
1 parent
4880fd0
commit ee2b5ee
Showing
7 changed files
with
94 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cats | ||
package instances | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
trait FiniteDurationInstances extends cats.kernel.instances.FiniteDurationInstances { | ||
implicit val catsStdShowForFiniteDuration: Show[FiniteDuration] = | ||
Show.fromToString[FiniteDuration] | ||
} |
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
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
32 changes: 32 additions & 0 deletions
32
kernel/src/main/scala/cats/kernel/instances/finiteDuration.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,32 @@ | ||
package cats.kernel | ||
package instances | ||
|
||
import scala.concurrent.duration.{ Duration, FiniteDuration } | ||
|
||
|
||
trait FiniteDurationInstances { | ||
implicit val catsKernelStdOrderForFiniteDuration: Order[FiniteDuration] with Hash[FiniteDuration] = new FiniteDurationOrder | ||
implicit val catsKernelStdGroupForFiniteDuration: CommutativeGroup[FiniteDuration] = new FiniteDurationGroup | ||
} | ||
class FiniteDurationOrder extends Order[FiniteDuration] with Hash[FiniteDuration] { | ||
def hash(x: FiniteDuration): Int = x.hashCode() | ||
|
||
def compare(x: FiniteDuration, y: FiniteDuration): Int = x compare y | ||
|
||
override def eqv(x: FiniteDuration, y: FiniteDuration): Boolean = x == y | ||
override def neqv(x: FiniteDuration, y: FiniteDuration): Boolean = x != y | ||
override def gt(x: FiniteDuration, y: FiniteDuration): Boolean = x > y | ||
override def gteqv(x: FiniteDuration, y: FiniteDuration): Boolean = x >= y | ||
override def lt(x: FiniteDuration, y: FiniteDuration): Boolean = x < y | ||
override def lteqv(x: FiniteDuration, y: FiniteDuration): Boolean = x <= y | ||
|
||
override def min(x: FiniteDuration, y: FiniteDuration): FiniteDuration = x min y | ||
override def max(x: FiniteDuration, y: FiniteDuration): FiniteDuration = x max y | ||
} | ||
|
||
class FiniteDurationGroup extends CommutativeGroup[FiniteDuration] { | ||
def empty: FiniteDuration = Duration.Zero | ||
def inverse(x: FiniteDuration): FiniteDuration = -x | ||
def combine(x: FiniteDuration, y: FiniteDuration): FiniteDuration = x + y | ||
override def remove(x: FiniteDuration, y: FiniteDuration): FiniteDuration = x - y | ||
} |
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,15 @@ | ||
package cats | ||
package tests | ||
|
||
import cats.laws.discipline.SerializableTests | ||
|
||
import scala.concurrent.duration.{ DurationInt, FiniteDuration } | ||
|
||
class FiniteDurationSuite extends CatsSuite { | ||
checkAll("Show[FiniteDuration]", SerializableTests.serializable(Show[FiniteDuration])) | ||
|
||
test("show works for FiniteDuration"){ | ||
Show[FiniteDuration].show(23.minutes) should ===("23 minutes") | ||
Show[FiniteDuration].show(10.seconds) should === ("10 seconds") | ||
} | ||
} |