Skip to content

Instantly share code, notes, and snippets.

@fronx
Created November 20, 2011 02:31
Show Gist options
  • Save fronx/1379722 to your computer and use it in GitHub Desktop.
Save fronx/1379722 to your computer and use it in GitHub Desktop.
class Array
def mapfn(&proc)
map do |item|
lambda do |*args|
proc.call(*([item] + args))
end
end
end
end
[1, 3, 4, 5].mapfn(&:+).
map do |adder|
adder.call(10)
end.
tap { |x| puts x.inspect } # => [11, 13, 14, 15]
stringify = Proc.new do |*args|
args.map(&:to_s).join
end
["A", "B"].mapfn(&stringify).
map do |prefixer|
prefixer.call(123, :foo)
end.
tap { |x| puts x.inspect } # => ["A123foo", "B123foo"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment