6.2.1 • Published 8 days ago

@mikuroxina/mini-fn v6.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 days ago

mini-fn

The minimal functional programming library.

codecov


mini-fn provides simple, tiny library having functional features and its type declarations.

Examples

You can pipe your functions with Cat<T>'s feed<U>(f: (t: T) => U): Cat<U> method like this:

import { Cat } from "@mikuroxina/mini-fn";

const result = Cat.cat(-3)
    .feed((x) => x ** 2)
    .feed((x) => x.toString());
console.log(result.value); // "9"

And there are some useful types such as Option<T>, Result<E, T>, and so on.

import { Option } from "@mikuroxina/mini-fn";
const sqrtThenToString = (num: number): Option.Option<string> => {
    if (num < 0) {
        return Option.none();
    }
    return Option.some(Math.sqrt(num).toString());
};

const applied = Option.andThen(sqrtThenToString);
applied(Option.some(4)); // some("2")
applied(Option.some(-1)); // none
applied(Option.none()); // none

Some of them also provides its monad implementation, so you can combine and transform them like this:

import { Cat, Option } from "@mikuroxina/mini-fn";

const half = (x: number): Option.Option<number> => {
    if (x % 2 != 0) {
        return Option.none();
    }
    return Option.some(x / 2);
};
const liftedHalf = Option.monad.flatMap(half);

Cat.cat(20)
    .feed(Option.monad.pure)
    .feed(liftedHalf)
    .feed(Cat.log) // some(10)
    .feed(liftedHalf)
    .feed(Cat.log) // some(5)
    .feed(liftedHalf)
    .feed(Cat.log); // none

Also CatT allows you to compute with a Monad environment as:

import { Cat, List } from "@mikuroxina/mini-fn";

// Find patterns where `x + y + z == 5` for all natural number `x`, `y`, and `z`.
const patterns = Cat.doT(List.monad)
    .addM("x", List.range(0, 6))
    .addMWith("y", ({ x }) => List.range(0, 6 - x))
    .addMWith("z", ({ x, y }) => 5 - (x + y))
    .finish(({ x, y, z }) => [x, y, z] as const);

console.dir(List.toArray(patterns));
/* [
    [0, 0, 5],
    [0, 1, 4],
    [0, 2, 3],
    [0, 3, 2],
    [0, 4, 1],
    [0, 5, 0],
    [1, 0, 4],
    [1, 1, 3],
    [1, 2, 2],
    [1, 3, 1],
    [1, 4, 0],
    [2, 0, 3],
    [2, 1, 2],
    [2, 2, 1],
    [2, 3, 0],
    [3, 0, 2],
    [3, 1, 1],
    [3, 2, 0],
    [4, 0, 1],
    [4, 1, 0],
    [5, 0, 0],
] */
6.2.1

8 days ago

6.2.0

9 days ago

6.1.0

23 days ago

6.1.1

23 days ago

6.0.0

27 days ago

5.7.0

2 months ago

5.6.0

3 months ago

5.5.1

5 months ago

5.5.0

5 months ago

5.4.2

6 months ago

5.4.1

6 months ago

5.3.0

6 months ago

5.2.0

7 months ago

5.1.0

8 months ago

5.0.0

8 months ago

4.1.0

9 months ago

4.0.0

10 months ago

4.1.1

9 months ago

5.4.3

6 months ago

3.1.1

11 months ago

3.0.1

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago

0.3.0

1 year ago

0.2.0

2 years ago

0.1.0

2 years ago