Skip to content

Commit

Permalink
Std: Iter::count, Iter::last, Iter.fold
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 25, 2024
1 parent 41d47c0 commit 2eef6a8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/std/iter/mod.no
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ pub trait Iterable<T> {
pub trait Iter<T> {
fn next(self): Option<T>

fn count(self): Int {
self.fold(|acc, _| { acc + 1 }, 0)
}

fn last(self): Option<T> {
let res = None()
for item in self {
res = Some(item)
}
return res
}

fn fold<U>(self, f: |U, T|: U, initial: U): U {
let res = initial
for item in self {
res = f(res, item)
}
return res
}

fn collect<C: Collector<T>>(self): C {
C::fromIter(self)
}
Expand Down

0 comments on commit 2eef6a8

Please sign in to comment.