反復子

このヘルプでは、COBOL、C#、VB.NET で使用できるさまざまな反復子について説明します。

COBOL の反復子

      $set  sourceformat(free)
class-id sillyCount.

    01 c binary-long value 1 static.

    method-id main static.
    01 i binary-long.
    procedure division using by value cmds as string occurs any.

        *> Use perform to loop over an iterator
        perform varying i through self::doCount()
            display i
        end-perform
    end method main.

    *> Create an iterator by defining a member as an iterator
    *> Using iterator-id instead of method-id
    iterator-id doCount static.
        *> The return value if the iterator is defined using the
        *> yielding verb
        procedure division yielding j as binary-long.
        perform until false
            add 1 to c
            move c to j
            *> Until the iterator is stopped, it will yield on
            *> a goback verb
            goback

            *> then start again directly after the goback on the next
            *> invocation of the iterator
            multiply c by 10 giving j
            if c less then 50
                goback
            else
                *> Stop iterator means the next goback will
                *> stop the iterator
                stop iterator
                goback
           end-if
        end-perform
        *> COBOL will implicitly stop iterator at the end of the iterator
        *> definition. In this example – the code never gets here
    end iterator.
end class.

C# の反復子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SillyStuff
{
    class  SillyCount
    {
        static int c = 1;

        static void Main(string[] args)
        {
            // Use foreach to loop through an iterator
            foreach(int i in doCount())
            {
                System.Console.WriteLine(i);
            }

        }

        // C# lacks specific syntax to mark a method as an iterator
        // you have to create a method with the correct concrete form
        // of the IEnumerable interface and then use the yield return
        // syntax inside the method.
        static IEnumerable<int> doCount()
        {

            while (true)
            {
                c++;
                // C# uses an implicit retun 'variable' rather than
                // the named one in COBOL. Yield return returns
                // the value as the current value of the iterator
                yield return  c;

                // then starts immediately after the
                // yield return on the next invocation of
                                // the iterator
                if (c < 50)
                {
                    yield return c * 50;
                }
                else
                {
                    // yield break marks the end of the iterator
                    yield break;
                }
            }
        }
    }
}

VB.NET の反復子

' VB lacks a direct syntax for creating iterators
' To create a lazy iterator as per the iterator-id in COBOL
' you must create a state machine yourself.

これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。