Added Zip(T) and Iterator(T).zip

This commit is contained in:
Luuk Machielse 2025-10-10 20:08:46 +02:00
parent cb94fae03b
commit 6122ca7b19

View file

@ -153,6 +153,10 @@ pub fn Iterator(comptime Inner: type) type {
return .{ .inner = .{ .first = self.inner, .second = other.inner } };
}
pub fn zip(self: Self, other: anytype) Iterator(adapters.Zip(Inner, Clean(@TypeOf(other)))) {
return .{ .inner = .{ .a = self.inner, .b = other.inner } };
}
pub fn toOwnedSlice(self: Self, gpa: std.mem.Allocator) ![]const Item {
var list: std.ArrayList(Item) = .empty;
errdefer list.deinit(gpa);
@ -287,6 +291,31 @@ pub const adapters = struct {
}
};
}
pub fn Zip(comptime A: type, comptime B: type) type {
return struct {
a: A,
b: B,
pub const Item = struct { A.Item, B.Item };
const Self = @This();
pub fn next(self: *Self) ?Item {
const x = self.a.next() orelse return null;
const y = self.b.next() orelse return null;
return .{ x, y };
}
};
}
test Zip {
const testing = @import("std").testing;
var it = Iter(i32).once(10).zip(Iter(i32).once(20));
try testing.expectEqualDeep(struct { i32, i32 }{ 10, 20 }, it.next().?);
}
};
pub fn Range(comptime T: type) type {