{"id":26221687,"url":"https://github.com/snowflyt/tailstep","last_synced_at":"2025-04-17T08:46:59.260Z","repository":{"id":269799174,"uuid":"908512106","full_name":"Snowflyt/tailstep","owner":"Snowflyt","description":"Simple trampoline-based tail recursion for modern JavaScript/TypeScript","archived":false,"fork":false,"pushed_at":"2024-12-26T08:59:57.000Z","size":243,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T16:05:27.629Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Snowflyt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-26T08:54:29.000Z","updated_at":"2025-02-27T17:12:04.000Z","dependencies_parsed_at":"2024-12-26T09:08:21.662Z","dependency_job_id":"a9a51eda-16e9-41bf-bdc3-8c433844da95","html_url":"https://github.com/Snowflyt/tailstep","commit_stats":null,"previous_names":["snowflyt/tailstep"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Snowflyt%2Ftailstep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Snowflyt%2Ftailstep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Snowflyt%2Ftailstep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Snowflyt%2Ftailstep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Snowflyt","download_url":"https://codeload.github.com/Snowflyt/tailstep/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741198,"owners_count":21154253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-12T16:29:12.404Z","updated_at":"2025-04-17T08:46:59.237Z","avatar_url":"https://github.com/Snowflyt.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003etailstep\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\nSimple \u003cstrong\u003etrampoline\u003c/strong\u003e-based \u003cstrong\u003etail recursion\u003c/strong\u003e for modern JavaScript/TypeScript\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/tailstep\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/tailstep.svg\" alt=\"npm version\" height=\"18\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://bundlephobia.com/package/tailstep\"\u003e\n    \u003cimg src=\"https://img.shields.io/bundlephobia/minzip/tailstep.svg\" alt=\"minzipped size\" height=\"18\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/Snowflyt/tailstep/actions/workflows/test.yml\"\u003e\n    \u003cimg src=\"https://github.com/Snowflyt/tailstep/actions/workflows/test.yml/badge.svg\" alt=\"test status\" height=\"18\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://coveralls.io/github/Snowflyt/tailstep?branch=main\"\u003e\n    \u003cimg src=\"https://coveralls.io/repos/github/Snowflyt/tailstep/badge.svg?branch=main\" alt=\"coverage status\" height=\"18\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/gvergnaud/tailstep\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/l/tailstep.svg\" alt=\"MIT license\" height=\"18\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n![screenshot](./screenshot.svg)\n\n## About\n\nTail recursion is a technique that enables recursive functions to be optimized into loops, preventing stack overflow in many programming languages. However, JavaScript does not natively support tail call optimization (TCO). This library offers a simple way to implement tail-recursive functions in JavaScript/TypeScript.\n\n## Installation\n\n```shell\nnpm install tailstep\n```\n\n## Usage\n\nConsider how we can implement a factorial function using tail recursion in simple JavaScript:\n\n```javascript\nfunction factorial(n, acc = 1) {\n  if (n === 0) return acc;\n  return factorial(n - 1, n * acc);\n}\n```\n\nHowever, this implementation will cause a stack overflow when `n` is large enough. To prevent this, we can use the `tailRec` function provided by this library:\n\n```javascript\nimport { done, step, tailRec } from \"tailstep\";\n\nconst factorial = tailRec((n, acc = 1) =\u003e {\n  if (n === 0) return done(acc);\n  return step(n - 1, n * acc);\n});\n```\n\nHere, `done` is used to return the final result, while `step` signals that the recursion should continue with the given arguments. The `tailRec` function automatically converts the tail-recursive function into a loop.\n\nFor TypeScript users, simply add type annotations to the function arguments:\n\n```typescript\nimport { done, step, tailRec } from \"tailstep\";\n\nconst factorial = tailRec((n: number, acc: number = 1) =\u003e {\n  //  ^?: (n: number, acc?: number) =\u003e number\n  if (n === 0) return done(acc);\n  return step(n - 1, n * acc);\n});\n```\n\nFor easier debugging, consider using a named function instead of an arrow function. This ensures that the function name will appear in the stack trace:\n\n```typescript\nconst factorial = tailRec(function factorial(n, acc = 1) {\n  if (n === 0) return done(acc);\n  return step(n - 1, n * acc);\n});\n```\n\nThe `tailRec` function may not work well with TypeScript when defining functions with generic types. In such cases, you may prefer to use the traditional trampoline approach:\n\n```typescript\nimport { done, cont, bounce, type Bounce } from \"tailstep\";\n\nfunction factorial(n: number): number {\n  function loop(acc: number, n: number): Bounce\u003cnumber\u003e {\n    if (n === 0) return done(acc);\n    return cont(() =\u003e loop(n * acc, n - 1));\n  }\n  return bounce(loop(1, n));\n}\n```\n\nHere’s how these functions are implemented in the library if you’re curious:\n\n```javascript\nconst done = (value) =\u003e ({ _tag: \"done\", value });\nconst cont = (thunk) =\u003e ({ _tag: \"cont\", thunk });\n\nfunction bounce(bounce) {\n  let current = bounce;\n  while (current._tag === \"cont\") current = current.thunk();\n  return current.value;\n}\n\nconst step = (...args) =\u003e ({ _tag: \"stepSignal\", args });\n\nfunction tailRec(f) {\n  const fn = (...args) =\u003e {\n    let current = f(...args);\n    while (current._tag === \"stepSignal\") current = f(...current.args);\n    return current.value;\n  };\n  return Object.defineProperty(fn, \"name\", { value: f.name });\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnowflyt%2Ftailstep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnowflyt%2Ftailstep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnowflyt%2Ftailstep/lists"}