Module: DR::CoreExt::Proc

Defined in:
lib/dr/ruby_ext/core_modules.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)



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/dr/ruby_ext/core_modules.rb', line 241

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)



265
266
267
268
269
# File 'lib/dr/ruby_ext/core_modules.rb', line 265

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)



257
258
259
260
261
# File 'lib/dr/ruby_ext/core_modules.rb', line 257

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



273
274
275
276
277
# File 'lib/dr/ruby_ext/core_modules.rb', line 273

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