Created
March 25, 2013 02:16
-
-
Save d11wtq/5234515 to your computer and use it in GitHub Desktop.
Lists, and the concept of "head" and "tail", coupled with recursion are at the core of functional programming. This demonstrates how you can do it in Ruby, without ever writing a loop.
This file contains hidden or 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
# foldl() is fundamental. With a fold, you can do everything else. | |
def foldl(list, acc, &fn) | |
if list == [] # base case, return the accumulator | |
acc | |
else | |
head, *tail = list | |
foldl(tail, fn.call(acc, head), &fn) #recurse on the remainder | |
end | |
end | |
# now we can write reverse() | |
def reverse(list) | |
foldl(list, []) {|acc, v| [v, *acc]} | |
end | |
# and we can write foldr (reverse foldl) | |
def foldr(list, acc, &fn) | |
foldl(reverse(list), acc, &fn) | |
end | |
# we can write map | |
def map(list, &fn) | |
reverse(foldl(list, []) {|acc, v| [fn.call(v), *acc]}) | |
end | |
# we can write foreach | |
def foreach(list, &fn) | |
if list != [] | |
head, *tail = list | |
fn.call(head) | |
foreach(tail, &fn) | |
end | |
end | |
# etc | |
foldl([1, 2, 3], 0) {|acc, n| acc + n} # => 6 | |
reverse([1, 2, 3]) # => [3, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pda wow, that's interesting. Presumably that will find its way into the default set of compile options then.