using System.Collections.Generic;
class Iterators
{
static void Use()
{
foreach (int i in GetSequence())
{
System.Console.WriteLine(i);
}
var instance = new Iterators();
foreach (int i in instance)
{
System.Console.WriteLine(i);
}
}
static IEnumerable<int> GetSequence()
{
int c = 1;
while (true)
{
c++;
// yield the next number
yield return c;
if (c < 5)
{
// yield the next number
yield return c * 10;
}
else
{
// end the iterator
yield break;
}
}
}
// Define an iterator for the current class
public IEnumerator<int> GetEnumerator()
{
for (int i = 23 ; i >= 0; i -= 5)
{
yield return i;
}
}
}
|
$set ilusing(System.Collections.Generic)
class-id Iterators.
method-id Use static.
perform varying i as binary-long thru GetSequence()
display i
end-perform
declare #instance = new Iterators()
perform varying i as binary-long thru #instance
display i
end-perform
end method.
iterator-id GetSequence static yielding retn as binary-long.
declare c as binary-long = 1
perform until exit
add 1 to c
*> yield the next number
set retn to c
goback
if c < 5
*> yield the next number
set retn to c * 10
goback
else
*> end the iterator
exit iterator
end-if
end-perform
end iterator.
*> Define an iterator for the current class
iterator-id self yielding retn as binary-long.
perform varying i as binary-long from 23 by -5 until i < 0
set retn to i
goback
end-perform
end iterator.
end class.
|
Imports System.Collections.Generic
class Iterators
Shared Sub Use()
For Each i In GetSequence()
System.Console.WriteLine(i)
Next
Dim instance = new Iterators()
For Each i In instance
System.Console.WriteLine(i)
Next
End Sub
Shared Iterator Function GetSequence() As IEnumerable(Of Integer)
Dim c as Integer = 1
while True
c = c + 1
' yield the next number
Yield c
If (c < 5)
' yield the next number
Yield c * 10
Else
' end the iterator
Exit Function
End If
End While
End Function
' Define an iterator for the current class
public Iterator Function GetEnumerator() As IEnumerator(Of Integer)
For i = 23 To 0 Step -5
Yield i
Next
End Function
End Class
|