Skip to content

Instantly share code, notes, and snippets.

@takahisa
Created March 3, 2012 06:59
Show Gist options
  • Save takahisa/1964780 to your computer and use it in GitHub Desktop.
Save takahisa/1964780 to your computer and use it in GitHub Desktop.
Enumerator := Object clone do(
current := method(Exception raise("not implemented"))
moveNext := method(Exception raise("not implemented"))
reset := method(Exception raise("not implemented"))
)
Enumerable := Object clone do(
enumerator := method(Exception raise("not implemented"))
)
List setProtos(List protos push(Enumerable))
List enumerator := method(
enumerator := Enumerator clone
enumerator index := nil
enumerator current = method(self source at(self index))
enumerator reset = method(self index = 0)
enumerator moveNext = method(
if(self index == nil) then(
self index = 0
return(true)
) elseif(self index +1 >= self source size) then(
return(false)
) else(
index = index + 1
return(true)
)
)
enumerator source := self
enumerator
)
Enumerable each := method(
enumerator := self enumerator
argCount := call argCount
if ( argCount == 0 ) then(
Exception raise("missing arguments")
) elseif ( argCount > 2 ) then(
Exception raise("too many arguments")
) elseif ( argCount == 1 ) then(
body := call argAt(0)
while(enumerator moveNext,
enumerator current doMessage(body)
)
) elseif ( argCount == 2 ) then(
name := call argAt(0) name
body := call argAt(1)
while(enumerator moveNext,
call sender setSlot(name,enumerator current)
call sender doMessage(body,call sender)
)
)
Enumerable clone do(
enumerator := method(enumerator)
)
)
list(1,2,3) each(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment