From ca4b29762ac0ed19e1f18474e0df3257f4a74a06 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 23 Oct 2025 17:21:19 -0500 Subject: Added README --- README.md | 9 +++++++++ build.zig | 4 ++-- build.zig.zon | 2 +- src/example.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.zig | 5 ----- src/main.zig | 39 --------------------------------------- src/root.zig | 6 ++++++ 7 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 README.md create mode 100644 src/example.zig delete mode 100644 src/lib.zig delete mode 100644 src/main.zig create mode 100644 src/root.zig diff --git a/README.md b/README.md new file mode 100644 index 0000000..7452cec --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# funcz: All the functional tools you need for zig + +This is a work in progress library meant for bringing functional ideas to zig +in easy to understand and reasonable ways. + +To add the latest version to your project, simply run +``` +$ zig fetch --save git+https://git.ngaffney.dev/funcz/#HEAD +``` diff --git a/build.zig b/build.zig index 99b3ef9..6de6611 100644 --- a/build.zig +++ b/build.zig @@ -7,7 +7,7 @@ pub fn build(b: *std.Build) !void { const lib = b.addLibrary(.{ .name = "funcz", .root_module = b.createModule(.{ - .root_source_file = b.path("src/lib.zig"), + .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }), @@ -17,7 +17,7 @@ pub fn build(b: *std.Build) !void { const exe = b.addExecutable(.{ .name = "funczExample", .root_module = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), + .root_source_file = b.path("src/example.zig"), .imports = &.{ .{.name = "funcz", .module = lib.root_module} }, .target = target, .optimize = optimize, diff --git a/build.zig.zon b/build.zig.zon index ae01ad9..0ab3d38 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = .funcz, .fingerprint = 0x30f8633228105f1a, - .version = "0.0.1", + .version = "0.1.0", .paths = .{ "src" }, } 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)}); +} diff --git a/src/lib.zig b/src/lib.zig deleted file mode 100644 index 721b2a0..0000000 --- a/src/lib.zig +++ /dev/null @@ -1,5 +0,0 @@ -pub const map = @import("map.zig").map; -pub const mapAlloc = @import("map.zig").mapAlloc; -pub const curry = @import("curry.zig").curry; -pub const compose = @import("compose.zig").compose; -pub const combinators = @import("combinators.zig"); diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index bffa1ae..0000000 --- a/src/main.zig +++ /dev/null @@ -1,39 +0,0 @@ -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(); - const iterThenMul2 = func.compose(mul2, iter); - var items = [_]i32{ 0, 1, 2, 3, 4 }; - const itemsSlice: []i32 = items[0..items.len]; - 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 }}) = {{ ", .{}); - // 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)}); -} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..1339904 --- /dev/null +++ b/src/root.zig @@ -0,0 +1,6 @@ +pub const map = @import("map.zig").map; +pub const mapAlloc = @import("map.zig").mapAlloc; +pub const curry = @import("curry.zig").curry; +pub const compose = @import("compose.zig").compose; +pub const combinators = @import("combinators.zig"); +pub const Functor = @import("functor.zig").Functor; -- cgit v1.2.3