## std.ranges: Ranges of integers and other things. For iteration. ## This module is imported by default. type Range[T] = struct start: T end: T type RangeIncl[T] = struct start: T end: T done: bool ## Exclusive ranges. Useful for iteration. ## Includes `from`, does not include `to`. pub func ..(from: int, to: int): Range[int] = { from, to } ## Inclusive ranges. Useful for ranges. ## Includes `from` and `to`. pub func ..=(from: int, to: int): RangeIncl[int] = { from, to, done: false } # todo: implement for all types that can increment or smth idk pub func next[T: int](self: mut Range[T]): T? = if self.start < self.end then self.start += 1 Some(self.start - 1) else None # todo: We don't need a mutable Range here to peek. # How does this interact with classes? pub func peek[T: int](self: mut Range[T]): T? = self.peek_nth(0) pub func peek_nth[T: int](self: mut Range[T], i: uint): T? = let res = self.start + i if res < self.end then return Some(res) else return None pub func next[T: int](self: mut RangeIncl[T]): T? = if self.done then None elif self.start < self.end then let res = self.start self.start += 1 Some(res) elif self.start == self.end then self.done = true Some(self.start) else self.done = true None pub func peek[T: int](self: mut RangeIncl[T]): T? = self.peek_nth(0) pub func peek_nth[T: int](self: mut RangeIncl[T], i: uint): T? = let res = self.start + i if res <= self.end then Some(res) else None