{"id":21839372,"url":"https://github.com/darkleaf/generator","last_synced_at":"2025-03-21T15:22:00.636Z","repository":{"id":49581847,"uuid":"298352440","full_name":"darkleaf/generator","owner":"darkleaf","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-13T14:19:20.000Z","size":41,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-26T10:42:10.844Z","etag":null,"topics":[],"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/darkleaf.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-09-24T17:45:44.000Z","updated_at":"2021-07-26T16:06:16.000Z","dependencies_parsed_at":"2022-09-21T17:00:32.024Z","dependency_job_id":null,"html_url":"https://github.com/darkleaf/generator","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkleaf%2Fgenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkleaf%2Fgenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkleaf%2Fgenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkleaf%2Fgenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darkleaf","download_url":"https://codeload.github.com/darkleaf/generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244819883,"owners_count":20515662,"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":"2024-11-27T21:17:28.864Z","updated_at":"2025-03-21T15:22:00.604Z","avatar_url":"https://github.com/darkleaf.png","language":"Clojure","readme":"+ [![CircleCI](https://circleci.com/gh/darkleaf/generator.svg?style=svg)](https://circleci.com/gh/darkleaf/generator)\n+ [![Clojars Project](https://img.shields.io/clojars/v/darkleaf/generator.svg)](https://clojars.org/darkleaf/generator)\n\n# Generator\n\nThe generator library brings [js-like](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) generators,\nalso known as continuations, to Clojure(Script).\n\nGenerators are useful for building [effect systems](https://overreacted.io/algebraic-effects-for-the-rest-of-us/) like:\n\n* [redux-saga](https://redux-saga.js.org/) for JavaScript. This is an awesome example of using generators. Check it out first!\n* [darkleaf/effect](https://github.com/darkleaf/effect) for Clojure(Script)\n\nSpecial thanks to [@leonoel](https://github.com/leonoel)\nfor his [cloroutine](https://github.com/leonoel/cloroutine).\n\n```clojure\n(require '[darkleaf.generator.core :as gen :refer [generator yield]])\n\n(let [f*  (fn []\n            (generator\n             (yield :my-value)))\n      gen (f*)]\n  (assert (= :my-value (gen/value gen)))\n  (gen/next gen :my-covalue)\n  (assert (= :my-covalue (gen/value gen)))\n  (assert (gen/done? gen)))\n```\n\nFor more examples, see [the test suite](test/darkleaf/generator/core_test.cljc).\n\nContinuations are not first class citizens in an underluing platform like JVM or V8, so we face with\n[colored functions](http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/).\nFunctions that return a generator are red in this terminology, and regular functions are blue.\nWe can't pass our red functions to blue ones. For example we can't pass them to functions like `map` or `reduce`.\nSo the library provides `gen/mapv*` and `gen/reduce*`.\n\nBy default generators are stackless, so\nif you want to call one red function from another one, you have to use `gen/wrap-stack` middleware:\n\n```clojure\n(let [nested* (fn []\n                (generator\n                 [(yield :a)\n                  (yield :b)]))\n      f*      (fn []\n                (generator\n                 [(yield :start)\n                  (yield (nested*))\n                  (yield :finish)]))\n      f*      (gen/wrap-stack f*)\n      gen     (f*)]\n  (assert (= :start (gen/value gen)))\n  (gen/next gen 1)\n  (assert (= :a (gen/value gen)))\n  (gen/next gen 2)\n  (assert (= :b (gen/value gen)))\n  (gen/next gen 3)\n  (assert (= :finish (gen/value gen)))\n  (gen/next gen 4)\n  (assert (= [1 [2 3] 4] (gen/value gen)))\n  (assert (gen/done? gen)))\n```\n\nFortunately, there is [Project Loom](https://openjdk.java.net/projects/loom/),\nwhich will bring first-class continuations on the JVM.\nWith Loom, it is possible to use `yield` (1) in regular nested functions called by generator (3).\nAlso, they can be passed into regular higher-order functions like `mapv` (2):\n\n```clojure\n(ns darkleaf.generator.loom-test\n  (:require\n   [darkleaf.generator.core :as gen]\n   ;; Loom support is in a separate namespace\n   [darkleaf.generator.loom :refer [generator yield]]\n   ...))\n\n(t/deftest loom-killer-feature-test\n  (let [nested (fn [x]\n                 (yield [:inc x]))      ;; (1)\n        f      (fn []\n                 (mapv nested [0 1 2])) ;; (2)\n        f*     (fn []\n                 (generator             ;; (3)\n                  (f)))\n        gen    (f*)]\n    (t/is (= [:inc 0] (gen/value gen)))\n    (gen/next gen 1)\n    (t/is (= [:inc 1] (gen/value gen)))\n    (gen/next gen 2)\n    (t/is (= [:inc 2] (gen/value gen)))\n    (gen/next gen 3)\n\n    (t/is (= [1 2 3] (gen/value gen)))\n    (t/is (gen/done? gen))))\n```\n\nTo play with it you need to use  [Loom's early access builds](https://jdk.java.net/loom/)\nand an [special version of this library](https://clojars.org/darkleaf/generator/versions/1.0.1-loom).\nCheck out [tests](https://github.com/darkleaf/generator/blob/loom2/test/darkleaf/generator/loom_test.clj).\n\n## Pro tips\n\nYou can use threading macors like this:\n\n```clojure\n(generator\n (-\u003e value\n     regular-fn\n     (-\u003e gen-fn* yield)\n     other-regular-fn\n     (-\u003e other-gen-fn* yield)))\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkleaf%2Fgenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarkleaf%2Fgenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkleaf%2Fgenerator/lists"}