{"id":19910982,"url":"https://github.com/streetsidesoftware/gensequence","last_synced_at":"2025-04-07T15:09:11.196Z","repository":{"id":14352699,"uuid":"76385880","full_name":"streetsidesoftware/GenSequence","owner":"streetsidesoftware","description":"Small library to simplify working with Generators and Iterators in Javascript / Typescript","archived":false,"fork":false,"pushed_at":"2025-04-06T12:04:47.000Z","size":1286,"stargazers_count":7,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T18:49:18.693Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/streetsidesoftware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-13T18:15:53.000Z","updated_at":"2025-03-30T12:30:54.000Z","dependencies_parsed_at":"2024-01-21T13:24:27.473Z","dependency_job_id":"5793bcbc-3738-4043-93ec-bff9094431f3","html_url":"https://github.com/streetsidesoftware/GenSequence","commit_stats":{"total_commits":296,"total_committers":7,"mean_commits":"42.285714285714285","dds":0.6587837837837838,"last_synced_commit":"e713b229000017d0ccc33c20facc609a5630a16c"},"previous_names":["jason3s/gensequence"],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2FGenSequence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2FGenSequence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2FGenSequence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2FGenSequence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/streetsidesoftware","download_url":"https://codeload.github.com/streetsidesoftware/GenSequence/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247675597,"owners_count":20977376,"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-12T21:22:45.727Z","updated_at":"2025-04-07T15:09:11.176Z","avatar_url":"https://github.com/streetsidesoftware.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GenSequence\n\n[![Coverage Status](https://coveralls.io/repos/github/streetsidesoftware/GenSequence/badge.svg?branch=main)](https://coveralls.io/github/streetsidesoftware/GenSequence?branch=main)\n\nSmall library to simplify working with Generators and Iterators in Javascript / Typescript\n\nJavascript [Iterators and Generators](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators_and_Generators)\nare very exciting and provide some powerful new ways to solve programming problems.\n\nThe purpose of this library is to make using the results of a generator function easier.\nIt is not intended as a replacement for arrays and the convenient `[...genFn()]` notation.\nGenSequence is useful for cases where you might not want an array of all possible values.\nGenSequence deals efficiently with large sequences because only one element at a time is evaluated.\nIntermediate arrays are not created, saving memory and cpu cycles.\n\n## Installation\n\n```\nnpm install -S gensequence\n```\n\n## Usage\n\n```\nimport { genSequence } from 'gensequence';\n```\n\n## Examples\n\n### Fibonacci\n\nThe Fibonacci sequence can be very simply expressed using a generator. Yet using the result of a generator can be a bit convoluted.\nGenSequence provides a wrapper to add familiar functionality similar to arrays.\n\n```javascript\nfunction fibonacci() {\n  function* fib() {\n    let [a, b] = [0, 1];\n    while (true) {\n      yield b;\n      [a, b] = [b, a + b];\n    }\n  }\n  // Wrapper the Iterator result from calling the generator.\n  return genSequence(fib);\n}\n\nlet fib5 = fibonacci()\n  .take(5) // Take the first 5 from the fibonacci sequence\n  .toArray(); // Convert it into an array\n// fib5 == [1, 1, 2, 3, 5]\n\nlet fib6n7seq = fibonacci().skip(5).take(2);\nlet fib6n7arr = [...fib6n7seq]; // GenSequence are easily converted into arrays.\n\nlet fib5th = fibonacci()\n  .skip(4) // Skip the first 4\n  .first(); // Return the next one.\n```\n\n### RegEx Match\n\nRegular expressions are wonderfully powerful. Yet, working with the results can sometimes be a bit of a pain.\n\n```javascript\nfunction* execRegEx(reg: RegExp, text: string) {\n  const regLocal = new RegExp(reg);\n  let r;\n  while ((r = regLocal.exec(text))) {\n    yield r;\n  }\n}\n\n/* return a sequence of matched text */\nfunction match(reg: RegExp, text: string) {\n  return (\n    genSequence(execRegEx(reg, text))\n      // extract the full match\n      .map((a) =\u003e a[0])\n  );\n}\n\n/* extract words out of a string of text */\nfunction matchWords(text: string) {\n  return genSequence(match(/\\w+/g, text));\n}\n\n/* convert some text into a set of unique words */\nfunction toSetOfWords(text: string) {\n  // Sequence can be used directly with a Set or Match\n  return new Set(matchWords(text));\n}\n\nconst text = 'Some long bit of text with many words, duplicate words...';\nconst setOfWords = toSetOfWords(text);\n// Walk through the set of words and pull out the 4 letter one.\nconst setOf4LetterWords = new Set(genSequence(setOfWords).filter((a) =\u003e a.length === 4));\n```\n\n## Reference\n\n- `genSequence(Iterable|Array|()=\u003eIterable)` -- generate a new Iterable from an Iterable, Array or function with the following functions.\n\n### Filters\n\n- `.filter(fn)` -- just like array.filter, filters the sequence\n- `.skip(n)` -- skip _n_ entries in the sequence\n- `.take(n)` -- take the next _n_ entries in the sequence.\n\n### Extenders\n\n- `.concat(iterable)` -- this will extend the current sequence with the values from _iterable_\n- `.concatMap(fnMap)` -- this is used to flatten the result of a map function.\n\n### Mappers\n\n- `.combine(fnCombiner, iterable)` -- is used to combine values from two different lists.\n- `.map(fn)` -- just like array.map, allows you to convert the values in a sequence.\n- `.pipe(...operatorFns)` -- pipe any amount of operators in sequence.\n- `.scan(fn, init?)` -- similar to reduce, but returns a sequence of all the results of fn.\n\n### Reducers\n\n- `.all(fn)` -- true if all values in the sequence return true for _fn(value)_ or the sequence is empty.\n- `.any(fn)` -- true if any value in the sequence exists where _fn(value)_ returns true.\n- `.count()` -- return the number of values in the sequence.\n- `.first()` -- return the next value in the sequence.\n- `.first(fn)` -- return the next value in the sequence where _fn(value)_ return true.\n- `.forEach(fn)` -- apply _fn(value, index)_ to all values.\n- `.max()` -- return the largest value in the sequence.\n- `.max(fn)` -- return the largest value of _fn(value)_ in the sequence.\n- `.min()` -- return the smallest value in the sequence.\n- `.min(fn)` -- return the smallest value of _fn(value)_ in the sequence.\n- `.reduce(fn, init?)` -- just like array.reduce, reduces the sequence into a single result.\n- `.reduceAsync(fn, init?)` -- just like array.reduce, reduces promises into the sequence into a single result chaining the promises, fn/init can be async or not, it will work, the previousValue, and currentValue will never be a promise.\n- `.reduceToSequence(fn, init)` -- return a sequence of values that _fn_ creates from looking at all the values and the initial sequence.\n\n### Cast\n\n- `.toArray()` -- convert the sequence into an array. This is the same as [...iterable].\n- `.toIterable()` -- Casts a Sequence into an IterableIterator - used in cases where type checking is too strict.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetsidesoftware%2Fgensequence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstreetsidesoftware%2Fgensequence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetsidesoftware%2Fgensequence/lists"}