Sha256: 12ce59b7b6b6fb1e8325813a3b0c9502e52da283d7f8887f43c12cb8aa88a07b

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module Invokable
  # The {<<} and {>>} methods for right and left function composition.
  # 
  # This module is included in the {Invokable} module and the Proc and Method classes
  # when used with versions of Ruby earlier than 2.6 (when they were added).
  #
  # @note This module should not be used directly.
  module Compose
    # Return a proc that is the composition of this invokable and the given invokable.
    # The returned proc takes a variable number of arguments, calls invokable with
    # them then calls this proc with the result.
    #
    # @return [Proc]
    def <<(invokable)
      lambda do |*args|
        call(Invokable.coerce(invokable).call(*args))
      end
    end

    # Return a proc that is the composition of this invokable and the given invokable.
    # The returned proc takes a variable number of arguments, calls invokable with
    # them then calls this proc with the result.
    #
    # @return [Proc]
    def >>(invokable)
      lambda do |*args|
        Invokable.coerce(invokable).call(call(*args))
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
invokable-1.0.0 lib/invokable/compose.rb
invokable-0.7.2 lib/invokable/compose.rb
invokable-0.7.1 lib/invokable/compose.rb
invokable-0.7.0 lib/invokable/compose.rb