From ca4b29762ac0ed19e1f18474e0df3257f4a74a06 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 23 Oct 2025 17:21:19 -0500 Subject: Added README --- src/example.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/example.zig (limited to 'src/example.zig') diff --git a/src/example.zig b/src/example.zig new file mode 100644 index 0000000..cc3d2c8 --- /dev/null +++ b/src/example.zig @@ -0,0 +1,45 @@ +const std = @import("std"); +const func = @import("funcz"); +const c = func.combinators; + +fn iter(n: i32) i32 { + return n + 1; +} + +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(); + // Currying + const curriedAddResult = func.curry(add)(4)(5); + // Composition + const iterThenMul2 = func.compose(mul2, iter); + const composed = iterThenMul2(5); + // Map + var items = [_]i32{ 0, 1, 2, 3, 4 }; + const itemsSlice: []i32 = items[0..items.len]; + const buffer = try func.mapAlloc(allocator, iterThenMul2, itemsSlice); + + std.debug.print("curry(add)(4)(5) = {any}\n", .{ curriedAddResult }); + std.debug.print("compose(mul2, iter)(5) = {d}\n", .{ composed }); + std.debug.print("mapAlloc(allocator, compose(mul2, iter), []i32{{ 0, 1, 2, 3, 4 }}) = {{ ", .{}); + // func.map(func: anytype, items: anytype, buffer: anytype) + 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)}); +} -- cgit v1.2.3