I have committed to SVN a small  (sequential) implementation of lazy, 
functional, streams in X10. Streams can be possibly infinite, and are 
materialized on demand, and support multiple readers.

Code is at courses/trunk/pppp11/, see package pppp.lib.lazystream.

The code uses thunks (expressions of type ()=>T, for some T) to express 
delayed evaluation, and operator overloading to express computations on 
streams. It uses static variables and methods to create recursive 
(potentially infinite) data-structures.

Note: In X10 2.2, a local variable cannot be initialized with a value 
that refers to the variable being initialized. Similarly there are 
difficulties when trying to recursively initialize an instance field 
because of escaping this. However, static variables and methods can be 
used to construct recursive functional data-structures.

For example, the hamming sequence is implemented thus:

/**
      *  X = 1:omerge(2*X,omerge(3*X,5*X))
      */
     public static def h():Stream[Int]=x;
     static val x= new StreamImp[Int](1, ()=> (2*h())^(3*h())^(5*h()));

Here * is a pointwise multiplication operator on streams (defined for 
types T s.t. T <: Arithmetic[T]), and ^ represents ordered merge 
(defined for types T s.t. T <: Ordered[T]).

The sieve of Eratosthenes is expressed thus. Here % is an operator on 
streams that takes a (T)=>Boolean filter and passes only those elements 
which do not satisfy the filter.

   static def primes()=primes(Utils.Gen(2 as Int, (n:Int)=>n+1));

     static def multiple(p:Int)=(x:Int):Boolean=>x%p==0;

     static def primes(r:Stream[Int]):Stream[Int] = {
         val p = r.x(), s=r.y();
         new StreamImp[Int](p, ()=>primes(s % multiple(p)))
     }

Enjoy.

Vijay






------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to