-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Enumerable.scala
128 lines (109 loc) · 4.57 KB
/
Enumerable.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cats
package kernel
import scala.{specialized => sp}
/**
* A typeclass with an operation which returns a member which is
* greater or `None` than the one supplied.
*/
trait PartialNext[@sp A] {
def partialOrder: PartialOrder[A]
def partialNext(a: A): Option[A]
}
/**
* A typeclass with an operation which returns a member which is
* always greater than the one supplied.
*/
trait Next[@sp A] extends PartialNext[A] {
def next(a: A): A
override def partialNext(a: A): Option[A] = Some(next(a))
}
/**
* A typeclass with an operation which returns a member which is
* smaller or `None` than the one supplied.
*/
trait PartialPrevious[@sp A] {
def partialOrder: PartialOrder[A]
def partialPrevious(a: A): Option[A]
}
/**
* A typeclass with an operation which returns a member which is
* always smaller than the one supplied.
*/
trait Previous[@sp A] extends PartialPrevious[A] {
def partialOrder: PartialOrder[A]
def previous(a: A): A
override def partialPrevious(a: A): Option[A] = Some(previous(a))
}
/**
* A typeclass which has both `previous` and `next` operations
* such that `next . previous == identity`.
*/
trait UnboundedEnumerable[@sp A] extends Next[A] with Previous[A] {
def order: Order[A]
override def partialOrder: PartialOrder[A] = order
}
trait BoundedEnumerable[@sp A] extends PartialPreviousUpperBounded[A] with PartialNextLowerBounded[A] {
def order: Order[A]
override def partialOrder: PartialOrder[A] = order
def cycleNext(a: A): A =
partialNext(a).getOrElse(minBound)
def cyclePrevious(a: A): A =
partialPrevious(a).getOrElse(maxBound)
}
object BoundedEnumerable {
implicit def catsKernelBoundedEnumerableForUnit: BoundedEnumerable[Unit] =
cats.kernel.instances.unit.catsKernelStdOrderForUnit
implicit def catsKernelBoundedEnumerableForBoolean: BoundedEnumerable[Boolean] =
cats.kernel.instances.boolean.catsKernelStdOrderForBoolean
implicit def catsKernelBoundedEnumerableForByte: BoundedEnumerable[Byte] =
cats.kernel.instances.byte.catsKernelStdOrderForByte
implicit def catsKernelBoundedEnumerableForInt: BoundedEnumerable[Int] =
cats.kernel.instances.int.catsKernelStdOrderForInt
implicit def catsKernelBoundedEnumerableForShort: BoundedEnumerable[Short] =
cats.kernel.instances.short.catsKernelStdOrderForShort
implicit def catsKernelBoundedEnumerableForLong: BoundedEnumerable[Long] =
cats.kernel.instances.long.catsKernelStdOrderForLong
implicit def catsKernelBoundedEnumerableForChar: BoundedEnumerable[Char] =
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] {
def order: Order[A]
override def partialOrder: PartialOrder[A] = order
}
trait UpperBoundedEnumerable[@sp A] extends PartialPreviousUpperBounded[A] with Previous[A] {
def order: Order[A]
override def partialOrder: PartialOrder[A] = order
}