{"id":17981473,"url":"https://github.com/francisrstokes/simple-transduce","last_synced_at":"2025-04-04T01:22:53.674Z","repository":{"id":57361029,"uuid":"112488241","full_name":"francisrstokes/simple-transduce","owner":"francisrstokes","description":"A really simple transducer module to easily convert map-filter-reduce chains to single pass transducers.","archived":false,"fork":false,"pushed_at":"2017-11-30T12:41:34.000Z","size":5,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T06:21:11.547Z","etag":null,"topics":["functional","javascript","library","transducer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/francisrstokes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-29T14:52:02.000Z","updated_at":"2023-11-20T04:51:45.000Z","dependencies_parsed_at":"2022-09-06T22:23:07.076Z","dependency_job_id":null,"html_url":"https://github.com/francisrstokes/simple-transduce","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/francisrstokes%2Fsimple-transduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fsimple-transduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fsimple-transduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fsimple-transduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/francisrstokes","download_url":"https://codeload.github.com/francisrstokes/simple-transduce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247104221,"owners_count":20884189,"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":["functional","javascript","library","transducer"],"created_at":"2024-10-29T18:10:02.277Z","updated_at":"2025-04-04T01:22:53.656Z","avatar_url":"https://github.com/francisrstokes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-transduce\n\nA really simple transducer module to easily convert map-filter-reduce chains to single pass transducers.\n\n## Install\n\n```bash\nnpm install simple-transduce\n```\n\n## Concept\n\nTake some existing code like this:\n\n```javascript\nconst collection = [1,2,3,4,5,6,7,8,9,10];\n\nconst multiplyBy5 = (value) =\u003e value + 5;\nconst notMultipleOf3 = (value) =\u003e value % 3 !== 0;\nconst square = (value) =\u003e value * value;\nconst notMultipleOf4 = (value) =\u003e value % 4 !== 0;\n\nconst newCollection = collection\n  .map(multiplyBy5)\n  .filter(notMultipleOf3)\n  .map(square)\n  .filter(notMultipleOf4);\n\n// Result = [ 49, 121, 169 ]\n// Total elements of all intermediate collections, including original: 34\n```\n\nThis works fine, and the original list is transformed from 10 members to just 3 in the end, as expected.\nHowever this transformation happens over multiple intermediate collections, meaning while the original array has 10 elements,\nyou actually iterate over 4 collections of 10, and thus (potentially) 40 elements. A transducer allows for a generic method that\nlets you do just a single pass of the 10 elements without sacrificing the clarity of having those 4 distinct transformation functions.\n\n### Converting map-filter to transduce\n\n```javascript\nconst st = require('simple-transduce');\n\nconst collection = [1,2,3,4,5,6,7,8,9,10];\n\nconst multiplyBy5 = st.mapTransducer((value) =\u003e value + 5);\nconst notMultipleOf3 = st.filterTransducer((value) =\u003e value % 3 !== 0);\nconst square = st.mapTransducer((value) =\u003e value * value);\nconst notMultipleOf4 = st.filterTransducer((value) =\u003e value % 4 !== 0);\n\nconst transducer = st.compose(multiplyBy5, notMultipleOf3, square, notMultipleOf4);\n\nconst newCollection = st.reduce(transducer(st.concat), [], collection);\n\n// Result = [ 49, 121, 169 ]\n// Total elements of all intermediate collections, including original: 10\n```\n\nNote that `compose`, `reduce` and `concat` are not special functions - they are found in many common functional libraries (and lodash)\nand are thus interchangable with those.\n\n## Functions\n\n- `mapTransducer(mapFunction)` -\u003e creates a composable transducer from the map function\n- `filterTransducer(filter)` -\u003e creates a composable transducer from the filter function\n- `reduce(reducerFunction, start, collection)` -\u003e generic reduce\n- `compose(...fns)` -\u003e generic compose. Example: `compose(f, g) = (x) =\u003e f(g(x))`\n- `pipe(...fns)` -\u003e generic pipe (compose inverse). Example: `pipe(f, g) = (x) =\u003e g(f(x))`\n- `concat(acc, item)` -\u003e generic concat reducer\n- `sum(acc, item` -\u003e generic sum reducer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fsimple-transduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrancisrstokes%2Fsimple-transduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fsimple-transduce/lists"}