-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreduce.zig
29 lines (25 loc) · 853 Bytes
/
reduce.zig
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
const zignite = @import("../zignite.zig");
const expect = @import("std").testing.expect;
const Fold = @import("fold.zig").Fold;
test "reduce: *" {
const mul = struct {
pub fn mul(x: u32, y: u32) u32 {
return x * y;
}
}.mul;
try expect(zignite.range(u32, 1, 5).reduce(mul).? == 120);
try expect(zignite.empty(u32).reduce(mul) == null);
}
pub fn Reduce(comptime T: type, comptime reducer: fn (accumulator: T, value: T) T) type {
return struct {
const F = Fold(?T, T, r);
fn r(a: ?T, v: T) ?T {
const a_i = .{ .modifier = .always_inline };
return if (a == null) v else @call(a_i, reducer, .{ a.?, v });
}
pub const Type = F.Type;
pub const init = F.init(null);
pub const next = F.next;
pub const deinit = F.deinit;
};
}