Skip to content

Commit

Permalink
Add BoundedEnumerable.reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymercer committed Feb 14, 2021
1 parent a8d77cf commit 534cee4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ class Tests extends TestsConfig with DisciplineSuite {
checkAll("BoundedEnumerable[Int]", BoundedEnumerableTests[Int].boundedEnumerable)
checkAll("BoundedEnumerable[Char]", BoundedEnumerableTests[Char].boundedEnumerable)
checkAll("BoundedEnumerable[Long]", BoundedEnumerableTests[Long].boundedEnumerable)
checkAll("BoundedEnumerable.reverse(BoundedEnumerable[Int])",
BoundedEnumerableTests(BoundedEnumerable.reverse(BoundedEnumerable[Int])).boundedEnumerable
)
checkAll(
"BoundedEnumerable.reverse(BoundedEnumerable.reverse(BoundedEnumerable[Int]))",
BoundedEnumerableTests(
BoundedEnumerable.reverse(BoundedEnumerable.reverse(BoundedEnumerable[Int]))
).boundedEnumerable
)

checkAll("Monoid[String]", MonoidTests[String].monoid)
checkAll("Monoid[String]", SerializableTests.serializable(Monoid[String]))
Expand Down
15 changes: 15 additions & 0 deletions kernel/src/main/scala/cats/kernel/Enumerable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ object BoundedEnumerable {
cats.kernel.instances.char.catsKernelStdOrderForChar

@inline def apply[A](implicit e: BoundedEnumerable[A]): BoundedEnumerable[A] = e

/**
* Defines a `BoundedEnumerable[A]` from the given enumerable such that
* all arrows / successor functions switch direction.
*/
def reverse[@sp A](e: BoundedEnumerable[A]): BoundedEnumerable[A] =
new BoundedEnumerable[A] {
override def order: Order[A] = Order.reverse(e.order)

override def partialNext(a: A): Option[A] = e.partialPrevious(a)
override def partialPrevious(a: A): Option[A] = e.partialNext(a)

override def minBound: A = e.maxBound
override def maxBound: A = e.minBound
}
}

trait LowerBoundedEnumerable[@sp A] extends PartialNextLowerBounded[A] with Next[A] {
Expand Down

0 comments on commit 534cee4

Please sign in to comment.