反復子

反復子は、値の順序付けシーケンスを戻すコードのセクションです。反復子は、iterator-id を使用して、メンバーを反復子として定義することで作成できます。

iterator-specification

iterator-header procedure-division

iterator-header

iterator-id-signatureメソッドの署名を使用すると、特定の要素のヘッダー内で、渡すパラメーターおよび戻り項目を指定できます。この署名を使用する場合は、メソッド内の手続き部のヘッダーを省きます。 拡張メソッドと演算子のオーバーロード Extension methods enable you to add methods to existing types thereby providing additional functionality without the need to edit or recompile the code. access-modifier attribute-clause 汎用 - マネージ構文 Generic types and methods can be defined in COBOL with the USING phrase.

ここで、iterator-id 段落 GetEven を使用して、m-FibonacciArray に偶数を戻します。

       class-id Fibonacci.Class1 static.

       data division.
       working-storage section.
       01 m-FibonacciArray binary-long occurs any static.
       method-id FibEvens static.
       procedure division.
        set content of m-FibonacciArray to (1 2 3 5 8 13 21 34 55 89 144)

        display "Even numbers in the Fibonacci sequence:"         
        perform varying evenNumber as binary-long through self::GetEven
            display evenNumber
        end-perform
           goback.
       end method.

       iterator-id GetEven yielding res as binary-long static.

        perform varying i as binary-long through m-FibonacciArray
            if i b-and 1 = 0
                set res to i
                goback
            end-if
        end-perform
        stop iterator *> stops the iterator explicitly
       end iterator.  *> end of method stops the iterator implicitly 
       end class.

以下は同様の例ですが、self 構文を使用して現在のクラス (反復子があるクラス) から直接反復できるようにしたものです。

       class-id Fibonacci.Class2.
       method-id main static (args as string occurs any).
           try
               declare max as binary-long = binary-long::Parse(args[0])
               declare fibs = new Fibonacci(max)
               perform varying i as binary-long through fibs
                   display i
               end-perform
           catch
               display "Bad command line"
           end-try
       end method.
       end class.

       class-id Fibonacci.
       01 MaxNumber binary-long.

       method-id new (#max as binary-long).
           set MaxNumber to #max
       end method.

       iterator-id self yielding res as binary-long.
           set res to 1
           exit iterator
           declare frst as binary-long = 0
           declare second as binary-long = 1
           perform until exit
               compute res = frst + second
               if res > MaxNumber
                   stop iterator
               end-if
               move second to frst
               move res to second
               exit iterator
           end-perform
       end iterator.
       end class.

反復子のサンプルも参照してください。このサンプルは、Start > All Programs > Micro Focus Visual COBOL > Samples [COBOL for JVM (Windows) または$COBDIR/demo (UNIX)]. に用意されています。

その他の情報

反復子のヘッダーでパラメーターを指定する場合は、反復子の本体に手続き部ヘッダーを含めることはできません。

STOP ITERATOR 文は反復を停止します。

この文は、次の文が END ITERATOR の場合は必要ありません。