Created
June 22, 2011 21:17
-
-
Save dce/1041240 to your computer and use it in GitHub Desktop.
Even though it already has `reduce`...
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
my_fold := method(list, coll, lambda, | |
if (list size == 0) then ( | |
return coll | |
) else ( | |
return fold(list rest, | |
lambda call(coll, list first), | |
lambda) | |
) | |
) | |
# Io> fold( | |
# ... list(1, 2, 3), | |
# ... 0, | |
# ... block(x, y, | |
# ... x + y | |
# ... ) | |
# ... ) | |
# ==> 6 | |
my_map := method(items, lambda, | |
my_fold( | |
items, | |
list(), | |
block(result, item, | |
result append(lambda call(item)) | |
) | |
) | |
) | |
# Io> map( | |
# ... list(1, 2, 3), | |
# ... block(x, | |
# ... x * x | |
# ... ) | |
# ... ) | |
# ==> list(1, 4, 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment