mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-14 17:23:15 +01:00
1.5 KiB
1.5 KiB
Iterating
A simple iterating library for the Zig programming language
Getting started
To add iterating to your build.zig.zon, run
zig fetch --save=iterating https://codeberg.org/kaasboteram/zig-iterating/archive/main.tar.gz
and add
const iterating = b.dependency("iterating", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("iterating", iterating.module("iterating"));
to your build.zig in order to make it available in your executable
Examples
test "basic usage" {
const allocator = std.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(allocator);
defer allocator.free(items);
try std.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,
);
}
Thanks
This project took a lot of inspiration from enumerable, which is currently unmaintained. The main differences are that iterating works in Zig 0.15.x and includes a wider range of "utility iterators" (e.g: Empty, Once) by default compared to enumerable.