List Info

Thread: Proposed extras for the Iter module




Proposed extras for the Iter module
user name
2007-03-05 16:00:18
Hello,

Recently, I finally figured out just how great the Iter
module is
(it's almost lazy lists!). There were two things that I find
myself
using a lot which are not in the module though. Firstly, a
version of
chain that takes an iterator returning iterators as its
argument (as
opposed to multiple already existing iterators), so that the
whole
process is truly lazy (ichain or maybe iconcat?). Seconldy,
I often
want to iterate over strings, so an iteratorfactory for them
is really
practical. Below is simple code demonstrating the idea, if
there is
nothing against these I'd be happy to supply a proper patch
&
documentation.

Regards,
Marijn

------

registerIteratorFactory("string",
function(x){return typeof(x) ==
"string";}, function (string){
  var pos = 0;
  return {next: function() {
    if (pos < string.length) return
string.charAt(pos++);
    else throw StopIteration;
  }};
});

function ichain(iterators){
  var current = iter([]);
  return {next: function (){
    try {
      return current.next();
    } catch (e) {
      if (e != StopIteration) throw e;
      current = iter(iterators.next());
      return arguments.callee();
    }
  }};
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "MochiKit" group.
To post to this group, send email to mochikitgooglegroups.com
To unsubscribe from this group, send email to
mochikit-unsubscribegooglegroups.com
For more options, visit this group at http://
groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )