Created
February 7, 2013 23:27
-
-
Save phildow/4735196 to your computer and use it in GitHub Desktop.
Implement map in ruby using a recursive function that takes a block
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
#!/usr/bin/env ruby -w | |
def map(l,&f) | |
return [] if l.empty? | |
return [ f.call(l.first) ].concat map(l.slice(1..l.length), &f) | |
end | |
puts map [1,2,3,4] {|x| x+1} | |
puts map ['a','b','c','d'] {|x| "#{x} is a letter"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment