https://github.com/saschagrunert/func
Functional additions to C
https://github.com/saschagrunert/func
c cpp either functional-programming maybe
Last synced: 7 months ago
JSON representation
Functional additions to C
- Host: GitHub
- URL: https://github.com/saschagrunert/func
- Owner: saschagrunert
- License: mit
- Created: 2018-03-03T13:45:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-15T20:11:27.000Z (over 7 years ago)
- Last Synced: 2025-03-17T18:48:35.574Z (7 months ago)
- Topics: c, cpp, either, functional-programming, maybe
- Language: C++
- Size: 24.4 KB
- Stars: 58
- Watchers: 14
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# func - Functional additions to C [](https://travis-ci.org/saschagrunert/func)
Func provides - header only - functional additions to the C language. For now,
the following features are included:- [`Maybe` data type](./include/Maybe.h)
- [`Either` data type](./include/Either.h)## Installation
Simply add the [`include`](./include) folder as a dependency to your project and
include the main header file:```c
#include
```## Usage
### Maybe
A Maybe type is a polymorphic type that represents encapsulation of an optional
value.The type can be used like this:
```c
#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").```c
#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.