https://github.com/johnpaulada/maybe
A simple, lightweight, Maybe implementation in JavaScript.
https://github.com/johnpaulada/maybe
functional-programming maybe monad
Last synced: 9 months ago
JSON representation
A simple, lightweight, Maybe implementation in JavaScript.
- Host: GitHub
- URL: https://github.com/johnpaulada/maybe
- Owner: johnpaulada
- Created: 2017-08-21T07:11:37.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-07T13:51:27.000Z (over 7 years ago)
- Last Synced: 2025-02-13T09:45:36.835Z (9 months ago)
- Topics: functional-programming, maybe, monad
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Maybe
A simple Maybe implementation in JavaScript.
[](http://forthebadge.com)
[](http://forthebadge.com)
[](https://www.jsdelivr.com/package/npm/@johnpaulada/maybe)
## Getting Started
### Importing the library
To use the library, first import it:
In Node:
```javascript
const Maybe = require('@johnpaulada/maybe')
```
As ES Module:
```javascript
import Maybe from '@johnpaulada/maybe'
```
On the browser:
```html
```
### Using the library
To create a Maybe (Just/Nothing) use the `of()` method like this:
```javascript
const value = Maybe.of(1) // Just(1)
```
To work with the value, you can `map()` for example:
```javascript
const plusFive = value.map(x => x + 5) // Just(6)
```
To get the value outside of Maybe, use the `reduce()` method:
```javascript
const sixValue = plusFive.reduce(x => x) // 6
```
or the `value()` method:
```javascript
const sixValue = plusFive.value(0)
```
This will get the value of the Maybe if it's a `Just` and will return a 0 if it's a `Nothing`.
## Development
1. Install [Yarn](https://yarnpkg.com).
2. Install Rollup.
2. Run `yarn` on the terminal to install dependencies.
3. Running `rollup -c` will build the library.
## Roadmap
- [ ] Lazy Evaluation
- [ ] Example for `ap`
- [ ] Example for `branch`
## License
MIT