https://github.com/winverse/rs-style-monads-ts
Rust style monads in Typescript
https://github.com/winverse/rs-style-monads-ts
Last synced: about 2 months ago
JSON representation
Rust style monads in Typescript
- Host: GitHub
- URL: https://github.com/winverse/rs-style-monads-ts
- Owner: winverse
- License: mit
- Created: 2022-09-15T03:03:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-15T23:08:47.000Z (over 2 years ago)
- Last Synced: 2025-02-05T11:49:26.530Z (4 months ago)
- Language: TypeScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rs-style-monads-ts
> Rust style monads in Typescript# Option
Sometimes called `Maybe` in other languages. [Maybe](https://hackage.haskell.org/package/base-4.17.0.0/docs/Data-Maybe.html) a =>```typescript
import * as O from './option';const a = O.Some(2);
console.log(O.isSome(a)) // trueconst b = O.Some(5);
console.log(O.map(arr, (a) => a * 2)); // { _tag: 'Some', value: 10 }```
## Methods
- isSome
- isNone
- unwrapOrDefault
- map
- mapOrElse# Result
Sometimes called `Either` in other languages. [Either](https://hackage.haskell.org/package/base-4.17.0.0/docs/Data-Either.html) a b =>```typescript
import * as R from './result'// before
export const main = () => {
try {
const a = "abc"; // enter the some str
const b = f(a);
let c;
try {
c = g(b);
} catch (error) {
c = 3;
}
const d = h(c);
program(d); // true or false
} catch (error) {
handleError(error); // error handling
}
};// after
const main = () => {
const a = "test"; // enter the some str
const b = f(a);
const c = R.flatMap(b, (b_) => R.ok(R.unwrapOrDefault(g(b_), (e) => 3)));
const d = R.flatMap(c, (_c) => h(_c));
const result = R.map(d, (_d) => program(_d)); // true or false
R.unwrapOrDefault(result, (e) => handleError(e)); // error handling
};
```## Methods (exists in rust)
- isOk
- isErr
- unwrapOrDefault
- unwrapOrElse
- map
- mapOrElse## Ts Methods (Not exists in rust)
- keepOk
- flat
- flatMap# Rust Reference
- [Option](https://doc.rust-lang.org/std/option/enum.Option.html#)
- [Result](https://doc.rust-lang.org/std/result/enum.Result.html#)