Maybe `it` notation (from `sortedByIt` fame) is also an abomination (or maybe 
only in combination with `do`), but you can do a pointer free, view type free, 
classic Nim (template impl at 
[lptabz.nim:587](https://github.com/c-blake/adix/blob/master/adix/lptabz.nim#L587)):
 
    
    
    import adix/lptabz
    var t = initLPTab[string, int]()
    for line in stdin.lines:
      t.edOrInIt line:  # edit clause
        it.inc; echo "had ", line
      do: # init clause
        it = 1; echo "new ", line
    
    
    Run

or 
    
    
    import adix/lptabz
    var t = initLPTab[string, int]()
    for line in stdin.lines:
      t.edOrInIt line,
                 edit = (it.inc; echo "had ", line),
                 init = (it = 1; echo "new ", line)
    
    
    Run

Note that some callers/call contexts might prefer to see the "init" stage 
appear first textually (since it does happen first temporally) and: 
    
    
    t.edOrInIt line,
               init = (it = 1; echo "new ", line),
               edit = (it.inc; echo "had ", line)
    
    
    Run

works just fine with the named parameter call.

The name could probably use some work. All the "its" in edIT, inIT, and the 
usual IT are a bit confusing/tongue twisting. `upsertIt` might be best of all 
since "upsert" has become a common term for this kind of operation, though 
"upPutIt" or "upSetIt" or some such might track the Nim stdlib naming 
conventions better.

All the above compile & work fine. The first, block call form could be both 
more clear in intent (named) and flexible in style (re-orderable) if my idea 
from [the do notation thread](https://forum.nim-lang.org/t/8259#53171) were 
adopted: 
    
    
    import adix/lptabz
    var t = initLPTab[string, int]()
    for line in stdin.lines:
      t.edOrInIt line, edit.do:
        it.inc; echo "had ", line
      init.do:
        it = 1; echo "new ", line
    
    
    Run

Reply via email to