Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hejsil/ziter

The missing iterators for Zig
https://github.com/hejsil/ziter

functional iterator iterator-library iterators zig zig-library zig-package

Last synced: 7 days ago
JSON representation

The missing iterators for Zig

Awesome Lists containing this project

README

        

# ziter

An iterator library for zig inspired by the iterators in the Rust standard libary.

```zig
const iter = @import("ziter");
const std = @import("std");

test "ascii" {
const ascii_digits = iter.range(u8, 0, 255)
.filter({}, iter.void_ctx(std.ascii.isDigit));

const ascii_alpha = try iter.range(u8, 0, 255)
.filter({}, iter.void_ctx(std.ascii.isAlphabetic))
.collect_no_allocator(std.BoundedArray(u8, 255){});

try iter.expectEqual(iter.deref("0123456789"), ascii_digits);
try std.testing.expectEqualStrings(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
ascii_alpha.slice(),
);
}

test "Fibonacci" {
const fib = Fibonacci{};

const fib_sum_first_5 = fib.take(5).sum(usize);
const fib_first_alphabetic = fib.map({}, to_u8)
.find({}, iter.void_ctx(std.ascii.isAlphabetic));

try std.testing.expectEqual(@as(usize, 7), fib_sum_first_5);
try std.testing.expectEqual(@as(?u8, 'Y'), fib_first_alphabetic);
}

pub const Fibonacci = struct {
c: usize = 0,
n: usize = 1,

pub fn next(it: *@This()) ?usize {
const curr = it.c;
it.c = it.n;
it.n = curr + it.n;
return curr;
}

pub usingnamespace iter;
};

fn to_u8(_: void, item: usize) u8 {
return @truncate(item);
}

```