diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2025-10-21 00:09:07 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2025-10-21 00:09:07 -0500 |
| commit | c034b4a1291a803028eef8b0950a7090ea54b047 (patch) | |
| tree | 987040745c887299e69f2c3014c44ea829d89bb3 /src/main.zig | |
| parent | b4588b2064c3afde9497084caef6e83246b32501 (diff) | |
| download | funcz-c034b4a1291a803028eef8b0950a7090ea54b047.tar.gz | |
Currying, Combinators, and organization
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/main.zig b/src/main.zig index 949c368..bffa1ae 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); const func = @import("funcz"); +const c = func.combinators; fn iter(n: i32) i32 { return n + 1; @@ -9,25 +10,30 @@ fn mul2(n: i32) i32 { return n * 2; } +fn add(n: i32, m: i32) i32 { + return n + m; +} + +fn addThenMultiply(n: i32, m: i32, q: i32) i32 { + return (n + m) * q; +} + pub fn main() !void { var gpa = std.heap.DebugAllocator(.{}){}; const allocator = gpa.allocator(); const iterThenMul2 = func.compose(mul2, iter); var items = [_]i32{ 0, 1, 2, 3, 4 }; const itemsSlice: []i32 = items[0..items.len]; - const newItems = try func.mapAlloc(allocator, iterThenMul2, itemsSlice); - defer allocator.free(newItems); - var buffer: [128]i32 = undefined; - func.map(iterThenMul2, itemsSlice, &buffer); + std.debug.print("curry(add)(4)(5) = {any}\n", .{ func.curry(add)(4)(5) }); std.debug.print("compose(mul2, iter)(5) = {d}\n", .{ iterThenMul2(5) }); std.debug.print("mapAlloc(allocator, compose(mul2, iter), []i32{{ 0, 1, 2, 3, 4 }}) = {{ ", .{}); - for(newItems) |item| { - std.debug.print("{d}, ", .{item}); - } - std.debug.print("}}\n", .{}); - std.debug.print("map(compose(mul2, iter), []i32{{ 0, 1, 2, 3, 4 }}, &buffer) = {{ ", .{}); - for(buffer[0..items.len]) |item| { + // func.map(func: anytype, items: anytype, buffer: anytype) + const buffer = try func.mapAlloc(allocator, func.curry(addThenMultiply)(1)(2), itemsSlice); + for(buffer) |item| { std.debug.print("{d}, ", .{item}); } std.debug.print("}}\n", .{}); + std.debug.print("I(5) = {any}\n", .{c.I(5)}); + std.debug.print("K(5)(7) = {any}\n", .{c.K(5)(7)}); + std.debug.print("(S K S K)(5)(7) = {any}\n", .{((c.S(c.K)(c.S)(c.K))(mul2)(func.curry(add)(3)))(7)}); } |
