Range

Range represents a range [x, y] that can be generated by using discrete operations Discrete.

Supported Operations

Inverted Range

Note that if x > y and we create the range [x, y] it will be treated as the inverted range [y, x].

Using Range

We can get the values from a range using generate or toList functions. We can also get the string representing the range in math notation. Range(x, y) is represented by [x, y] and Range.empty is represented by [] by using cats.Show

import cats._, cats.implicits._, cats.collections._, cats.collections.syntax.all._, cats.collections.Range._

val range = Range(1, 10)
// range: Range[Int] = Range(1, 10)
range.show
// res0: String = "[1, 10]"

range.toList
// res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

We can get the inverted range using the reverse functions

range.reverse.show
// res2: String = "[10, 1]"

Asking for start and end on a Range

val range = Range(1, 10)
// range: Range[Int] = Range(1, 10)
range.start
// res3: Int = 1
range.end
// res4: Int = 10

Asking for a value within Range

val range = Range(1, 10)
// range: Range[Int] = Range(1, 10)
range.contains(5)
// res5: Boolean = true

Asking for a value that is not within Range

val range = Range(1, 10)
// range: Range[Int] = Range(1, 10)
range.contains(20)
// res6: Boolean = false

Asking whether a Range overlaps another

val range1 = Range(1,10)
// range1: Range[Int] = Range(1, 10)
val range2 = Range(5,15)
// range2: Range[Int] = Range(5, 15)
range1.overlaps(range2)
// res7: Boolean = true

Asking for the difference between two Ranges returns 0, 1, or 2 result ranges

val range = Range(1, 10)
// range: Range[Int] = Range(1, 10)
Range(10, 20).show
// res8: String = "[10, 20]"
(range - Range(5, 9)).show
// res9: String = "Some(([1, 4],Some([10, 10])))"
(range - Range(30, 40)).show
// res10: String = "Some(([1, 10],None))"
(range - Range(15, 18)).show
// res11: String = "Some(([1, 10],None))"
(range - Range (5, 30)).show
// res12: String = "Some(([1, 4],None))"

Creating an inverted range

val range = Range(50, 20)
// range: Range[Int] = Range(50, 20)

range.toList
// res13: List[Int] = List(
//   50,
//   49,
//   48,
//   47,
//   46,
//   45,
//   44,
//   43,
//   42,
//   41,
//   40,
//   39,
//   38,
//   37,
//   36,
//   35,
//   34,
//   33,
//   32,
//   31,
//   30,
//   29,
//   28,
//   27,
//   26,
//   25,
//   24,
//   23,
//   22,
//   21,
//   20
// )

The reverse of a range should be its inverted range

val range = Range(20, 30)
// range: Range[Int] = Range(20, 30)
range.show
// res14: String = "[20, 30]"

val other = Range(30, 20)
// other: Range[Int] = Range(30, 20)
other.show
// res15: String = "[30, 20]"

range.reverse.toList == other.toList
// res16: Boolean = true