Sha256: 0ecd9228492c3f58d4ed1fad37039444267a1ff7af9a66058c0be3370be589cd

Contents?: true

Size: 605 Bytes

Versions: 4

Compression:

Stored size: 605 Bytes

Contents

# -*- encoding : utf-8 -*-
module LambdaDriver::Composable
  # Returns new lambda which composed self and given function.
  # A composed proc called with args, executes `self.(g(*args)).
  #
  #   f = lamdba{|x| x.to_s }
  #   g = lambda{|y| y.length }
  #   h = f compose g
  #   h.(:hoge) # => 4
  #
  # This method is aliased as `<<`.
  #
  #   f << g # => f.compose(g)
  #
  def compose(g)
    lambda{|*args| self.to_proc.call(g.to_proc.call(*args)) }
  end

  # g compose self
  def >>(g)
    g.to_proc << self
  end

  def self.included(klass)
    klass.send(:alias_method, :<<, :compose)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lambda_driver-1.2.4 lib/lambda_driver/composable.rb
lambda_driver-1.2.3 lib/lambda_driver/composable.rb
lambda_driver-1.2.2 lib/lambda_driver/composable.rb
lambda_driver-1.2.1 lib/lambda_driver/composable.rb