Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/hejsil/ziter
- Owner: Hejsil
- License: mit
- Created: 2020-11-04T18:10:36.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-13T12:42:50.000Z (4 months ago)
- Last Synced: 2025-01-08T04:26:59.744Z (14 days ago)
- Topics: functional, iterator, iterator-library, iterators, zig, zig-library, zig-package
- Language: Zig
- Homepage:
- Size: 127 KB
- Stars: 73
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
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);
}```