Socket
Book a DemoInstallSign in
Socket

piecewise

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

piecewise

0.3.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

Piecewise Rubygem

Piecewise Enumerables, Enumerators, and Lazy Enumerators for Ruby.

Do you ever find yourself doing something like:

even_keys = tuples.map { |k, v| k.upcase if v.even? }.compact

Or:

even_keys = tuples.select { |_, v| v.even? }.keys.map(&:upcase)

I've always found this pattern a little bit off-putting, especially when I have a big do..end block containing conditional logic and then a .compact tacked on to it. (Maybe I'm picky.) And sometimes nil is a valid value, so you have to introduce a new sentinel value to reject by. You can wrap the logic in an Enumerator.new { |yielder| .. } or move the logic to a method that returns an enumerator, but that can be a lot of boilerplate for a simple filter+map operation.

This simple gem monkeypatches Enumerable, Enumerator, and Enumerator::Lazy to add a #piecewise method that lets you do kind of an inline enumerator. The above example can be rewritten like this:

even_keys = tuples.piecewise { |yielder, (k, v)| yielder << k.upcase if v.even? }

I find this easier to read and grok. Perhaps you will too.

Installation

Add this line to your application's Gemfile:

gem 'piecewise'

And then execute:

$ bundle

Or install it yourself as:

$ gem install piecewise

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

This gem was briefly named chainenum before being renamed to piecewise.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mwpastore/ruby-piecewise.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 14 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.