Useful alternative control structures?

后端 未结 28 981
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

28条回答
  •  猫巷女王i
    2021-01-30 02:48

    This is similar to the response by @Paul Keister.

    (mumble, mumble) years ago, the application I was working on had lots of variations of so-called control-break processing -- all that logic that goes into breaking sorted rows of data into groups and subgroups with headers and footers. As the application was written in LISP, we had captured the common idioms in a macro called WITH-CONTROL-BREAKS. If I were to transpose that syntax into the ever-popular squiggly form, it might look something like this:

    withControlBreaks (x, y, z : readSortedRecords()) {
      first (x) :     { emitHeader(x); subcount = 0; }
      first (x, y) :  { emitSubheader(x, y); zTotal = 0; }
      all (x, y, z) : { emitDetail(x, y, z); ztotal += z; }
      last (x, y) :   { emitSubfooter(x, y, zTotal); ++subCount; }
      last (x) :      { emitFooter(x, subcount); }
    }
    

    In this modern era, with widespread SQL, XQuery, LINQ and so on, this need does not seem to arise as much as it used to. But from time to time, I wish that I had that control structure at hand.

提交回复
热议问题