Added Iterator(T).any and Iterator(T).all

This commit is contained in:
Luuk Machielse 2025-10-10 19:19:47 +02:00
parent 4f57df8f72
commit 3259d0d888

View file

@ -111,6 +111,24 @@ pub fn Iterator(comptime Inner: type) type {
return acc; return acc;
} }
pub fn any(self: Self, f: fn (Item) bool) bool {
var mutSelf = self;
while (mutSelf.next()) |v| {
if (f(v)) return true;
}
return false;
}
pub fn all(self: Self, f: fn (Item) bool) bool {
var mutSelf = self;
while (mutSelf.next()) |v| {
if (!f(v)) return false;
}
return true;
}
pub fn count(self: Self) usize { pub fn count(self: Self) usize {
return self.fold(usize, 0, struct { return self.fold(usize, 0, struct {
fn inc(i: usize, _: Item) usize { fn inc(i: usize, _: Item) usize {