Module: Ramda::Internal::CurriedMethod

Included in:
Function, List, Logic, Math, Object, Relation, String, Type
Defined in:
lib/ramda/internal/curried_method.rb

Overview

Curried Method

Instance Method Summary collapse

Instance Method Details

#__make_curry_proc__(proc, passed, arity) ⇒ Object

This hack resolved issue:

undefined method `__make_curry_proc__' for Ramda::Math:Module

Source:

https://github.com/jruby/jruby/issues/1523

rubocop:disable Metrics/MethodLength



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ramda/internal/curried_method.rb', line 66

def __make_curry_proc__(proc, passed, arity)
  is_lambda = proc.lambda?
  passed.freeze

  __send__((is_lambda ? :lambda : :proc)) do |*argv, &passed_proc|
    my_passed = passed + argv
    # original
    # if my_passed.length < arity
    # changed
    if my_passed.length < arity.abs - 1
      warn "#{caller[0]}: given block not used" unless passed_proc.nil?
      __make_curry_proc__(proc, my_passed, arity)
    else
      proc.call(*my_passed)
    end
  end
end

#curried_method(name, &block) ⇒ Object



7
8
9
# File 'lib/ramda/internal/curried_method.rb', line 7

def curried_method(name, &block)
  define_singleton_method(name, &curried_method_body(name, block.arity, &block))
end

#curried_method_body(name, arity, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ramda/internal/curried_method.rb', line 12

def curried_method_body(name, arity, &block)
  Ramda::Internal::FunctionWithArity.new.call(arity) do |*args|
    begin
      if args.include?(Ramda.__)
        replace_placeholder(args, &block).curry
      else
        args.empty? ? block : yield(*args)
      end
    rescue StandardError => e
      raise e, [name, e.exception].join(' -> '), e.backtrace
    end
  end.curry
end

#curried_method_v1(name, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/ramda/internal/curried_method.rb', line 47

def curried_method_v1(name, &block)
  define_singleton_method(name) do |*args|
    curried = block.curry
    # curried.define_singleton_method(:origin_arity) { block.arity }

    return curried if args.empty?

    curried.call(*args)
  end
end

#curried_method_v2(name, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ramda/internal/curried_method.rb', line 35

def curried_method_v2(name, &block)
  fn = Ramda::Internal::FunctionWithArity.new.call(block.arity) do |*args|
    if args.empty?
      block
    else
      yield(*args)
    end
  end

  define_singleton_method(name, &fn.curry)
end

#replace_placeholder(basic_args) ⇒ Object

rubocop:enable Metrics/MethodLength



27
28
29
30
31
32
33
# File 'lib/ramda/internal/curried_method.rb', line 27

def replace_placeholder(basic_args)
  Ramda::Internal::FunctionWithArity.new.call(basic_args.count(Ramda.__)) do |*new_args|
    cloned_args = basic_args.dup
    new_args.each { |arg| cloned_args[cloned_args.index(Ramda.__)] = arg }
    yield(*cloned_args)
  end
end