{"id":20576513,"url":"https://github.com/divs1210/recursor","last_synced_at":"2026-06-05T17:31:15.015Z","repository":{"id":62434414,"uuid":"259896673","full_name":"divs1210/recursor","owner":"divs1210","description":"Better recursion for Clojure","archived":false,"fork":false,"pushed_at":"2020-05-23T13:14:11.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-20T09:52:48.224Z","etag":null,"topics":["clojure","optimize","recursion","stack","stackoverflow"],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/divs1210.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":"2020-04-29T10:38:22.000Z","updated_at":"2021-07-15T16:59:58.000Z","dependencies_parsed_at":"2022-11-01T21:16:05.755Z","dependency_job_id":null,"html_url":"https://github.com/divs1210/recursor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/divs1210/recursor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Frecursor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Frecursor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Frecursor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Frecursor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/divs1210","download_url":"https://codeload.github.com/divs1210/recursor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Frecursor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33951164,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-05T02:00:06.157Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["clojure","optimize","recursion","stack","stackoverflow"],"created_at":"2024-11-16T05:46:02.708Z","updated_at":"2026-06-05T17:31:14.994Z","avatar_url":"https://github.com/divs1210.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# recursor\n[![Clojars Project](https://img.shields.io/clojars/v/recursor.svg)](https://clojars.org/recursor)\n[![CircleCI](https://circleci.com/gh/divs1210/recursor/tree/master.svg?style=svg)](https://circleci.com/gh/divs1210/recursor/tree/master)\n[![codecov](https://codecov.io/gh/divs1210/recursor/branch/master/graph/badge.svg)](https://codecov.io/gh/divs1210/recursor)\n\nBetter recursion for Clojure.\n\n## Usage\n\nHere's the Ackermann function:\n\n```\nA(0, n) = n+1\nA(m, 0) = A(m-1, 1)\nA(m, n) = A(m-1, A(m, n-1))\n```\n\nHere's a straightforward implementation in Clojure:\n\n```clojure\n(defn A [m n]\n  (cond\n    (zero? m) (inc n)\n    (zero? n) (recur (dec m) 1)\n    :else (recur (dec m) (A m (dec n)))))\n```\n\nGreat! Let's try it out!\n\n```clojure\nuser\u003e (time (A 3 8))\n\"Elapsed time: 85.518103 msecs\"\n;; =\u003e 2045\nuser\u003e (time (A 4 1))\n;; StackOverflowError !!!\n```\n\nNow, while you:\n\n1. look for a mathematician, and\n2. plead them to derive an iterative version of the function, and\n3. refuse to budge till they optimize it\n\nI will do this:\n\n```clojure\n(require ' [recursor.core :as r :refer [defrec recurse return]])\n\n(defrec ^{::r/cache-size 100000}\n  A\n  [m n]\n  (cond\n    (zero? m) (return (inc n))\n    (zero? n) (recurse (A (dec m) 1))\n    :else (recurse (A m (dec n))\n            :then #(A (dec m) %))))\n```\n\nLet's see what it can do:\n\n```clojure\nuser\u003e (time (A 3 8))\n\"Elapsed time: 95.970201 msecs\"\n;; =\u003e 2045\nuser\u003e (time (A 4 1))\n\"Elapsed time: 2790.475629 msecs\"\n;; =\u003e 65533\n```\n\nIt achieves this by:\n\n1. Using a custom **unbounded** stack\n2. **Optionally** using an [LU cache](https://github.com/clojure/core.cache/wiki/LU) to remember past results if specified in the metadata\n\nIt also provides replacements for some other Clojure core forms, all of which can be memoized by setting `^{::r/cache-size \u003cSOME-NUMBER\u003e}` before their names:\n\n| clojure.core  | recursor.core |\n|:-------------:|:-------------:|\n| `fn`          | `recfn`       |\n| `defn`        | `defrec`      |\n| `defn-`       | `defrec-`     |\n| `letfn`       | `letrec`      |\n\nAll of these work in the way you would expect them to, with the following caveats:\n\n1. No varargs in `recfn`s - use a `fn` with varargs to dispatch to fixed-arity `recfn`\n2. No multiple-arity `recfn`s - use a multi-arity `fn` to dispatch to fixed-arity `recfn`\n3. No destructuring maps in arguments vector - `(recfn f [{:keys [a b]}])`\n4. No mutual recursion support - use [trampoline](https://clojuredocs.org/clojure.core/trampoline) as usual\n\nAll this is subject to change **without** breaking the API.\n\n## License\n\nCopyright © 2020 Divyansh Prakash\n\nThis program and the accompanying materials are made available under the\nterms of the Eclipse Public License 2.0 which is available at\nhttp://www.eclipse.org/legal/epl-2.0.\n\nThis Source Code may also be made available under the following Secondary\nLicenses when the conditions for such availability set forth in the Eclipse\nPublic License, v. 2.0 are satisfied: GNU General Public License as published by\nthe Free Software Foundation, either version 2 of the License, or (at your\noption) any later version, with the GNU Classpath Exception which is available\nat https://www.gnu.org/software/classpath/license.html.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivs1210%2Frecursor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivs1210%2Frecursor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivs1210%2Frecursor/lists"}