https://github.com/katyo/stardust-types
Rust-style monadic types implementation for TypeScript
https://github.com/katyo/stardust-types
async either future monads option result rust stream typescript
Last synced: 21 days ago
JSON representation
Rust-style monadic types implementation for TypeScript
- Host: GitHub
- URL: https://github.com/katyo/stardust-types
- Owner: katyo
- Created: 2017-11-30T09:49:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-20T16:40:40.000Z (over 7 years ago)
- Last Synced: 2025-04-05T02:27:44.889Z (6 months ago)
- Topics: async, either, future, monads, option, result, rust, stream, typescript
- Language: TypeScript
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rust-style monad types for TypeScript [](https://travis-ci.org/katyo/stardust-types)
## Implemented types
### Option\
The option can hold some value or none. This type is preferred to use instead of special values like `null` or `undefined`.
```typescript
import { Option, Some, None } from 'stardust-types/option';function square_odd(opt: Option): Option {
return opt
.and_then(a => a % 2 ? Some([a, a * a]) : None())
.map(([a, s] => `The square of odd number ${a} is ${s}`)
.unwrap_or("Please give me odd numbers");
}console.log(square_odd(None)); // Please give me odd numbers
console.log(square_odd(Some(1))); // The square of odd number 1 is 1
console.log(square_odd(Some(2))); // Please give me odd numbers
console.log(square_odd(Some(3))); // The square of odd number 3 is 9
```See also: [test/option.ts](test/option.ts)
### Result
The result can hold resulting value or error. This type helpful to return from functions or asynchronous operations which may be failed. This type is preferred than throwing errors or using nullable error-result arguments in NodeJS style callbacks or using different callbacks in Promise-style.
```typescript
import { Result, Ok, Err } from 'stardust-types/result';// TODO
```See also: [test/result.ts](test/result.ts)
### Either
The either type is similar to result but preffered for another usecases.
```typescript
import { Either, A, B } from 'stardust-types/either';// TODO
```See also: [test/either.ts](test/either.ts)
### Future
The future type is intended for representing results which cannot be given at same moment when it requested.
```typescript
import { Task, Future, oneshot } from 'stardust-types/future';const [task, future] = oneshot();
let timer;
task.start(() => {
timer = setTimeout(() => {
task.done(123);
// end.fail("Something went wrong");
}, 1000);
}).abort(() => {
clearTimeout(timer);
});future
.map(a => a * a)
.map_err(e => new Error(e))
.end(res => {
res.map_or_else(
err => { throw err; },
val => {
console.log(`The number is: ${val}`);
}
);
})
.start();// prints '123' if not aborted within 1 sec
// setTimeout(() => { future.abort(); }, 500);
```See also: [test/future.ts](test/future.ts)
### Stream _(TODO)_
```typescript
import { Sink, Stream, channel } from 'stardust-types/stream';// TODO
```## Implemented functions
### timeout(msec: number): Future
```typescript
import { timeout } from 'stardust-types/timer';const future = timeout(1000, Ok("Hello"))
.map(phrase => `${phrase}!!!`)
.end(res => { console.log(res.unwrap()); })
.start();
// prints 'Hello!!!' if not aborted within 1 sec
// setTimeout(() => { future.abort(); }, 500);
```See also: [test/timer.ts](test/timer.ts)
### interval(msec: number): Stream _(TODO)_
```typescript
import { interval } from 'stardust-types/timer';interval(1000)
.map(() =>
```### request(Request): Future _(TODO)_
```typescript
import { request } from 'stardust-types/request';// TODO
```