{"id":21730513,"url":"https://github.com/andy2046/tco-node","last_synced_at":"2025-04-13T00:14:55.304Z","repository":{"id":57376541,"uuid":"109213081","full_name":"andy2046/tco-node","owner":"andy2046","description":"javascript lib for tail call optimization","archived":false,"fork":false,"pushed_at":"2017-11-04T05:29:12.000Z","size":82,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T00:14:48.720Z","etag":null,"topics":["javascript","tail-call-optimization","tco","trampoline"],"latest_commit_sha":null,"homepage":"","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/andy2046.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}},"created_at":"2017-11-02T03:25:00.000Z","updated_at":"2021-07-18T01:38:37.000Z","dependencies_parsed_at":"2022-09-26T16:41:16.560Z","dependency_job_id":null,"html_url":"https://github.com/andy2046/tco-node","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Ftco-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Ftco-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Ftco-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Ftco-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andy2046","download_url":"https://codeload.github.com/andy2046/tco-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647274,"owners_count":21139086,"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":["javascript","tail-call-optimization","tco","trampoline"],"created_at":"2024-11-26T04:16:12.810Z","updated_at":"2025-04-13T00:14:55.285Z","avatar_url":"https://github.com/andy2046.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tco-node\ntco-node is a JavaScript library for tail call optimization.\n\n## Examples\n```js\nimport { tco, trampoline } from 'tco-node';\n\nconst sum = tco(function(x, y) {\n  if (y \u003e 0) {\n    return sum(x + 1, y - 1)\n  } else {\n    return x\n  }\n})\n\nconsole.log( sum(1, 100000) ) // =\u003e 100001\n\nfunction isEvenInner (num) {\n  if (num === 0) {\n    return true\n  }\n\n  if (num === 1) {\n    return false\n  }\n\n  return function() {\n    return isEvenInner(Math.abs(num) - 2)\n  }\n}\n\nconsole.log( trampoline(isEvenInner, 99999) )\n// =\u003e false\n\nconsole.log( trampoline(isEvenInner, 99998) )\n// =\u003e true\n\nconst isEven = trampoline.bind(null, isEvenInner)\n\nconsole.log( isEven(99999) )\n// =\u003e false\n\n\nfunction factorial (n) {\n  function recur (n, acc) {\n    if (n === 0) {\n      return acc\n    } else {\n      return recur.bind(null, n-1, n*acc)\n    }\n  }\n  return function () {\n    return recur.bind(null, n, 1)\n  }\n}\n\nconsole.log( trampoline(factorial(100000)) )\n// =\u003e Infinity\n\nfunction _even (n) {\n  if (n === 0) {\n    return true\n  } else {\n    return _odd.bind(null, n-1)\n  }\n}\n\nfunction _odd (n) {\n  if (n === 0) {\n    return false\n  } else {\n    return _even.bind(null, n-1)\n  }\n}\n\nfunction even (n) {\n  return trampoline(_even.bind(null, n))\n}\n\nfunction odd (n) {\n  return trampoline(_odd.bind(null, n))\n}\n\nconsole.log(\n  even(99999),\n  odd(99999),\n  even(99998),\n  odd(99998)\n)\n// false true true false\n\n```\n\n## Installation\n\n```\nnpm install --save tco-node\n```\n\n## Usage\nYou can import from `tco-node`:\n\n```js\nimport { tco, trampoline } from 'tco-node';\n// or\nconst { tco, trampoline } = require('tco-node');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy2046%2Ftco-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandy2046%2Ftco-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy2046%2Ftco-node/lists"}