aboutsummaryrefslogtreecommitdiff
path: root/src/example.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2025-10-28 20:02:47 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2025-10-28 20:02:47 -0500
commit99c32737fe07bf7dc6094a1326be418ffd00e36f (patch)
treebc8549038f85586ef4b9724f51ab850193f36d68 /src/example.zig
parent96e8c19ebdc7c168a1bd243ce1793b2df1480939 (diff)
downloadfuncz-99c32737fe07bf7dc6094a1326be418ffd00e36f.tar.gz
filter implemented
Diffstat (limited to 'src/example.zig')
-rw-r--r--src/example.zig13
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});