Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crubier/infinistack
Infinite recursion in JS without stack overflow errors, based on magic 🎩✨🐇
https://github.com/crubier/infinistack
functional-programming js recursion
Last synced: 7 days ago
JSON representation
Infinite recursion in JS without stack overflow errors, based on magic 🎩✨🐇
- Host: GitHub
- URL: https://github.com/crubier/infinistack
- Owner: crubier
- License: mit
- Created: 2018-04-15T22:04:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-04-16T20:09:02.000Z (over 6 years ago)
- Last Synced: 2024-09-17T15:33:49.853Z (about 2 months ago)
- Topics: functional-programming, js, recursion
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 25
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Infinistack 🎩✨🐇
Infinite recursion in JS without stack overflow errors!
Based on magic, memoization, abuse of exceptions and the work of [@razimantv](https://gist.github.com/razimantv/1b33d4a090a5bc9ed94928012b37c3f0).
If you need this package to make your code work, then my advice would be to rethink your code structure. This library works, but is not efficient or safe. Instead of using this, unroll your recursion into iterative algorithms, you will thank me later.
By [@crubier](https://github.com/crubier)
## Install
```bash
npm install infinistack
```## Usage
Here is a classical, "dumb" factorial implementation:
```javascript
const factorial = N => {
if (N == 0) {
return 1;
}
return N * factorial(N - 1);
};// Don't do this, it will crash:
console.log(factorial(180000));
```This can be transformed with infinistack in order to emancipate from stack overflow errors:
```javascript
import infinistack from "infinistack";const factorial = infinistack(N => {
if (N == 0) {
return 1;
}
return N * factorial(N - 1);
});// This works now!
console.log(factorial(180000));
```For more info, see [the tests](https://github.com/crubier/infinistack/blob/master/test.js)
Amazing. Thanks to [@razimantv](https://github.com/razimantv) for [the original idea in python](https://gist.github.com/razimantv/1b33d4a090a5bc9ed94928012b37c3f0)
## Caveats
Not yet tested on:
* Async functions
* Complex recursion schemes
* Function with non-stringifiable arguments (higher order functions for example)