Skip to content

Commit

Permalink
Merge pull request #1 from phenax/enum_examples_section
Browse files Browse the repository at this point in the history
Adds more enum example use-cases
  • Loading branch information
phenax authored Oct 18, 2018
2 parents 1b5df46 + 8ca3d74 commit 6dd4d6c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,65 @@ const CounterComponent = reducerComponent({ state, reducer })(
),
);
```


### Enum use cases

#### In the react world

* Reducer Component ([Docs](https://github.com/phenax/enum-fp/wiki/Reducer-Component-in-React))
```js
import reducerComponent from 'enum-fp/reducerComponent';
```

#### Safely work with empty/invalid states

* Working with invalid values
```js
// Just an example. You should use `Maybe` functor in cases like these
const Value = EnumType({ Invalid: [], Valid: ['value'] });

const getName = user => user && user.name
? Value.Valid(user.name)
: Value.Invalid();

const splitBySpace = Value.caseOf({
Valid: name => name.split(' '),
Invalid: () => [],
});

const [ firstName, lastName ] = compose(splitBySpace, getName, getUser)();
```

#### In the functional world

* Maybe
```js
const Maybe = EnumType({
Just: [ 'value' ],
Nothing: [],
});

const fmap = (fn, a) => Maybe.match(a, {
Just: value => Maybe.Just(fn(value)),
Nothing: () => a,
});
```

* Either
```js
const Either = EnumType({
Left: [ 'error' ],
Right: [ 'value' ],
});

const fmap = (fn, a) => Maybe.match(a, {
Left: () => a,
Right: value => Maybe.Right(fn(value)),
});
const fmapL = (fn, a) => Maybe.match(a, {
Left: value => Maybe.Right(fn(value)),
Right: () => a,
});

```

0 comments on commit 6dd4d6c

Please sign in to comment.