gdritter repos documents / master posts / multiwhile.md
master

Tree @master (Download .tar.gz)

multiwhile.md @masterview rendered · raw · history · blame

I read a paper recently that described a new looping construct. This
is admittedly pretty weird: we've pretty much settled on looping
constructs in imperative languages by now, and with a few exceptions
I don't see much change. You've got your `while` and (increasingly
rarely) your `do while` loop, which moves the test around:

~~~~
                     cond -> true
-----------------------------------------------------------
  while (cond) { body }   ->   body; while (cond) { body }

          cond -> false
-------------------------------------
  while (cond) { body }   ->   ()


                        cond -> true
------------------------------------------------------------------
  do { body } while (cond)   ->   body; do { body } while (cond)

              cond -> false
------------------------------------------
  do { body } while (cond)   ->   body
~~~~

We also have the nice scaffolding provided to us by `for` loops, which
are nice sugar for `while` loops:

~~~~
for (init, iter, cond) { body } => init; do { body; iter } while (cond)
~~~~

and we have flavors of `foreach` loops, which add awareness of some
underlying iterator protocol or collection type, which introduces an
element of binding

~~~~
foreach (x in coll) { body } => coll.iter(); while (coll.has_next()) { x = coll.next(); body }
~~~~