mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-14 17:23:15 +01:00
Added README
This commit is contained in:
parent
2c091d9d22
commit
fed67e2571
1 changed files with 62 additions and 0 deletions
62
README.md
Normal file
62
README.md
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Iterating
|
||||
|
||||
A simple iterating library for the Zig programming language
|
||||
|
||||
## Getting started
|
||||
|
||||
To add iterating to your `build.zig.zon`, run
|
||||
|
||||
```bash
|
||||
zig fetch --save=iterating https://codeberg.org/kaasboteram/zig-iterating/archive/main.tar.gz
|
||||
```
|
||||
|
||||
and add
|
||||
|
||||
```zig
|
||||
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
|
||||
|
||||
```zig
|
||||
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](https://github.com/lawrence-laz/zig-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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue