Module: DR::CoreExt::Proc

Defined in:
lib/drain/ruby_ext/core_ext.rb,
lib/drain/ruby_ext/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#call_block(*args, **opts) ⇒ Object

Safely call our block, even if the user passed in something of a different arity (lambda case)



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/drain/ruby_ext/core_ext.rb', line 110

def call_block(*args,**opts)
  if block.arity >= 0
    case block.arity
    when 0
      block.call(**opts)
    else
      block.call(args[0...block.arity],**opts)
    end
  else
    block.call(*args,**opts)
  end
end

#compose(g) ⇒ Object

return self o g f.compose(g).(5,6)



148
149
150
151
152
# File 'lib/drain/ruby_ext/core_ext.rb', line 148

def compose(g)
  lambda do |*a,&b|
    self.call(g.call(*a,&b))
  end
end

#rcurry(*args, &b) ⇒ Object

similar to curry, but pass the provided arguments on the right (a difference to Proc#curry is that we pass the argument directly, not via .call)



140
141
142
143
144
# File 'lib/drain/ruby_ext/core_ext.rb', line 140

def rcurry(*args,&b)
  return Proc.new do |*a,&b|
    self.call(*a,*args,&b)
  end
end

#uncurryObject

(->(x) {x+y}).uncurry.(2,3) #=> 5 (->(x,y) x+y).curry.uncurry.(2,3) #=>5



156
157
158
159
160
# File 'lib/drain/ruby_ext/core_ext.rb', line 156

def uncurry
  lambda do |*a|
    a.reduce(self) {|fun,v| fun.call(v)}
  end
end