Sha256: 5d658aac2931668b3ede67778c877427e153cb9629aef60e1fe5fd3103a6cd3e

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'invokable/version'

# TODO: Add memoize, transducers?
module Invokable
  # If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
  #
  # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-call Proc#call
  # @return [Proc]
  def to_proc
    if respond_to?(:call)
      # TODO: Would method(:call) be more performant? We need benchmarks.
      Proc.new do |*args|
        call(*args)
      end
    else
      raise "Don't know how to convert #{self.inspect} into a Proc"
    end
  end

  # Return a curried proc. If the optional `arity` argument is given, it determines the number of arguments.
  # A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the
  # supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc
  # that takes the rest of arguments.
  #
  # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
  # @param arity [Number]
  # @return [Proc]
  def curry(arity = nil)
    to_proc.curry(arity)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
invokable-0.2.2 lib/invokable.rb