Skip to content

mooreniemi/proc_compose

Repository files navigation


 ___|)___________________________________________________________
|___/____________________________________________________________
|__/|____________________________________________________________
|_/(|,\__________________________________________________________
|_\_|_/__________________________________________________________
|   |
| (_|
|
|________________________________________________________________
|__/___\_._______________________________________________________
|__\___|_._______________________________________________________
|_____/__________________________________________________________
|____/___________________________________________________________ ejm

Proc#compose

This gem does just one thing: it adds compose to Proc, with the alias *. Based on this experiment. It performs just a little worse than chaining map'd blocks, and currently does not preserve lambdas (they are coerced into Proc).

performance graph of Proc#compose

Installation

Add this line to your application's Gemfile:

gem 'proc_compose'

And then execute:

$ bundle

Or install it yourself as:

$ gem install proc_compose

Usage

First things first:

require 'proc_compose'

Now, given two Proc objects, say:

double = proc {|a| a * 2 }
triple = proc {|a| a * 3 }

We can compose them:

(double * triple).(2)
=> 12

So you can do things like this:

[1, 2, 3, 4, 5].map(&(double * triple))
=> [6, 12, 18, 24, 30]

Neat!

Alternatives

You can write your own version in Ruby, or use funkify. Though its performance is worse it is pure Ruby and you get some really nice other stuff like >= and |!

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mooreniemi/proc_compose. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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