mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-15 09:43:14 +01:00
Added Zip(T) and Iterator(T).zip
This commit is contained in:
parent
cb94fae03b
commit
6122ca7b19
1 changed files with 29 additions and 0 deletions
29
src/root.zig
29
src/root.zig
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue