Comparing version 0.0.0 to 0.0.1
16
index.js
@@ -83,2 +83,7 @@ /* | ||
/** | ||
* @param {(...args: any[]) => any} func | ||
* @param {any[]} args | ||
* @return {StackFrame} | ||
*/ | ||
function call(func, ...args) { | ||
@@ -88,2 +93,8 @@ return new StackFrame(func, args); | ||
/** | ||
* @param {any} thisArg | ||
* @param {(...args: any[]) => any} func | ||
* @param {any[]} args | ||
* @return {StackFrame} | ||
*/ | ||
function callWithContext(thisArg, func, ...args) { | ||
@@ -133,2 +144,7 @@ return StackFrame.withContext(thisArg, func, args); | ||
/** | ||
* @param {(...args: any[]) => any} func | ||
* @param {any} thisArg | ||
* @param {(...args: any[]) => any} func | ||
*/ | ||
function recursive(func, thisArg) { | ||
@@ -135,0 +151,0 @@ const name = func.name || 'recurse'; |
{ | ||
"name": "tallstack", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "A library that allows you to define recursive functions in JavaScript without stack overflow.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,2 +6,4 @@ # tallstack | ||
It is published for download on [npm](https://www.npmjs.com/package/tallstack). | ||
## Contents | ||
@@ -27,3 +29,3 @@ | ||
if (N === 0) return 1; | ||
return call(multiply, n, call(factorial, n - 1)); | ||
return call(multiply, N, call(factorial, N - 1)); | ||
}); | ||
@@ -56,6 +58,22 @@ ``` | ||
if (N === 0) return 1; | ||
return callWithContext(object, multiply, n, call(factorial, n - 1)); | ||
return callWithContext(object, multiply, N, call(factorial, N - 1)); | ||
}); | ||
``` | ||
The function `recurse` also takes an optional 2nd parameter and binds that object to | ||
the recursive function's `this` object. Extending the `N` factorial example again: | ||
```javascript | ||
const { call, callWithContext } = require('tallstack'); | ||
const obj = { multiply: (x, y) => (x * y) }; | ||
function factorialWithThis(N) { | ||
if (N === 0) return 1; | ||
return call(this.multiply, N, call(factorial, N - 1)); | ||
} | ||
const factorial = recursive(factorialWithThis, obj); | ||
``` | ||
## How does it work? | ||
@@ -62,0 +80,0 @@ |
10973
5
143
137