# recursive-free

> Recursive without stack overflow

Latest version **1.0.6** (published 2024-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install recursive-free
pnpm add recursive-free
yarn add recursive-free
bun add recursive-free
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2024-08-14 |
| First published | 2022-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ruojianll |
| Keywords | recursive, stack-overflow, stack, loop |

## Links

- npm: https://www.npmjs.com/package/recursive-free
- npm.io page: https://npm.io/package/recursive-free

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2024-08-14
- 1.0.5 — 2024-08-14
- 1.0.4 — 2024-08-14
- 1.0.3 — 2023-03-21
- 1.0.2 — 2022-09-19
- 1.0.1 — 2022-09-18
- 0.0.1 — 2022-09-18

## README

# Readme

Make your recursive-like functions leave stack overflow.

* Simulate function stack to void stack overflow in recursive-like functions.
* High performance implemented by JavaScript `Map`.


## Install

`npm install --save recursive-free`

## Usage

Write your recursive in `function *`. `yield` means call self.

`recursiveFree` has tow generic types. First for function paramater, also provide after `yeild` expression. Second for function return value, also recevied from `yeild` expression.

> Just consider `yield` is a calling of the function self.

```typescript

import recursiveFree from 'recursive-free'

const rec = recursiveFree<number, string>(function* (num) {
    if (num === 1) {
        return `,${num}`
    }
    return `${yield num - 1},${num}`
})

console.log(
    rec(5)
)

// ,1,2,3,4,5

```

## Example

```typescript
//Call 100000 times. It will throw stack overflow error in normal recursive functions.

import recursiveFree from 'recursive-free'

const rec = recursiveFree<number, void>(function* (num) {
    if (num === 1000000) {
        console.log('finish')
        return
    }
    yield num + 1
})
let t = performance.now()
rec(1)
console.log(`${performance.now() - t}ms`)

//finish
//500ms
```

```typescript
//Fibonacci

import recursiveFree from 'recursive-free'

const rec = recursiveFree<number, number>(function* (n) {
    if (n < 2) {
        return n
    }
    return (yield n - 1) + (yield n - 2)
})

console.log([1, 2, 3, 4, 5, 6, 7, 8].map(ind => rec(ind)).join(','))
//1,1,2,3,5,8,13,21
```

```typescript
//Output result of [1,2,3] X [1,2,3] X [1,2,3]

import recursiveFree from 'recursive-free'

const rec = recursiveFree<{
    num: number
    str: string
}, void>(function* (param) {
    const { str, num } = param
    if (num === 3) {
        console.log(str)
        return
    }
    for (let i of [1, 2, 3]) {
        yield {
            str: `${str},${i}`,
            num: num + 1
        }
    }
})
rec({
    num: 0,
    str: ''
})
```

---
_Source: https://npm.io/package/recursive-free · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
