Skip to content

Commit

Permalink
Add unit test for composed Functor|Applicative
Browse files Browse the repository at this point in the history
  • Loading branch information
flyfish30 committed Sep 14, 2024
1 parent fac933c commit 301a6a1
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 49 deletions.
52 changes: 20 additions & 32 deletions sample/zcats_sample.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ fn arraylistSample() !void {
std.debug.print("arr_binded: {any}\n", .{arr_binded.items});
}

// Deinit the array3 with type ArrayList(Maybe(ArrayList(A))
fn array3Deinit(array3: anytype) void {
for (array3.items) |item| {
if (item) |o| {
o.deinit();
}
}
array3.deinit();
}

fn composeSample() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
Expand Down Expand Up @@ -215,7 +225,7 @@ fn composeSample() !void {
std.debug.print("arr_applied: {any}\n", .{arr_applied.items});

// pretty print the arr3 with type ArrayList(Maybe(ArrayList(A))
const prettyPrintArr3 = struct {
const array3PrettyPrint = struct {
fn prettyPrint(arr3: anytype) void {
std.debug.print("{{ \n", .{});
var j: u32 = 0;
Expand Down Expand Up @@ -270,15 +280,8 @@ fn composeSample() !void {
}
}{ .allocator = allocator, .fns = fn_int_array[0..2] };

var arr3_fns = try array_maybe.fmapLam(.NewValMap, intToFns, arr);
defer {
for (arr3_fns.items) |item| {
if (item) |o| {
o.deinit();
}
}
arr3_fns.deinit();
}
const arr3_fns = try array_maybe.fmapLam(.NewValMap, intToFns, arr);
defer array3Deinit(arr3_fns);

const intToArr = struct {
allocator: Allocator,
Expand All @@ -298,36 +301,21 @@ fn composeSample() !void {
}
}{ .allocator = allocator };

var arr3_ints = try array_maybe.fmapLam(.NewValMap, intToArr, arr_applied);
defer {
for (arr3_ints.items) |item| {
if (item) |o| {
o.deinit();
}
}
arr3_ints.deinit();
}
// std.debug.print("arr3_ints: {any}\n", .{arr3_ints.items});
const arr3_ints = try array_maybe.fmapLam(.NewValMap, intToArr, arr_applied);
defer array3Deinit(arr3_ints);
// std.debug.print("arr3_ints: ", .{});
// array3PrettyPrint(arr3_ints);

const ArrayMaybeArrayApplicative = ComposeApplicative(ArrayListMaybeApplicative, ArrayListApplicative);
var array_maybe_array = ArrayMaybeArrayApplicative.init(.{ .functor_sup = .{
.instanceF = array_maybe,
.instanceG = ArrayListApplicative.init(.{
.allocator = allocator,
}),
.instanceG = ArrayListApplicative.init(.{ .allocator = allocator }),
} });

const arr3_appried = try array_maybe_array.fapply(u32, u32, arr3_fns, arr3_ints);
defer {
for (arr3_appried.items) |item| {
if (item) |o| {
o.deinit();
}
}
arr3_appried.deinit();
}
defer array3Deinit(arr3_appried);
std.debug.print("arr3_appried: ", .{});
prettyPrintArr3(arr3_appried);
array3PrettyPrint(arr3_appried);
}

fn productSample() !void {
Expand Down
7 changes: 6 additions & 1 deletion src/applicative.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub fn ApplicativeFxTypes(comptime F: TCtor, comptime E: type) type {
return struct {
/// return type of pure a
pub fn APaType(comptime A: type) type {
return E!F(A);
const has_err, const _A = comptime isErrorUnionOrVal(A);
if (has_err) {
return (E || @typeInfo(A).ErrorUnion.error_set)!F(_A);
} else {
return E!F(A);
}
}

/// return type of fapply
Expand Down
20 changes: 14 additions & 6 deletions src/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,20 @@ pub fn isErrorUnionOrVal(comptime E: type) struct { bool, type } {

pub fn castInplaceValue(comptime T: type, val: anytype) T {
const info = @typeInfo(@TypeOf(val));
if (info == .Optional) {
const v = val orelse return null;
const retv: std.meta.Child(T) = @bitCast(v);
return retv;
} else {
return @bitCast(val);
switch (info) {
.Optional => {
const v = val orelse return null;
return castInplaceValue(std.meta.Child(T), v);
},
.Struct => {
if (info.Struct.layout == .auto) {
@compileError("Can't inplace cast struct with auto layout");
}
return @bitCast(val);
},
else => {
return @bitCast(val);
},
}
}

Expand Down
Loading

0 comments on commit 301a6a1

Please sign in to comment.