mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-15 09:43:14 +01:00
Added example usage for the iter function
This commit is contained in:
parent
27b073b7f8
commit
a5d2efdd10
1 changed files with 30 additions and 0 deletions
30
src/root.zig
30
src/root.zig
|
|
@ -22,6 +22,36 @@ pub fn iter(comptime T: type) type {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test iter {
|
||||||
|
const testing = @import("std").testing;
|
||||||
|
|
||||||
|
const gpa = testing.allocator;
|
||||||
|
|
||||||
|
const arith = struct {
|
||||||
|
fn is_square(x: i32) bool {
|
||||||
|
const sqrt = @sqrt(@as(f32, @floatFromInt(x)));
|
||||||
|
return sqrt == @floor(sqrt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const items = try iter(i32)
|
||||||
|
.once(-100)
|
||||||
|
.chain(iter(i32)
|
||||||
|
.rangeInclusive(1, 100)
|
||||||
|
.filter(arith.is_square)
|
||||||
|
.chain(iter(i32)
|
||||||
|
.range(0, 10)))
|
||||||
|
.toOwnedSlice(gpa);
|
||||||
|
|
||||||
|
defer gpa.free(items);
|
||||||
|
|
||||||
|
try testing.expectEqualSlices(
|
||||||
|
i32,
|
||||||
|
&[_]i32{ -100, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
|
||||||
|
items,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn Iterator(comptime Inner: type) type {
|
pub fn Iterator(comptime Inner: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
inner: Inner,
|
inner: Inner,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue