diff options
Diffstat (limited to 'src/example.zig')
| -rw-r--r-- | src/example.zig | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/example.zig b/src/example.zig index 6622726..e20eb31 100644 --- a/src/example.zig +++ b/src/example.zig @@ -18,6 +18,10 @@ fn addThenMultiply(n: i32, m: i32, q: i32) i32 { return (n + m) * q; } +fn isEven(n: i32) bool { + return (@mod(n, 2) == 0); +} + pub fn main() !void { var gpa = std.heap.DebugAllocator(.{}){}; defer _=gpa.deinit(); @@ -28,13 +32,14 @@ pub fn main() !void { const iterThenMul2 = func.compose(mul2, iter); const composed = iterThenMul2(5); // Map - var items = [_]i32{ 0, 1, 2, 3, 4 }; + var items = [_]i32{ 0, 1, 2, 3, 4, 5 }; const itemsSlice: []i32 = items[0..items.len]; var buffer: [16]i32 = undefined; var buffSlice: []i32 = buffer[0..16]; func.map(iterThenMul2, itemsSlice, &buffSlice); const bufferAlloc = try func.mapAlloc(allocator, iterThenMul2, itemsSlice); defer allocator.free(bufferAlloc); + const filtered = func.filter(isEven, itemsSlice); std.debug.print("curry(add)(4)(5) = {any}\n", .{ curriedAddResult }); std.debug.print("compose(mul2, iter)(5) = {d}\n", .{ composed }); @@ -45,6 +50,12 @@ pub fn main() !void { } std.debug.print("}}\n", .{}); + std.debug.print("filter(isEven, bufferAlloc) = {{ ", .{}); + for(filtered) |item| { + std.debug.print("{d}, ", .{item}); + } + std.debug.print("}}\n", .{}); + std.debug.print("mapAlloc(allocator, compose(mul2, iter), []i32{{ 0, 1, 2, 3, 4 }}) = {{ ", .{}); for(bufferAlloc) |item| { std.debug.print("{d}, ", .{item}); |
