Iterators are a looping construct in the language Sather, a language developed at the International Computer Science Institute at Berkeley. Iterators are based on an idea in the language CLU. Iterators are designed to be safer than ordinary looping constructs, since creation, succession, and termination operations are defined where the iterator is defined, instead of depending on the code using the iterator to perform them. ## 11.3.1 Iterators in SatherThis example defines an iterator called fibonacci! : INT -- Declare an iterator which returns -- integer values a : INT := 1; -- Initialize a and b b : INT := 2; yield a; -- Return 1 the first time yield b; -- Return 2 the second time loop -- Loop forever c : INT := a + b; -- The next value is the sum -- of the two previous values yield c; -- Return that value a := b; -- Copy the values backwards, b := c; -- to set up the next iteration end; end; An iterator is used as a method is used in Java. A For example, here's some code that prints out the first ten Fibonacci numbers: loop -- Loop until quitting i : INT := range!(1, 10); -- An iterator that yields values -- from 1 to 10 before quitting f : INT := fibonacci!; -- Get the next Fibonacci number #OUT + i + ' ' + f + '\n'; -- Print it out end; This code uses two iterators, The first time through the loop, Next, the loop goes back to the beginning. This time, The The next time The iterator continues this way, with Iterators have many other uses. All arrays in Sather have an iterator called a:ARRAY{INT}:= | 43, 49649, 194 |; -- Create an array of -- integers with 3 -- elements total:INT := 0; loop total:= total + a.elt!; -- Sum up the elements end; Each time The summation loop can be done even more succinctly with the loop total := sum!(a.elt!); end; Each time This encapsulation prevents many common bugs that occur when loops are used. There is no possibility of failing to initialize the total to 0 or of accessing an invalid array element. By comparison, this is the equivalent Java code: int[] a = {43, 49649, 194 }; int total = 0; for(int i = 0; i < a.length; i++) total += a[i]; In this code, the The Sather ## 11.3.2 Implementing IteratorsTo implement iterators in the Java virtual machine, each iterator must be able to keep the state of its local variables, as well as its current position. One way to do this is to make each iterator its own class, using an instance of the iterator inside the loop. The class keeps track of the state of the iterator with an integer value, using the Here is one possible implementation of the .class fibonacci .field private a I ; Variables local to the .field private b I ; iterator .field private c I .field private state$ I ; Keeps the state .method public next ()I .throws QuitException aload_0 getfield fibonacci/state$ I ; Jump to the current state tableswitch 0 ; Initally, the state is 0 state0 state1 state2 state3 default: done ; Start here, since state$ is initialized to 0 state0: aload_0 ; Initialize a and b iconst_1 putfield fibonacci/a I aload_0 iconst_2 putfield fibonacci/b I aload_0 getfield fibonacci/a I ; Compute return value (a) aload_0 iconst_1 putfield fibonacci/state$ I ; Change to state 1 ireturn ; Yield a ; Pick up here when state$ is 1 state1: aload_0 getfield fibonacci/b I ; Compute return value (b) aload_0 iconst_2 putfield fibonacci/state$ I ; Change to state 2 ireturn ; Yield b ; Begin here when state$ is 2 state2: loop: aload_0 aload_0 ; Compute c=a+b getfield fibonacci/a I aload_0 getfield fibonacci/b I iadd putfield fibonacci/c I aload_0 getfield fibonacci/c I ; Get c to return aload_0 ; Change to state 3 iconst_3 putfield fibonacci/state$ I ireturn ; Yield c state3: aload_0 ; Shuffle b->a and c->b aload_0 getfield fibonacci/b I putfield fibonacci/a I aload_0 aload_0 getfield fibonacci/c I putfield fibonacci/b I goto loop ; Continue with the loop done: ; Default case: quit by new QuitException ; throwing an exception dup invokespecial QuitException/<init> ()V athrow .end method .method <init> ()V ; Basic constructor aload_0 invokespecial java/lang/Object/<init> ()V return .end method Wherever the Sather code says to The code begins with a The next time it is called, the The third call begins at On all subsequent calls, the All subsequent calls jump directly to An interesting thing to note is that this program doesn't correspond to any Java-language program, since you cannot write a ## 11.3.3 Iterators with ArgumentsThe range!(min, max:INT) : INT is -- Define the iterator range! -- It takes 2 arguments, min -- and max, which are both -- integers x : INT := min; -- Initialize the loop variable loop -- Loop until quitting if x > max -- Quit when x > max then quit end; yield x; -- Yield the next value x := x + 1; -- Increment x end; end; This iterator initializes a loop variable .class range .field x I .field state$ I .method public next (II)I aload_0 ; Jump to the current state getfield range/state$ I tableswitch 0 state0 state1 default: done state0: aload_0 ; Initialize x to min iload_1 putfield range/x I loop: aload_0 getfield range/x I iload_2 if_icmpgt done ; Break the loop if x>max aload_0 getfield range/x I ; Get return value (x) aload_0 iconst_1 putfield range/state$ I ; Change to state 1 ireturn ; Yield x state1: aload_0 ; Increment x aload_0 getfield range/x I iconst_1 iadd putfield range/x I goto loop ; Loop again done: new QuitException ; Throw a QuitException when dup ; done invokespecial QuitException/<init> ()V athrow .end method In this iterator, The loop first checks to see if the termination condition is met. If the test fails, then the iterator returns the latest value of When the termination test succeeds, then control passes to ## 11.3.4 Using IteratorsTo use the iterators implemented here, it is necessary to create an instance of each iterator contained in the program, calling loop -- Loop until quitting i : INT := range!(1, 10); -- An iterator that yields values -- from 1 to 10 before quitting f : INT := fibonacci!; -- Get the next fibonacci number #OUT + i + ' ' + f + '\n'; -- Print it out end; This code can be implemented in Oolong as .catch QuitException from loop to end using end .var 1 is fibonacci Lfibonacci; .var 2 is range Lrange; .var 3 is i I .var 4 is f I new fibonacci ; Create the iterators, dup ; storing them in invokespecial fibonacci/<init> ()V ; variables 1 and 2 astore_1 new range dup invokespecial range/<init> ()V astore_2 loop: ; Beginning of the loop aload_2 ; Call range! with 1, 10 iconst_1 bipush 10 invokevirtual range/next (II)I istore_3 ; Put that into i aload_1 ; Call fibonacci! invokevirtual fibonacci/next ()I istore 4 ; Store the result in f ;; Print out i and f (code omitted) goto loop ; Loop again end: pop ; Remove the exception ;; Control continues here after the loop The code begins by creating instances of the After initializing the iterators, the code goes into an infinite loop. It continues until an iterator terminates by throwing a In this code, the variables |

Programming for the Javaв„ў Virtual Machine

ISBN: 0201309726

EAN: 2147483647

EAN: 2147483647

Year: 1998

Pages: 158

Pages: 158

Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net