Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Created May 3, 2013 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/5515142 to your computer and use it in GitHub Desktop.
Save dsisnero/5515142 to your computer and use it in GitHub Desktop.
module Functional
module_function
def unary
->(a,*b){ a}
end
def binary
->(a,b,*c){ [a,b]}
end
def variadic(fn)
->(*args){
required_args = (0 < fn.arity) ? fn.arity : (fn.arity.abs) - 1
one_less_index = required_args -2
rest_index = one_less_index + 1
ordinary = (0 <= one_less_index) ? args[0..one_less_index] : []
rest = args[rest_index..-1]
args = required_args <= args.length ? ordinary + [rest]: []
fn.call(*args)
}
end
def apply_first(fn,first)
->(*args){
fn.call(*([first] + args))
}
end
def apply_last(fn,last)
->(*args){
fn.call( *( args + [last]) )
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment