Ranges
Working with ranges and sequences
Ranges use the x..y syntax and are a form of sequence. Bounded ranges are finite (seq), while unbounded ranges are potentially infinite (gen).
for 1..5 | i -> println("Hello times {i}")
def naturalNumbers = 0..
def oneToTen = 1..10
Types and ranges
Any type can be used in a range if it supports succ and comparison:
def aToZ = 'a'..'z'
type Point = (x: int, y: int)
def Point.succ(p: Point) -> Point(p.x + 1, p.y + 1)
def Point.`<=`(p1: Point, p2: Point) -> magnitude(p1) <= magnitude(p2)
def points = Point(1, 1)..Point(8, 8)
Implementing ranges
You can implement your own range generator when you have succ and a comparison:
def range
: [(succ: a -> a, `<=`: (a, a) -> bool)] (start: a, end: a) -> seq(a)
= { start, end ->
seq {
memory {
def current = mut start
while current <= end ->
yield current
current := succ(current)
}
}
}