Created
November 20, 2011 02:31
-
-
Save fronx/1379722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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