Issue #6284 has been updated by Paul Mucur.


Alexander Moore-Niemi wrote:
> Paul: what's the performance of your `compose`? If I have time later I can use https://github.com/mooreniemi/graph-function to try and see.

I ran the following benchmark with benchmark-ips:

~~~
require 'benchmark/ips'

double = proc { |x| x * 2 }
quadruple = proc { |x| x * 4 }
octuple = double * quadruple
inline_octuple = proc { |x| x * 2 * 4 }
nested_octuple = proc { |x| quadruple.call(double.call(x)) }

numbers = [1, 2, 3, 4, 5]

Benchmark.ips do |x|
  x.report('composing procs') do
    numbers.map(&octuple)
  end

  x.report('chaining procs') do
    numbers.map(&double).map(&quadruple)
  end

  x.report('single proc') do
    numbers.map(&inline_octuple)
  end

  x.report('nested proc') do
    numbers.map(&nested_octuple)
  end

  x.compare!
end
~~~

And also see a performance drop with composition over chaining multiple `map`s:

~~~
Warming up --------------------------------------
     composing procs    27.822k i/100ms
      chaining procs    32.096k i/100ms
         single proc    49.021k i/100ms
         nested proc    27.337k i/100ms
Calculating -------------------------------------
     composing procs    341.874k ( 0.5%) i/s -      1.725M in   5.045764s
      chaining procs    389.031k ( 0.7%) i/s -      1.958M in   5.032912s
         single proc    666.544k ( 0.6%) i/s -      3.333M in   5.001266s
         nested proc    321.919k ( 0.8%) i/s -      1.613M in   5.010562s

Comparison:
         single proc:   666543.8 i/s
      chaining procs:   389031.4 i/s - 1.71x  slower
     composing procs:   341873.8 i/s - 1.95x  slower
         nested proc:   321919.1 i/s - 2.07x  slower
~~~

It might be interesting to look at object allocations as we effectively create a nested `Proc` which might account for the slow down.

----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-60864

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=begin
It would be nice to be able to compose procs like functions in functional programming languages:

    to_camel = :capitalize.to_proc
    add_header = ->val {"Title: " + val}

    format_as_title = add_header << to_camel << :strip

instead of:

    format_as_title = lambda {|val| "Title: " + val.strip.capitalize }


It's pretty easy to implement in pure ruby:

  class Proc
    def << block
      proc { |*args| self.call( block.to_proc.call(*args) ) }
    end
  end
=end

---Files--------------------------------
0001-proc.c-Implement-Proc-for-Proc-composition.patch (3.65 KB)
0002-proc.c-Implement-Method-for-Method-composition.patch (2.67 KB)
0003-proc.c-Support-any-callable-when-composing-Procs.patch (3.97 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>