CSc 520 - Principles of Programming Languages
31 : Control Structures -- Iterators

Christian Collberg

Department of Computer Science

University of Arizona

1 Iterators

for n := tree_nodes_in_inorder(T) do
   print n
end


Iterators in Java


2 Iterators in Java

3 Java 1.5 extended for-loop

4 Java 1.5 extended for-loop


Ruby iterators


5 Blocks

$flock = ["huey","dewey","louie"]

def isDuck?(name)
   for i in 0...$flock.length 
      if $flock[i] == name then
         return true
      end
   end
   return false
end

puts isDuck?("dewey"), isDuck?("donald")

6 Iterators

def isDuck?(name)
   $flock.find do |x|
      x == name
   end
end

puts isDuck?("dewey")
puts isDuck?("donald")

7 Yield

8 Yield...

def triplets()
   yield "huey"
   yield "dewey"
   yield "louie"
end

triplets() {|d| puts d}

triplets() do |d|
  puts d
end

9 Factorial

def fac(n)
   f = 1
   for i in 1..n 
     f *= i
     yield f
   end
end

fac(5) {|f| puts f}

10 Passing arguments

def fac(n)
   f = 1
   for i in 1..n 
     f *= i
     yield i,f
   end
end

fac(5) do |i,x|
   puts "#{i}! = #{x}"
end

11 Nesting iterators

fac(3) do |i,x| 
   fac(3) do |j,y|
      puts "#{i}! * #{j}! = #{x*y}"
   end
end

12 Scope

def sumfac(n)
   y = 0
   fac(n) do |i,x|
      y = y + x
   end
   return y
end

puts sumfac(5)

13 Implementing Array#find

def find(arr)
   for i in 0..arr.length 
      if yield arr[i] then return true end
   end
   return false
end

puts find($flock) {|x| x=="dewey"}
puts find($flock) {|x| x=="donald"}

14 Array#collect

$flock = ["huey","dewey","louie"]
$flock.each {|x| puts x}

puts $flock.collect {|x| x.length}
puts $flock.collect do |x| 
   "junior woodchuck, General " + x
end

15 Array#inject

x = $flock.inject("") do |elmt,total|
   total = elmt + " " + total 
end
puts x

x = $flock.inject() do |elmt,total|
   total = elmt + " " + total 
end
puts x


Icon Generators


16 Icon Generators

Procedures are really generators; they can return 0, 1, or a sequence of results. There are three cases

fail
The procedure fails and generates no value.
return e
The procedure generates one value, e.
suspend e
The procedure generates the value e, and makes itself ready to possibly generate more values.

procedure To(i,j)
   while i <= j do {
      suspend i
      i+:= 1
   }
end

17 Example

procedure To(i,j)
   while i <= j do {
      suspend i
      i+:= 1
   }
end

procedure main()
   every k := To(1,3) do
      write(k)
end

18 simple.icn

procedure P()
   suspend 3
   suspend 4
   suspend 5
end

procedure main()
   every k := P() do
      write(k)
end

19 simple.icn...

> setenv TRACE 100
> simple
             :       main()
simple.icn   :    8  | P()
simple.icn   :    2  | P suspended 3
3
simple.icn   :    9  | P resumed
simple.icn   :    3  | P suspended 4
4
simple.icn   :    9  | P resumed
simple.icn   :    4  | P suspended 5
5
simple.icn   :    9  | P resumed
simple.icn   :    5  | P failed
simple.icn   :   10  main failed


Iterators in CLU


20 CLU-Style Iterators



21 CLU-style Iterators...

22 CLU-style Iterators...



23 CLU-style Iterators...

24 CLU-style Iterators...



25 CLU-style Iterators...

26 CLU-style Iterators...



27 CLU-style Iterators...

28 CLU Iterators -- Example A

for i in from_to_by(first,last,step) do
   ...
end

29 CLU Iterators -- Example A...

from_to_by = iter(from,to,by:int) yields(int)
   i : int := from
   if by> 0 then
      while i <= to do
         yield i
         i +:= by
      end
   else
      while i >= to do
         yield i
         i +:= by
      end
   end

30 CLU Iterators -- Example B

for t: bin_tree in bin_tree$tree_gen(n) do
   bin_tree$print(t)
end

31 CLU Iterators -- Example B...

bin_tree = cluster ...
   node = record [left,right : bin_tree]
   rep  = variant [some : node, empty : null]
   ...
   tree_gen = iter (k : int) yields (cvt)
      if k=0 then
         yield red$make_empty(nil)
      else
         for i:int in from_to(1,k) do
            for l : bin_tree in tree_gen(i-1) do
              for r : bin_tree in tree_gen(k-i) do
                 yield rep$make_some(node${l,r})
              end
            end
      end
   end tree_gen
   ...
end


Implementing Iterators


32 Iterator Implementation



33 Iterator Implementation

34 Iterator Implementation...

iterator1

35 Iterator Implementation...

36 Nested Iterators



37 Nested Iterators...

38 Nested Iterators...

iterator2

39 Simpler Iterator Implementation



40 Simpler Iterator Implementation...



41 Simpler Iterator Implementation...




Summary


42 Readings and References

  1. Read Scott, pp. 278-284, 135CD-136CD.
  2. Russell R. Atkinson, Barbara H. Liskov, and Robert W. Scheifler: Aspects of Implementing CLU, Proceedings ACM National Conference, pp. 123-129, Dec, 1978.
  3. Murer, Omohundro, Szyperski: Sather Iters: Object-Oriented Iteration Abstraction: ftp://ftp.icsi.berkeley.edu/pub/techreports/1993/tr-93-045.ps.gz
  4. Todd A. Proebsting: Simple Translation of Goal-Directed Evaluation, PLDI'97, pp. 1-6. This paper describes an efficient implementation of Icon iterators.

43 Summary



Christian Collberg 2008-04-07