aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/iterators.pk
blob: 224460f03c3aee3923019d551e2f3012a7846e2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
## std.iterators: The Iter interface and associated functions.
## This module is imported by default.

## The Iter interface. Any type implementing `next()` is iterable.
pub type Iter[T] = interface
  next(mut Self): Option[T]

# todo: useful functions for an iterator
# https://doc.rust-lang.org/std/iter/trait.Iterator.html#provided-methods

pub func advance_by[T](self: Iter[T], n: uint): Result[T, ...] =
  for i in 0 .. n:
    if self.next().is_none():
      return Error(...)
  Okay

pub func get[T](self: Iter[T], at: uint): Option[T]
  self.advance_by(at-1).ok?
  self.next()

# todo: implement iter[T](self: ...): Iter[T] funcs
# todo: efficient functional methods

## The Peek interface. Any type implementing `Iter`, `peek`, and `peek_nth` is peekable.
pub type Peek[T] = interface
  next(mut Self): Option[T]
  peek(mut Self): Option[T]
  peek_nth(mut Self, int): Option[T]

# todo: implement peek[T](self: Iter[T]): Peek[T]
# todo: implement Peekable struct
# https://github.com/LukeMathWalker/multipeek/blob/main/src/lib.rs

## We don't want a Countable. It's not terribly useful.
# pub type Countable[T] = interface
#   next(mut Self): Option[T]
#   len(Self): uint
#   get(Self, uint): Option[T]