Skip to content

Instantly share code, notes, and snippets.

@phildow
Created February 7, 2013 23:27
Show Gist options
  • Save phildow/4735196 to your computer and use it in GitHub Desktop.
Save phildow/4735196 to your computer and use it in GitHub Desktop.
Implement map in ruby using a recursive function that takes a block
#!/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