A simple iterating library for the Zig programming language
Find a file
2025-12-10 16:59:30 +01:00
src refactor iterators/adapters to wrap inner structs in Iterator internally 2025-12-10 16:59:30 +01:00
.gitignore Added .gitignore 2025-10-10 17:23:59 +02:00
build.zig Removed executable module 2025-11-03 12:59:33 +01:00
build.zig.zon Added LICENSE to paths in build.zig.zon 2025-11-03 13:11:24 +01:00
LICENSE Added LICENSE 2025-10-10 17:40:42 +02:00
README.md Added README 2025-12-06 23:24:59 +01:00
zls.json Initial commit 2025-10-09 16:51:39 +02:00

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.