diff options
Diffstat (limited to 'std/prelude/ranges.pk')
-rw-r--r-- | std/prelude/ranges.pk | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/std/prelude/ranges.pk b/std/prelude/ranges.pk index e3af8a4..8410c5e 100644 --- a/std/prelude/ranges.pk +++ b/std/prelude/ranges.pk @@ -5,7 +5,7 @@ type Range[T] = struct start: T end: T -type RangeInclusive[T] = struct +type RangeIncl[T] = struct start: T end: T done: bool @@ -17,49 +17,48 @@ pub func ..(from: int, to: int): Range[int] = ## Inclusive ranges. Useful for ranges. ## Includes `from` and `to`. -pub func ..=(from: int, to: int): RangeInclusive[int] = +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: - result = Some(self.start) + if self.start < self.end then self.start += 1 - else: - result = None + Some(self.start - 1) + else + None # todo: We don't need a mutable Range here to peek. -# How does this interact with interfaces? +# 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: + if res < self.end then return Some(res) - else: + else return None -pub func next[T: int](self: mut RangeInclusive[T]): T? = - if self.done: - return None - elif self.start < self.end: +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 - return Some(res) - elif self.start == self.end: + Some(res) + elif self.start == self.end then self.done = true - return Some(self.start) - else: + Some(self.start) + else self.done = true - return None + None -pub func peek[T: int](self: mut RangeInclusive[T]): T? = +pub func peek[T: int](self: mut RangeIncl[T]): T? = self.peek_nth(0) -pub func peek_nth[T: int](self: mut RangeInclusive[T], i: uint): T? = +pub func peek_nth[T: int](self: mut RangeIncl[T], i: uint): T? = let res = self.start + i - if res <= self.end: - return Some(res) - else: - return None + if res <= self.end + then Some(res) + else None |