Saschagrunert Func Save

Functional additions to C

Project README

func - Functional additions to C Build Status

Func provides - header only - functional additions to the C language. For now, the following features are included:

Installation

Simply add the include folder as a dependency to your project and include the main header file:

#include <Func.h>

Usage

Maybe

A Maybe type is a polymorphic type that represents encapsulation of an optional value.

The type can be used like this:

#include "include/Func.h"

MAYBE(int, foo);

/* Alternatively, if you dont need the 'foo' synonym:
 * MAYBE_TYPE(int); */
 
 /* Maybe will only take struct pointers */
struct bar { int value; };
MAYBE(struct bar *, bar);
/* MAYBE(struct bar, bar); */ /* ERROR */

int main(void) {
    Maybe(foo) maybeFoo = Just_foo(2);

    /* check if the Maybe value contains nothing */
    if (isNothing(maybeFoo)) {
        /* wont be executed */
    }

    /* check if the Maybe value contains something */
    if (isJust(maybeFoo)) {
        /* will be executed */
    }

    /* Extract the content to `t`.
     * Will evaluate to the default value '0'
     * if the maybeFoo would be 'Nothing' */
    int t = fromMaybe(0, maybeFoo);

    /* t will be `2` now */

    return 0;
}

Either

The Either type represents values with two possibilities: a value of type Either a b is either Left a or Right b.

The Either type is sometimes used to represent a value which is either correct or an error; by convention, the Left constructor is used to hold an error value and the Right constructor is used to hold a correct value (mnemonic: "right" also means "correct").

#include "include/Func.h"

EITHER(int, foo, char, bar);

/* Alternatively, if you dont need the 'foo' and 'bar' synonyms:
 * EITHER_TYPE(int, char); */
 
 /* Either will only take struct pointers */
struct baz { int value; };
EITHER(struct baz *, baz, char, bax);
/* EITHER(struct baz, baz, char, bax); */ /* ERROR */

int main(void) {
    Either(foo, bar) eitherFooOrBar = Right_foo_bar('a');

    if (isLeft(eitherFooOrBar)) {
        /* wont be executed */
    }

    if (isRight(eitherFooOrBar)) {
        /* will be executed */
    }

    /* Try to extract the Left content to `x`.
     * Will evaluate to the default '1' if the
     * Either type would be 'Right' */
    int x = fromLeft(1, eitherFooOrBar);

    /* x will be '1' */

    /* Try to extract the Right content to `y`.
     * Will evaluate to the default ' ' if the
     * Either type is 'Left' */
    char y = fromRight(' ', eitherFooOrBar);

    /* y will be 'a' */

    return 0;
}

Contributing

You want to contribute to this project? Wow, thanks! So please just fork it and send me a pull request.

Open Source Agenda is not affiliated with "Saschagrunert Func" Project. README Source: saschagrunert/func
Stars
56
Open Issues
0
Last Commit
6 years ago
Repository
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating