I am about to add some radical new constructions to Felix which should
improve it out of sight .. :)

First: a stream is a generator. Here's an example:

gen f() = { for var i in 0 upto 10 do yield i*2; done; println "Fin"; return 
99; }
var k = f;
for var j in 0 upto 12 do
  println$  #k;
done

An iterator is a generator that returns an option type, and which returns
Some thing a finite number of times, then None forever after:

gen g()={
  for var i in 0 upto 10 do yield Some (i*2); done; println "Fin"; return 
None[int];
}

With that in place, a comprehension is a finite data structure
which fills itself from an iterator: here's a list comprehension:


fun comprehension (f: &(1->opt[int])) = {
  fun aux (l:list[int]) = {
    var x = (*f)();
    return 
      match x with 
     | Some ?elt => aux (Cons (elt,l)) 
     | None => rev l
     endmatch
    ;
  }
  return aux Empty[int];
}

Note this thing is *eager* .. the iterator is lazy.

Of course given a list, we can easily make a generator that returns
all its members as Some x, then all Nones.

So now, we have an abstraction which will get rid of the evil for loop:

for var i in iterator do .. done

This calls the iterator, does a match, executes the body with
i set to the argument of Some, or exits the loop is None is returned.

Hence:

for var i = { var j = 0; while j < 10 do yield (Some j); ++j; done return None; 
} do .. done

loops with i=0 upto 9 (inclusive). Of course we can make a stock iterator for 
this.

And clearly it is easy enough to make a stock iterator that takes any list
and returns successive elements, so

for var i in list(1,2,3,42) do .. done

can be made to work, and it isn't specific to lists: all we need is a suitable
class and an instance for a particular data structure.

Now we just need the right syntax to make it look sexy.. :)

--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to