Sha256: a2536c57b425a3cca47a1e5453d88da8c64f96c6908004e7065c0f3c6d6f11fe

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

# -*- encoding : utf-8 -*-
module LambdaDriver::Flipable
  # Returns function whose parameter order spawed 1st for 2nd.
  # A result of filped fuction is curried by Proc#curry.
  #
  #   f = lambda{|x, y, z| [x, y, z]}
  #   h = f.flip
  #   h.(:a).(:b).(:c) # => [:b, :a, :c]
  #
  # If arguments is var-args, pass explicitly arity to curring.
  #
  #   p = Proc.new{|*args| args.inspect }
  #   p.flip(3).call(:a).(:b).(:c)      # => "[:b, :a, :c]"
  #   p.flip(4).call(:a).(:b).(:c).(:d) # => "[:b, :a, :c, :d]"
  #
  # If arity is 0 or 1, flip returns itself.
  #
  # This method is aliased as `~@`.
  #
  #   ~f # => f.filp
  #
  def flip(arity = nil)
    f = self.to_proc
    return self if (0..1).include?(f.arity)
    return self if f.arity == -1 && arity.nil?

    curried = f.curry(arity)
    lambda{|x|
      lambda{|y|
        g = curried[y]
        (g.respond_to? :call) ? g[x] : g
      }
    }
  end

  def self.included(klass)
    klass.send(:alias_method, :~@, :flip)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lambda_driver-1.3.0 lib/lambda_driver/flipable.rb