https://github.com/phadej/trampa
Trampolines, to emulate tail-recursion.
https://github.com/phadej/trampa
Last synced: over 1 year ago
JSON representation
Trampolines, to emulate tail-recursion.
- Host: GitHub
- URL: https://github.com/phadej/trampa
- Owner: phadej
- License: mit
- Created: 2015-07-14T08:04:12.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-10-31T16:02:14.000Z (over 7 years ago)
- Last Synced: 2025-03-12T11:38:30.320Z (over 1 year ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# trampa
Trampolines, to emulate tail-call recursion.
[](http://travis-ci.org/phadej/trampa)
[](http://badge.fury.io/js/trampa)
[](https://david-dm.org/trampa/trampa)
[](https://david-dm.org/trampa/trampa#info=devDependencies)
[](https://codeclimate.com/github/phadej/trampa)
## Synopsis
```js
var trampa = require("trampa");
function loop(n, acc) {
return n === 0 ? trampa.wrap(acc) : trampa.lazy(function () {
return loop(n - 1, acc + 1);
});
}
loop(123456789, 0).run(); // doesn't cause stack overflow!
```
## API
- `isTrampoline(t: obj): bool` — Returns, whether `t` is a trampolined object.
- `wrap(t: Trampoline a | a): Trampoline a` — Wrap `t` into trampoline, if it's not already one.
- `lazy(t : () -> Trampoline a | a)` — Wrap lazy computation into trampoline. Useful when constructing computations.
- `Trampoline.jump(f : a -> b | Trampoline b)` — *map* or *flatmap* trampoline computation. Like `.then` for promises.
- `Trampoline.run(): a` — Run the trampoline synchronously resulting a value.
## Changelog
- **1.0.0** — *2015-07-14* — Initial release