Exception handling vs. hasNext()

Kris Kowal kris.kowal at gmail.com
Sat Nov 17 00:28:41 PST 2007


Consider the iteration decorator pattern:

var i = 0;
var producer = {
   'next': function () {
       if (i < 10) {
           return i++;
       } else {
           throw new StopIteration();
       }
   }
};

var consumer = {
   'next': function () {
       return producer.next() * 2;
   },
};

while (true) {
   try {
       log(consumer.next();
   } catch (exception) {
       if (exception instanceof StopIteration) {
       } else {
           throw exception;
       }
   }
}

(or, hiding the details, conceptually the equivalent:)

var producer = range(10);
var consumer = new Iteration(function () {
   return producer.next() * 2;
});
consumer.forEach(log);

Both of these examples would log even numbers in the interval [0, 20).
 The consumer iteration does not have a terminal condition, but the
producer iteration's StopIteration exception passed up through the
decorator to the consumer.  This would work really well if you
recursively decorated or "pipelined" an iteration with a conenience
function like eachIter.

/* an indefinite iteration of the values in someArray that are at
indicies that are multiples of six */
var it = range(100).eachIter(function (n) n * 2).whereIter(function
(n) !(n % 3)).eachIter(function (i) someArray[i]).each(log)

In that particular example, the last each on the line would catch the
StopIteration thrown by Range.next when it reaches 100.  Naturally, no
100 item arrays are ever created.

Kris Kowal

On 11/16/07, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
> Hi Erik,
>
> Can you explain a little better by showing a code example?
>
> Thanks.
>
>
> On Nov 16, 2007 5:44 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:
> > One benefit of StopIteration is that code inside map/some/every etc
> > can just throw a StopIteration to stop the iteration.  The same thing
> > is harder to keep clean with a hasNext/next pattern.
> >
> >
> > On Nov 16, 2007 5:30 PM, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
> > > Which is better?
> > >
> > > var nodes : int;
> > > var widgetMap = Widget.instances; // a map.
> > > var it:Iterator<string> = widgetMap.getKeys();
> > >
> > >  -- this: --
> > >
> > > try {
> > >   widgetMap.get(it.next()).hide();
> > > }
> > > catch(Exception e) {
> > >   if(e instanceof StopIteration) {
> > >
> > >   }
> > > }
> > >
> > >
> > >  -- or this: --
> > >
> > > while(it.hasNext()) {
> > >   widgetMap.get(it.next()).hide();;
> > > }
> > >
> > > It might be the case that there might be some error
> > > I want to catch other than StopIteration. In that case, to be
> > > backwards-compatible and work across implementations, the developer
> > > must use conditional
> > > checks inside 1 catch block.
> > >
> > > A hasNext() would not prevent the developer from writing such code.
> > > Omitting hasNext() forces developers to use exception handling for
> > > non-exceptional condition.
> > >
> > > How does using try/catch for normal termination of the loop look?
> > >
> > > It looks like all exceptions are unchecked in ES4. correct?  (I cannot
> > > verify this to be true because
> > > I am unable to access the spec namespace on ecmascript.org.)
> > > _______________________________________________
> > > Es4-discuss mailing list
> > > Es4-discuss at mozilla.org
> > > https://mail.mozilla.org/listinfo/es4-discuss
> > >
> >
> >
> >
> > --
> > erik
> >
>
>
>
> --
> Programming is a collaborative art.
>
>
>
> --
> Programming is a collaborative art.
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>



More information about the Es4-discuss mailing list