Module: DR::CoreExt::Proc
- Defined in:
- lib/dr/ruby_ext/core_modules.rb
Instance Method Summary collapse
-
#call_block(*args, **opts) ⇒ Object
Safely call our block, even if the user passed in something of a different arity (lambda case).
-
#compose(g) ⇒ Object
return self o g f.compose(g).(5,6).
-
#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).
-
#uncurry ⇒ Object
(->(x) {x+y}).uncurry.(2,3) #=> 5 (->(x,y) x+y).curry.uncurry.(2,3) #=>5.
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)
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/dr/ruby_ext/core_modules.rb', line 160 def call_block(*args,**opts) if arity >= 0 case arity when 0 call(**opts) else call(args[0...arity],**opts) end else call(*args,**opts) end end |
#compose(g) ⇒ Object
return self o g f.compose(g).(5,6)
184 185 186 187 188 |
# File 'lib/dr/ruby_ext/core_modules.rb', line 184 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)
176 177 178 179 180 |
# File 'lib/dr/ruby_ext/core_modules.rb', line 176 def rcurry(*args,&b) return ::Proc.new do |*a,&b| self.call(*a,*args,&b) end end |
#uncurry ⇒ Object
(->(x) {x+y}).uncurry.(2,3) #=> 5 (->(x,y) x+y).curry.uncurry.(2,3) #=>5
192 193 194 195 196 |
# File 'lib/dr/ruby_ext/core_modules.rb', line 192 def uncurry lambda do |*a| a.reduce(self) {|fun,v| fun.call(v)} end end |