Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created August 14, 2011 10:10
Show Gist options
  • Select an option

  • Save draftcode/1144765 to your computer and use it in GitHub Desktop.

Select an option

Save draftcode/1144765 to your computer and use it in GitHub Desktop.
List foldLeft := method(
context := Object clone prependProto(call sender)
if(call sender hasLocalSlot("self"),
context setSlot("self", call sender self)
)
initValue := call message evalArg(0)
eName := call message argAt(1) name
eResult := call message argAt(2) name
body := call message argAt(3)
context setSlot(eResult, initValue)
self foreach(value,
context setSlot(eName, getSlot("value"))
context setSlot(eResult, context doMessage(body))
)
context getSlot(eResult)
)
List myAverage := method(
total := self foldLeft(0, e, r,
if(e isKindOf(Number),
e + r,
Exception raise("Value is not a number.")
)
)
total / self size
)
list(1,2,3) myAverage println
list(1,"2",3) myAverage println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment