4.3BSD/usr/contrib/icon/samples/parallel.icn

#
#          P A R A L L E L   E V A L U A T I O N
#

#  This program illustrates an alternative to Icon's normal
#  last-in, first-out resumption of generators that results
#  in "cross-product" evaluation of arguments.  Here, parallel
#  evaluation is provided.

procedure main()
   every Parallel{|write,!"abcd",1 to 10}
   every Parallel{|write,octal(),star("abc")}
end

procedure star(s)
   suspend "" | (star(s) || !s)
end

procedure octal()
   suspend (0 to 3) || (0 to 7) || (0 to 7)
end

procedure Parallel(a)
   local i, x
   x := list(*a)
   repeat {
      every i := 1 to *a do
         x[i] := @a[i] | fail
      suspend Call(x)
      }
end

procedure Call(a)
   suspend case *a of {
      1 : a[1]()
      2 : a[1](a[2])
      3 : a[1](a[2],a[3])
      4 : a[1](a[2],a[3],a[4])
      5 : a[1](a[2],a[3],a[4],a[5])
      6 : a[1](a[2],a[3],a[4],a[5],a[6])
      7 : a[1](a[2],a[3],a[4],a[5],a[6],a[7])
      8 : a[1](a[2],a[3],a[4],a[5],a[6],a[7],a[8])
      9 : a[1](a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9])
      10 : a[1](a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10])
      default :  stop("Call : too many args.")
      }
end