Skip to content

Commit

Permalink
Merge pull request #3793 from ashleymercer/master
Browse files Browse the repository at this point in the history
Add BoundedEnumerable.reverse
  • Loading branch information
larsrh authored Feb 16, 2021
2 parents e4542c9 + 534cee4 commit d1f6e53
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 @@ -210,6 +210,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 d1f6e53

Please sign in to comment.