@@ -45,7 +45,7 @@ import cats.kernel.{ScalaVersionSpecificLazyListCompat => LazyListLike}
45
45
trait Enumerable [@ sp A ] extends PartialNext [A ] with PartialPrevious [A ]{
46
46
def order : Order [A ]
47
47
def fromEnum (a : A ): BigInt
48
- def toEnum (i : BigInt ): Option [A ]
48
+ def toEnumOpt (i : BigInt ): Option [A ]
49
49
50
50
/** The fundamental function in the `Enumerable` class. Given a `first`
51
51
* element, a second element, and an optional `last` element, enumerate the
@@ -128,7 +128,7 @@ trait Enumerable[@sp A] extends PartialNext[A] with PartialPrevious[A]{
128
128
}
129
129
130
130
def enumFromByToOpt (first : A , step : BigInt , last : Option [A ]): LazyListLike .T [A ] =
131
- toEnum (step).fold(
131
+ toEnumOpt (step).fold(
132
132
LazyListLike .empty[A ]
133
133
)(second =>
134
134
enumFromThenToOpt(first, second, last)
@@ -204,6 +204,20 @@ trait Enumerable[@sp A] extends PartialNext[A] with PartialPrevious[A]{
204
204
LazyListLike (first)
205
205
}
206
206
207
+ def membersDescending (implicit A : UpperBounded [A ]): LazyListLike .T [A ] =
208
+ partialPrevious(A .maxBound).fold(
209
+ LazyListLike .empty[A ]
210
+ )(previous =>
211
+ enumFromThen(A .maxBound, previous)
212
+ )
213
+
214
+ def membersAscending (implicit A : LowerBounded [A ]): LazyListLike .T [A ] =
215
+ partialNext(A .minBound).fold(
216
+ LazyListLike .empty[A ]
217
+ )(next =>
218
+ enumFromThen(A .minBound, next)
219
+ )
220
+
207
221
override final def partialOrder : PartialOrder [A ] = order
208
222
}
209
223
@@ -238,6 +252,9 @@ trait PartialNext[@sp A] {
238
252
239
253
loop(a, n)
240
254
}
255
+
256
+ def nextOrMin (a : A )(implicit A : LowerBounded [A ]): A =
257
+ partialNext(a).getOrElse(A .minBound)
241
258
}
242
259
243
260
/**
@@ -246,6 +263,10 @@ trait PartialNext[@sp A] {
246
263
*/
247
264
trait Next [@ sp A ] extends PartialNext [A ] {
248
265
def next (a : A ): A
266
+
267
+ override final def nextOrMin (a : A )(implicit A : LowerBounded [A ]): A =
268
+ next(a)
269
+
249
270
override def partialNext (a : A ): Option [A ] = Some (next(a))
250
271
}
251
272
@@ -276,6 +297,9 @@ trait PartialPrevious[@sp A] {
276
297
277
298
loop(a, n)
278
299
}
300
+
301
+ def previousOrMax (a : A )(implicit A : UpperBounded [A ]): A =
302
+ partialPrevious(a).getOrElse(A .maxBound)
279
303
}
280
304
281
305
/**
@@ -285,29 +309,44 @@ trait PartialPrevious[@sp A] {
285
309
trait Previous [@ sp A ] extends PartialPrevious [A ] {
286
310
def partialOrder : PartialOrder [A ]
287
311
def previous (a : A ): A
312
+
313
+ override final def previousOrMax (a : A )(implicit A : UpperBounded [A ]): A =
314
+ previous(a)
315
+
288
316
override def partialPrevious (a : A ): Option [A ] = Some (previous(a))
289
317
}
290
318
291
319
/**
292
320
* A typeclass which has both `previous` and `next` operations
293
321
* such that `next . previous == identity`.
294
322
*/
323
+ // TODO: Not sure what to do about UnboundedEnumerable. It should extend
324
+ // Enumerable, but we can't do that without breaking
325
+ // bincompat. BoundlessEnumerable could extened UnboundedEnumerable, but that
326
+ // seems silly...
295
327
trait UnboundedEnumerable [@ sp A ] extends Next [A ] with Previous [A ] {
296
328
def order : Order [A ]
297
329
override def partialOrder : PartialOrder [A ] = order
298
330
}
299
331
332
+ trait BoundlessEnumerable [@ sp A ] extends Enumerable [A ] with Next [A ] with Previous [A ] {
333
+ def toEnum (i : BigInt ): A
334
+
335
+ override final def toEnumOpt (i : BigInt ): Option [A ] = Some (toEnum(i))
336
+ }
337
+
300
338
trait BoundedEnumerable [@ sp A ] extends PartialPreviousUpperBounded [A ] with PartialNextLowerBounded [A ] {
301
339
302
340
def order : Order [A ]
303
341
override def partialOrder : PartialOrder [A ] = order
304
342
343
+ @ deprecated(message = " Please use nextOrMin instead." , since = " 2.10.0" )
305
344
def cycleNext (a : A ): A =
306
- partialNext (a).getOrElse(minBound )
345
+ nextOrMin (a)( this )
307
346
347
+ @ deprecated(message = " Please use previousOrMax instead." , since = " 2.10.0" )
308
348
def cyclePrevious (a : A ): A =
309
- partialPrevious(a).getOrElse(maxBound)
310
-
349
+ previousOrMax(a)(this )
311
350
}
312
351
313
352
object BoundedEnumerable {
0 commit comments