1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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 filterAlloc(
allocator: std.mem.Allocator,
func: anytype,
items: []typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.?,
) ![]typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.? {
var tmp = items;
var i: u32 = 0;
for (items) |it|
if (func(it)) {
tmp[i] = it;
i = i + 1;
};
const output = try allocator.alloc(typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.?, i);
for (0..i) |j|
output[j] = tmp[i];
return output;
}
pub fn filterBuff(
func: anytype,
items: []typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.?,
buffer: *[]typeVerify(@TypeOf(func), .{ .@"fn" }).@"fn".params[0].type.?,
) void {
var tmp = items;
var i: u32 = 0;
for (items) |it|
if (func(it)) {
tmp[i] = it;
i = i + 1;
};
for (0..i) |j|
buffer[j] = tmp[i];
}
|