Skip to content

Instantly share code, notes, and snippets.

@dce
Created June 22, 2011 21:17
Show Gist options
  • Save dce/1041240 to your computer and use it in GitHub Desktop.
Save dce/1041240 to your computer and use it in GitHub Desktop.
Even though it already has `reduce`...
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