https://github.com/aminnairi/lemonade
:lemon: Node.js implementation of some monads found in Elm.
https://github.com/aminnairi/lemonade
err just maybe monad nothing ok result task
Last synced: 4 months ago
JSON representation
:lemon: Node.js implementation of some monads found in Elm.
- Host: GitHub
- URL: https://github.com/aminnairi/lemonade
- Owner: aminnairi
- License: mit
- Created: 2020-04-08T17:39:01.000Z (about 5 years ago)
- Default Branch: latest
- Last Pushed: 2023-01-06T03:12:19.000Z (over 2 years ago)
- Last Synced: 2025-01-16T08:37:01.772Z (5 months ago)
- Topics: err, just, maybe, monad, nothing, ok, result, task
- Language: JavaScript
- Homepage:
- Size: 428 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :lemon: lemonade
Node.js implementation of some monads found in Elm.
## :package: Requirements
- [Node.js](https://nodejs.org/en/)
## :arrow_down: Installation
```console
$ npm install aminnairi/lemonade
```## :sparkles: Examples
### :thinking: Maybe
```javascript
const { Maybe: { Nothing, Just } } = require("@aminnairi/lemonade");const divide = (a, b) => b === 0 ? Nothing() : Just(a / b);
const goodDivision = divide(1, 2)
.map(x => x + 1)
.andThen(x => divide(x, 2))
.map(x => x + 1)
.withDefault(0);const badDivision = divide(1, 0)
.map(x => x + 1)
.andThen(x => divide(x, 2))
.map(x => x + 1)
.withDefault(0);const anotherBadDivision = divide(1, 2)
.map(x => x + 1)
.andThen(x => divide(x, 0))
.map(x => x + 1)
.withDefault(0);console.log(goodDivision); // 1.75
console.log(badDivision); // 0
console.log(anotherBadDivision); // 0
```### :ok_hand: Result
```javascript
const { Result: { Ok, Err } } = require("@aminnairi/lemonade");const divide = (a, b) => b === 0 ? Err("division by zero") : Ok(a / b);
divide(1, 2)
.map(x => x + 1)
.andThen(x => divide(x, 2))
.map(x => x + 1)
.when({ Ok: console.log, Err: console.error })
// 1.75divide(1, 2)
.map(x => x + 1)
.andThen(x => divide(x, 0))
.map(x => x + 1)
.when({ Ok: console.log, Err: console.error })
// division by zerodivide(1, 0)
.map(x => x + 1)
.andThen(x => divide(x, 0))
.map(x => x + 1)
.when({ Ok: console.log, Err: console.error })
// division by zerodivide(1, 0)
.map(x => x + 1)
.andThen(x => divide(x, 0))
.map(x => x + 1)
.withDefault(-1);
// -1
```### :construction_worker: Task
```javascript
const { Task: { Task } } = require("@aminnairi/lemonade");Task(() => fetch("https://jsonplaceholder.typicode.com/users/1"))
.map(response => response.json())
.andThen(({ id }) => Task(() => fetch(`https://jsonplaceholder.typicode.com/posts?userId=${id}`)))
.map(response => response.json())
.map(([ post ]) => post.title)
.when({ Ok: console.log, Err: console.error })
// sunt aut facere repellat provident occaecati excepturi optio reprehenderitTask(() => "Hello")
.map(string => string + " world")
.andThen((string) => Task(() => string + "!"))
.when({ Ok: console.log, Err: console.error });
// "Hello world!"Task(() => "Hello")
.map(string => string + " world")
.andThen(string => Task(() => string + "!"))
.perform();
// Promise { }Task(() => 1)
.fork([
task => task.map(x => x + 1).when({Err: console.error, Ok: console.log}), // 2
task => task.map(x => x + 2).when({Err: console.error, Ok: console.log}) // 3
])
.when({Err: console.error, Ok: console.log}); // 1
```### Task X Express
```javascript
"use strict";const {Task: {Task}} = require("@aminnairi/lemonade");
const express = require("express");
const fetch = require("node-fetch");const application = express();
const user = id => Task(() => fetch(`https://jsonplaceholder.typicode.com/users/${id}`));
const postsByUser = id => Task(() => fetch(`https://jsonplaceholder.typicode.com/posts?userId=${id}`));
const responseToJson = response => response.json();application.get("/users/:id/posts", (request, response) => {
user(request.params.id)
.map(responseToJson)
.andThen(({id}) => postsByUser(id))
.map(responseToJson)
.when({
Err: error => response.status(404).send(error),
Ok: posts => response.json(posts)
});
});application.listen(8080, () => console.log("http://localhost:8080"));
// curl localhost:8080/users/1/posts
// [{...}, {...}, ...]
```