Giter Club home page Giter Club logo

func's Introduction

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.

func's People

Contributors

ailijic avatar saschagrunert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.