Skip to content

Instantly share code, notes, and snippets.

@takahisa
Created March 3, 2012 07:35
Show Gist options
  • Save takahisa/1964902 to your computer and use it in GitHub Desktop.
Save takahisa/1964902 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 map := method(
enumerator := self enumerator
result := list()
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,
result push(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)
result push(call sender doMessage(body,call sender))
)
)
result
)
Enumerable select := method(
enumerator := self enumerator
result := list()
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,
t := enumerator current
t doMessage(body) ifTrue(result push(t))
)
) elseif ( argCount == 2 ) then(
name := call argAt(0) name
body := call argAt(1)
while(enumerator moveNext,
t := enumerator current
call sender setSlot(name,t)
call sender doMessage(body,call sender) ifTrue(result push(t))
)
)
result
)
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)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment