/[fenfire]/fenfire/docs/pegboard/swamp_easier--benja/peg.rst
ViewVC logotype

Diff of /fenfire/docs/pegboard/swamp_easier--benja/peg.rst

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by benja, Mon Sep 22 02:08:25 2003 UTC revision 1.2 by benja, Mon Sep 22 06:04:43 2003 UTC
# Line 21  have a steep learning curve. Line 21  have a steep learning curve.
21  (Besides, easy-to-read and easy-to-use APIs are of course  (Besides, easy-to-read and easy-to-use APIs are of course
22  the right thing to have anyway.)  the right thing to have anyway.)
23    
24    Part of the original proposal in this PEG is split off
25    into ``swamp_easier_iteration--benja`` because mudyc
26    requested it.
27    
28    
29  .. Issues  .. Issues
30     ======     ======
# Line 162  in the graph.) Line 166  in the graph.)
166  ``Triples``  ``Triples``
167  -----------  -----------
168    
169  The iterator-like object, ``Triples``, shall have  For the API of the iterator-like object, ``Triples``,
170  the following API::  see ``swamp_easier_iteration--benja``.
   
     Object sub, pred, ob;  
   
 (These are ``null`` when the object hasn't been  
 initialized, i.e., ``next()`` hasn't been called yet.)  
   
     /** Advance to the next triple. */  
     void next();  
   
     /** Whether there are any more triples to iterate through. */  
     boolean hasNext();  
   
     /** Indicate that this <code>Triples</code> object won't be  
      *  used any more.  
      *  This shall only be called by the code that has requested  
      *  this object from <code>ConstGraph</code> (through  
      *  <code>.get()</code>). It's purpose is to tell the  
      *  <code>ConstGraph</code> that it can be re-used for the  
      *  next <code>get()</code>; <code>ConstGraph</code> can then  
      *  cache <code>Triples</code> objects, making life easier  
      *  for the garbage collector.  
      *  <p>  
      *  Calling this method is not obligatory. (If you don't,  
      *  this object will be garbage-collected normally.)  
      */  
     void free();  
   
     boolean loop() {  
         if(hasNext()) {  
             next();  
             return true;  
         } else {  
             free();  
             return false;  
         }  
     }  
   
 The purpose of ``loop()`` is to enable the common loop  
 pattern, ::  
   
     for(Triples t = graph.get(...); t.loop();) {  
         // ...  
     }  
   
 which would otherwise have to be written as::  
   
     Triples t;  
     for(t = graph.get(...); t.hasNext(); t.next()) {  
         // ...  
     }  
     t.free();  
   
 This isn't just harder to read, it also scopes ``t``  
 wrongly. With the ``loop()`` pattern, the scope of ``t``  
 is the body of the loop, which is exactly the code  
 executed before ``free()`` is called.  
171    
172    
173  ``Graph``  ``Graph``

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26