Sha256: 63a5ad2c7d9e49e4cfd9ea54b406773b92670497c62fe244fa541c60e79dccc0

Contents?: true

Size: 785 Bytes

Versions: 20

Compression:

Stored size: 785 Bytes

Contents

class Proc

  # Returns a new proc that is the functional
  # composition of two procs, in order.
  #
  #   a = lambda { |x| x + 4 }
  #   b = lambda { |y| y / 2 }
  #
  #   a.compose(b).call(4)  #=> 6
  #   b.compose(a).call(4)  #=> 4
  #
  # CREDIT: Dave

  def compose(g)
    raise ArgumentError, "arity count mismatch" unless arity == g.arity
    lambda{ |*a| self[ *g[*a] ] }
  end

  # Operator for Proc#compose and Integer#times_collect/of.
  #
  #   a = lambda { |x| x + 4 }
  #   b = lambda { |y| y / 2 }
  #
  #   (a * b).call(4)  #=> 6
  #   (b * a).call(4)  #=> 4
  #
  # CREDIT: Dave

  def *(x)
    if Integer===x
      # collect times
      c = []
      x.times{|i| c << call(i)}
      c
    else
      # compose procs
      lambda{|*a| self[x[*a]]}
    end
  end

end

Version data entries

20 entries across 19 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/proc/compose.rb
facets-3.1.0 lib/core/facets/proc/compose.rb
facets-3.0.0 lib/core/facets/proc/compose.rb
facets-2.9.3 lib/core/facets/proc/compose.rb
facets-2.9.2 lib/core/facets/proc/compose.rb
facets-2.9.2 src/core/facets/proc/compose.rb
facets-2.9.1 lib/core/facets/proc/compose.rb
facets-2.9.0 lib/core/facets/proc/compose.rb
facets-2.9.0.pre.2 lib/core/facets/proc/compose.rb
facets-2.9.0.pre.1 lib/core/facets/proc/compose.rb
facets-2.8.4 lib/core/facets/proc/compose.rb
facets-2.8.3 lib/core/facets/proc/compose.rb
facets-2.8.2 lib/core/facets/proc/compose.rb
facets-2.8.1 lib/core/facets/proc/compose.rb
facets-2.8.0 lib/core/facets/proc/compose.rb
facets-2.7.0 lib/core/facets/proc/compose.rb
facets-2.6.0 lib/core/facets/proc/compose.rb
facets-2.5.0 lib/core/facets/proc/compose.rb
facets-2.5.1 lib/core/facets/proc/compose.rb
facets-2.5.2 lib/core/facets/proc/compose.rb