Udostępnij za pośrednictwem


MethodHandles.CountedLoop Method

Definition

Overloads

CountedLoop(MethodHandle, MethodHandle, MethodHandle, MethodHandle)

Constructs a loop that counts over a range of numbers.

CountedLoop(MethodHandle, MethodHandle, MethodHandle)

Constructs a loop that runs a given number of iterations.

CountedLoop(MethodHandle, MethodHandle, MethodHandle, MethodHandle)

Constructs a loop that counts over a range of numbers.

[Android.Runtime.Register("countedLoop", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)]
public static Java.Lang.Invoke.MethodHandle? CountedLoop (Java.Lang.Invoke.MethodHandle? start, Java.Lang.Invoke.MethodHandle? end, Java.Lang.Invoke.MethodHandle? init, Java.Lang.Invoke.MethodHandle? body);
[<Android.Runtime.Register("countedLoop", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)>]
static member CountedLoop : Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle -> Java.Lang.Invoke.MethodHandle

Parameters

start
MethodHandle

a non-null handle to return the start value of the loop counter, which must be int. See above for other constraints.

end
MethodHandle

a non-null handle to return the end value of the loop counter (the loop will run to end-1). The result type must be int. See above for other constraints.

init
MethodHandle

optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.

body
MethodHandle

body of the loop, which may not be null. It controls the loop parameters and result type in the standard case (see above for details). It must accept its own return type (if non-void) plus an int parameter (for the counter), and may accept any number of additional types. See above for other constraints.

Returns

a method handle representing the loop.

Attributes

Remarks

Constructs a loop that counts over a range of numbers. This is a convenience wrapper for the #loop(MethodHandle[][]) generic loop combinator.

The loop counter i is a loop iteration variable of type int. The start and end handles determine the start (inclusive) and end (exclusive) values of the loop counter. The loop counter will be initialized to the int value returned from the evaluation of the start handle and run to the value returned from end (exclusively) with a step width of 1.

If the body handle returns a non-void type V, a leading loop iteration variable of that type is also present. This variable is initialized using the optional init handle, or to the #empty default value of type V if that handle is null.

In each iteration, the iteration variables are passed to an invocation of the body handle. A non-void value returned from the body (of type V) updates the leading iteration variable. The result of the loop handle execution will be the final V value of that variable (or void if there is no V variable).

The following rules hold for the argument handles:<ul> <li>The start and end handles must not be null, and must both return the common type int, referred to here as I in parameter type lists. <li>The body handle must not be null; its type must be of the form (V I A...)V, where V is non-void, or else (I A...)void. (In the void case, we assign the type void to the name V, and we will write (V I A...)V with the understanding that a void type V is quietly dropped from the parameter list, leaving (I A...)V.) <li>The parameter list (V I A...) of the body contributes to a list of types called the <em>internal parameter list</em>. It will constrain the parameter lists of the other loop parts. <li>As a special case, if the body contributes only V and I types, with no additional A types, then the internal parameter list is extended by the argument types A... of the end handle. <li>If the iteration variable types (V I) are dropped from the internal parameter list, the resulting shorter list (A...) is called the <em>external parameter list</em>. <li>The body return type V, if non-void, determines the type of an additional state variable of the loop. The body must both accept a leading parameter and return a value of this type V. <li>If init is non-null, it must have return type V. Its parameter list (of some form <c>(A*)</c>) must be effectively identical to the external parameter list (A...). <li>If init is null, the loop variable will be initialized to its #empty default value. <li>The parameter list of start (of some form (A*)) must be effectively identical to the external parameter list (A...). <li>Likewise, the parameter list of end must be effectively identical to the external parameter list. </ul>

The resulting loop handle's result type and parameter signature are determined as follows:<ul> <li>The loop handle's result type is the result type V of the body. <li>The loop handle's parameter types are the types (A...), from the external parameter list. </ul>

Here is pseudocode for the resulting loop handle. In the code, V/v represent the type / value of the second loop variable as well as the result type of the loop; and A.../a... represent arguments passed to the loop. <blockquote>

{@code
            int start(A...);
            int end(A...);
            V init(A...);
            V body(V, int, A...);
            V countedLoop(A... a...) {
              int e = end(a...);
              int s = start(a...);
              V v = init(a...);
              for (int i = s; i < e; ++i) {
                v = body(v, i, a...);
              }
              return v;
            }
            }

</blockquote>

Added in 9.

Java documentation for java.lang.invoke.MethodHandles.countedLoop(java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

CountedLoop(MethodHandle, MethodHandle, MethodHandle)

Constructs a loop that runs a given number of iterations.

[Android.Runtime.Register("countedLoop", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)]
public static Java.Lang.Invoke.MethodHandle? CountedLoop (Java.Lang.Invoke.MethodHandle? iterations, Java.Lang.Invoke.MethodHandle? init, Java.Lang.Invoke.MethodHandle? body);
[<Android.Runtime.Register("countedLoop", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)>]
static member CountedLoop : Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle -> Java.Lang.Invoke.MethodHandle

Parameters

iterations
MethodHandle

a non-null handle to return the number of iterations this loop should run. The handle's result type must be int. See above for other constraints.

init
MethodHandle

optional initializer, providing the initial value of the loop variable. May be null, implying a default initial value. See above for other constraints.

body
MethodHandle

body of the loop, which may not be null. It controls the loop parameters and result type in the standard case (see above for details). It must accept its own return type (if non-void) plus an int parameter (for the counter), and may accept any number of additional types. See above for other constraints.

Returns

a method handle representing the loop.

Attributes

Remarks

Constructs a loop that runs a given number of iterations. This is a convenience wrapper for the #loop(MethodHandle[][]) generic loop combinator.

The number of iterations is determined by the iterations handle evaluation result. The loop counter i is an extra loop iteration variable of type int. It will be initialized to 0 and incremented by 1 in each iteration.

If the body handle returns a non-void type V, a leading loop iteration variable of that type is also present. This variable is initialized using the optional init handle, or to the #empty default value of type V if that handle is null.

In each iteration, the iteration variables are passed to an invocation of the body handle. A non-void value returned from the body (of type V) updates the leading iteration variable. The result of the loop handle execution will be the final V value of that variable (or void if there is no V variable).

The following rules hold for the argument handles:<ul> <li>The iterations handle must not be null, and must return the type int, referred to here as I in parameter type lists. <li>The body handle must not be null; its type must be of the form (V I A...)V, where V is non-void, or else (I A...)void. (In the void case, we assign the type void to the name V, and we will write (V I A...)V with the understanding that a void type V is quietly dropped from the parameter list, leaving (I A...)V.) <li>The parameter list (V I A...) of the body contributes to a list of types called the <em>internal parameter list</em>. It will constrain the parameter lists of the other loop parts. <li>As a special case, if the body contributes only V and I types, with no additional A types, then the internal parameter list is extended by the argument types A... of the iterations handle. <li>If the iteration variable types (V I) are dropped from the internal parameter list, the resulting shorter list (A...) is called the <em>external parameter list</em>. <li>The body return type V, if non-void, determines the type of an additional state variable of the loop. The body must both accept a leading parameter and return a value of this type V. <li>If init is non-null, it must have return type V. Its parameter list (of some form <c>(A*)</c>) must be effectively identical to the external parameter list (A...). <li>If init is null, the loop variable will be initialized to its #empty default value. <li>The parameter list of iterations (of some form (A*)) must be effectively identical to the external parameter list (A...). </ul>

The resulting loop handle's result type and parameter signature are determined as follows:<ul> <li>The loop handle's result type is the result type V of the body. <li>The loop handle's parameter types are the types (A...), from the external parameter list. </ul>

Here is pseudocode for the resulting loop handle. In the code, V/v represent the type / value of the second loop variable as well as the result type of the loop; and A.../a... represent arguments passed to the loop. <blockquote>

{@code
            int iterations(A...);
            V init(A...);
            V body(V, int, A...);
            V countedLoop(A... a...) {
              int end = iterations(a...);
              V v = init(a...);
              for (int i = 0; i < end; ++i) {
                v = body(v, i, a...);
              }
              return v;
            }
            }

</blockquote>

Added in 9.

Java documentation for java.lang.invoke.MethodHandles.countedLoop(java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to