Created
August 14, 2011 10:10
-
-
Save draftcode/1144765 to your computer and use it in GitHub Desktop.
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
| 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