{"id":15470704,"url":"https://github.com/dlesl/scala3-transducers","last_synced_at":"2025-03-28T12:43:14.209Z","repository":{"id":60149831,"uuid":"541306455","full_name":"dlesl/scala3-transducers","owner":"dlesl","description":null,"archived":false,"fork":false,"pushed_at":"2022-09-25T21:07:21.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-02T13:17:01.512Z","etag":null,"topics":["scala","scala3","transducers"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dlesl.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":"2022-09-25T20:52:45.000Z","updated_at":"2022-09-25T21:23:19.000Z","dependencies_parsed_at":"2022-09-25T21:48:31.435Z","dependency_job_id":null,"html_url":"https://github.com/dlesl/scala3-transducers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlesl%2Fscala3-transducers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlesl%2Fscala3-transducers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlesl%2Fscala3-transducers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlesl%2Fscala3-transducers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dlesl","download_url":"https://codeload.github.com/dlesl/scala3-transducers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246034276,"owners_count":20712851,"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":["scala","scala3","transducers"],"created_at":"2024-10-02T02:06:24.374Z","updated_at":"2025-03-28T12:43:14.184Z","avatar_url":"https://github.com/dlesl.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"## scala3-transducers\n\n### Experiments with transducers for Scala 3 (JVM, JS, Native)\n\nThis is a Scala version of [Clojure's\ntransducers](https://clojure.org/reference/transducers). They are implemented in\nmuch the same way and therefore should have similar performance and other\nproperties.\n\nThe main difference is that instead of using multiple arities, reducing\nfunctions are defined as a trait:\n\n```scala\ncase class Result[A](value: A, continue: Boolean = true)\n\ntrait Reducer[A, R]:\n    def init(): R\n    def step(result: R, input: A): Result[R]\n    def completion(result: R): R\n```\n\n`Result` is analogous to Clojure's `reduced`. \n\nA Transducer takes a reducing function and returns a new reducing function. As\nin Clojure, it is a function.\n\n```scala\ntype Transducer[A, B] = [R] =\u003e Reducer[B, R] =\u003e Reducer[A, R]\n```\n\n`[R] =\u003e` in the type signature means that this holds for any possible type `R`,\nin other words that a transducer is independent of the final result's type.\n\nNote also that the type arguments are swapped: A `Transducer[A, B]` converts a\n`Reducer[B, R]` to a `Reducer[A, R]`. This is done so that the order of the type\nparameters reflects the flow of the data, which is hopefully more intuitive. For\nexample, the type of `Transducer.map((a: Int) =\u003e a.toString)` is \n`Transducer[Int, String]`.\n\n### Example\n\n```scala\nimport com.dlesl.transducers.*\n\nval xf = Transducer\n  .map[Int, Int](_ + 1)\n  .map(_.toString)\n  .filter(_.length \u003c 2)\n  .map(Integer.parseInt)\n\nval toVector = Reducer.simple[Int, Vector[Int]](Vector.empty, _:+_)\ntoVector.reduce(Seq(1, 2, 3)) // Vector(1, 2, 3)\nxf(toVector).reduce(Seq(1, 2, 3)) // Vector(2, 3, 4)\n\nval sum = Reducer.simple[Int, Int](0, _+_)\nxf(sum).reduce(Seq(1, 2, 3)) // 9\n```\n\n### Notes\n\n* [transducers-scala](https://github.com/knutwalker/transducers-scala) is very\n  similar (and more complete), but I decided to write my own anyway as an\n  exercise in learning Scala :). It has a neat `into` implementation and a\n  different way of representing the \"reduced\" state (an `AtomicBoolean` passed\n  into the step function).\n  \n* The clojure implementation represents the reduced state by [wrapping the\n  value](https://github.com/clojure/clojure/blob/2b3ba822815981e7f76907a3b75e9ecc428f7656/src/clj/clojure/core.clj#L2853).\n  This is potentially more performant than wrapping every value in `Result` as\n  done here, given that unreduced values will be vastly more common. This might\n  be worth investigating (eg. returning `A | Reduced[A]`). TODO: benchmarks\n  \n* [This Haskell sketch of\n  transducers](https://github.com/FranklinChen/clojure-transducers-in-haskell/blob/master/Transducers.hs)\n  based on code by Rich Hickey helped me understand the concept using types.\n\n### TODO (maybe)\n\n* Investigate and compare to `fs2` and `ZIO` streams\n* Benchmarks\n* Feature parity with `clojure.core`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlesl%2Fscala3-transducers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdlesl%2Fscala3-transducers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlesl%2Fscala3-transducers/lists"}