0.0
No commit activity in last 3 years
No release in over 3 years
Proc#compose (*) C extension
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
>= 0
~> 10.0
~> 3.0
 Project Readme

 ___|)___________________________________________________________
|___/____________________________________________________________
|__/|____________________________________________________________
|_/(|,\__________________________________________________________
|_\_|_/__________________________________________________________
|   |
| (_|
|
|________________________________________________________________
|__/___\_._______________________________________________________
|__\___|_._______________________________________________________
|_____/__________________________________________________________
|____/___________________________________________________________ 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.