8000 GitHub - ali-shahwali/zig-functools: Library with useful functional programming tools for Zig.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ali-shahwali/zig-functools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig Functools

A Zig library that provides functional programming tools such as map, reduce and filter.

Add it to your project

Add the .functools dependency to your build.zig.zon.

$ zig fetch --save git+https://github.com/ali-shahwali/zig-functools

Add this to your build.zig inside the build function.

const functools = b.dependency("functools", .{
        .target = target,
        .optimize = optimize,
    });
exe.root_module.addImport("functools", functools.module("functools"));
// Or, if you are building a library
// lib.root_module.addImport("functools", functools.module("functools"));

The library can now be imported as a module.

const functools = @import("functools");

Examples

The documentation contains some examples. The tests are also good examples of how to use the library, below are some simple examples from the tests.

Map over slice and increment each element.

test "test map mutable slice on i32 slice without args" {
    const allocator = testing.allocator;

    var slice = try range.rangeSlice(allocator, i32, 3);
    defer allocator.free(slice);

    mapSlice(CommonMappers.inc(i32), slice, .{});

    try testing.expectEqualSlices(i32, slice, &[_]i32{ 1, 2, 3 });
}

Filter even integers.

test "test filter on i32 slice" {
    const slice = &[_]i32{ 1, 2, 3, 4, 5 };
    const allocator = testing.allocator;
    const even = try filterSlice(
        allocator,
        CommonPredicates.even(i32),
        slice,
        .{},
    );
    defer allocator.free(even);

    try testing.expectEqualSlices(i32, even, &[_]i32{ 2, 4 });
}

Check that every vector is orthogonal to the x basis vector.

const Point2D = struct {
    x: i32,
    y: i32,
};

fn orthogonal(p1: Point2D, p2: Point2D) bool {
    return (p1.x * p2.x + p1.y * p2.y) == 0;
}

test "test every on Point2D slice" {
    const slice = &[_]Point2D{
        .{ .x = 0, .y = 1 },
        .{ .x = 0, .y = 3 },
        .{ .x = 1, .y = 4 }, // Not orthogonal to (1, 0)
    };
    const e_x = Point2D{ .x = 1, .y = 0 };
    const every_orthogonal = everySlice(orthogonal, slice, .{e_x});

    try testing.expect(!every_orthogonal);
}

About

Library with useful functional programming tools for Zig.

Resources

Stars

Watchers

Forks

Languages

0