{"id":17925012,"url":"https://github.com/mwpastore/ruby-piecewise","last_synced_at":"2025-04-03T11:14:54.265Z","repository":{"id":56888060,"uuid":"132274331","full_name":"mwpastore/ruby-piecewise","owner":"mwpastore","description":"Piecewise Enumerables, Enumerators, and Lazy Enumerators for Ruby","archived":false,"fork":false,"pushed_at":"2018-05-17T19:20:10.000Z","size":12,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T16:09:49.413Z","etag":null,"topics":["ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/mwpastore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-05T18:30:50.000Z","updated_at":"2018-05-17T19:20:12.000Z","dependencies_parsed_at":"2022-08-21T00:50:51.603Z","dependency_job_id":null,"html_url":"https://github.com/mwpastore/ruby-piecewise","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mwpastore%2Fruby-piecewise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mwpastore%2Fruby-piecewise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mwpastore%2Fruby-piecewise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mwpastore%2Fruby-piecewise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mwpastore","download_url":"https://codeload.github.com/mwpastore/ruby-piecewise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246989753,"owners_count":20865331,"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":["ruby"],"created_at":"2024-10-28T20:51:58.475Z","updated_at":"2025-04-03T11:14:54.237Z","avatar_url":"https://github.com/mwpastore.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Piecewise Rubygem\n\nPiecewise Enumerables, Enumerators, and Lazy Enumerators for Ruby.\n\n### Synopsis\n\nDo you ever find yourself doing something like:\n\n```ruby\neven_keys = tuples.map { |k, v| k.upcase if v.even? }.compact\n\n# Or:\n\neven_keys = tuples.select { |_, v| v.even? }.keys.map(\u0026:upcase)\n\n# Or:\n\neven_keys = tuples.each_with_object([]) do |(k, v), memo|\n  memo \u003c\u003c k.upcase if v.even?\nend\n```\n\n**Now consider the case when `tuples` is infinitely large and you want to yield\neven keys, capitalized, to a downstream consumer.**\n\nI've always found this pattern (and its alternatives) a little bit off-putting,\nespecially when you have a big `do`..`end` block containing conditional logic\nand then a `.compact` tacked on to it. (Maybe I'm picky.) And sometimes `nil`\nis a valid value, so you have to introduce a new sentinel value to reject by.\nYou can wrap the logic in an `Enumerator.new { |yielder| .. }` or move the\nlogic to a method that returns an enumerator, but that can be a lot of\nboilerplate for what should be a simple filter+map operation.\n\nThis simple gem monkeypatches Enumerable, Enumerator, and Enumerator::Lazy to\nadd a `#piecewise` method that lets you do kind of an inline enumerator. The\nabove example can be rewritten like this:\n\n```ruby\neven_keys = tuples.piecewise { |yielder, (k, v)| yielder \u003c\u003c k.upcase if v.even? }\n```\n\nI find this easier to read and grok, and it works the same whether `tuples` is\na simple enumerable or a (lazy) enumerator.\n\n### Concept\n\nThis gem implements an [inversion of control][1] pattern in which the\nunderlying Enumerator::Yielder object of the [Enumerator][2] is yielded back to\nthe caller to make use of directly. This makes it easy to perform\n[piecewise][3] filter and/or map operations over an enumerable collection. It\nis particularly useful when not all of the elements on the input are guaranteed\nto be present on the output (otherwise a simple `#map` would do) *and* when\nsome of the elements may be mutated by the filter (otherwise a simple `#select`\nwould do). The confluence of these two requirements can lead to inelegant Ruby\ncode.\n\nYou can think of `#piecewise` like Enumerable#each_with_object and\nEnumerator#with_object with the following key differences:\n\n* The order of block arguments is reversed\n* The \"memo\" is *always* an Enumerator::Yielder (not an arbitrary object)\n* When the receiver is an Enumerator, the result is an Enumerator (not an\n  array)\n* When the receiver is an Enumerator::Lazy, the result is an Enumerator::Lazy\n  (not an array)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'piecewise'\n```\n\nAnd then execute:\n\n```sh\n$ bundle\n```\n\nOr install it yourself like so:\n\n```sh\n$ gem install piecewise\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run\n`rake test` to run the tests. You can also run `bin/console` for an interactive\nprompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To\nrelease a new version, update the version number in `version.rb`, and then run\n`bundle exec rake release`, which will create a git tag for the version, push\ngit commits and tags, and push the `.gem` file to\n[rubygems.org](https://rubygems.org).\n\nThis gem was briefly named **chainenum** before being renamed to **piecewise**.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at\nhttps://github.com/mwpastore/ruby-piecewise.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT\nLicense](https://opensource.org/licenses/MIT).\n\n[1]: https://en.wikipedia.org/wiki/Inversion_of_control\n[2]: https://ruby-doc.org/core-2.5.1/Enumerator.html\n[3]: https://en.wikipedia.org/wiki/Piecewise\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmwpastore%2Fruby-piecewise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmwpastore%2Fruby-piecewise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmwpastore%2Fruby-piecewise/lists"}