aboutsummaryrefslogtreecommitdiff
path: root/src/filter.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/filter.zig
parent96e8c19ebdc7c168a1bd243ce1793b2df1480939 (diff)
downloadfuncz-99c32737fe07bf7dc6094a1326be418ffd00e36f.tar.gz
filter implemented
Diffstat (limited to 'src/filter.zig')
-rw-r--r--src/filter.zig22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/filter.zig b/src/filter.zig
new file mode 100644
index 0000000..b9e2167
--- /dev/null
+++ b/src/filter.zig
@@ -0,0 +1,22 @@
+const std = @import("std");
+const typeVerify = @import("util.zig").typeVerify;
+
+/// ```zig
+/// (fn (fn (fn (a) bool, []a) []a)
+/// ```
+/// Filters a slice, only keeping items give `true` when passed into `func`
+/// Type signature: `(a -> bool) -> [a] -> [a]`
+/// `func` is of type `a -> bool`, where `items` is of type `[a]`
+pub fn filter(
+ func: anytype,
+ items: []typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.?,
+) []typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.? {
+ var output = items;
+ var i: u32 = 0;
+ for (items) |it|
+ if (func(it)) {
+ output[i] = it;
+ i = i + 1;
+ };
+ return output[0..i];
+}