<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>1. Conditionals and select (cond ? true : false) look like the next level. </span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><span>2. Use cases always come in handy. Here is one: It would be interesting if we could define realistic random number generators entirely in one update_counter operation. Linear congruential ones should fit with your proposal.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style:
 normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><span>3. An old favorite of mine: instead of crashing when a counter does not exist, initialize it to the submitted value. (Implement as an option or a new BIF.) Usage: profiling, in my case.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><span>Best,</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style:
 normal;"><span>Thomas</span></div><div><br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"> <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font size="2" face="Arial"> <b><span style="font-weight:bold;">From:</span></b> Ulf Wiger <ulf@feuerlabs.com><br> <b><span style="font-weight: bold;">To:</span></b> erlang questions <erlang-questions@erlang.org> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, August 7, 2013 1:39 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> [erlang-questions] update_counter on steroids<br> </font> </div> <div class="y_msg_container"><br>
<br>There was some discussion recently on teaching update_counter some new tricks.<br><br>Here is a suggestion:<br><br>Right now, the function takes (Tab, Key, UpOp | [UpOp]), where<br>  UpOp ::== {Pos, Incr} | {Pos, Incr, Threshold, WrapTo}<br><br>I'd like to save the result of an operation in a temporary variable.<br><br>An atomic reset, for example. (let's say counter c1 has the value 17):<br><br>ets:update_counter(counters, c1, [{2, 0, '$1'}, {2, {'-', '$1'}}]) -><br>  [17, 0]<br><br>(Increment by 0 means we read the existing value, just like today).<br><br>Why use this instead of simply ets:insert/2? Well, for one thing, we get the old value back, so it's an atomic read-reset.<br><br>Sum counter:<br><br>ets:update_counter(counters, c2, [{2,0,'$1'}, {3,0,'$2'}, {4, {'+', '$1', '$2'}}])<br><br>Wrap with parameterized threshold and reset value:<br><br>ets:update_counter(counters, c3, [{3,0,'$1'}, {4,0,'$2'}, {2, Incr, '$1',
 '$2'}])<br><br>This assumes the following changes:<br><br>- A number of temp variables, like in match specs.<br>- A 3-tuple  {Pos, Incr, SaveTo}, where SaveTo is a variable name (e.g. '$1')<br>- A 5-tuple  {Pos, Incr, Threshold, WrapTo, SaveTo}<br>- The possibility to use, wherever an integer() is expected<br>  VariableName | {UnaryOp, VariableName} | {BinaryOp, Var1, Var2}<br><br>We could also use this to e.g. store a value together with an increment operation. A reset with timestamp:<br><br>TS = timestamp(),  % in milliseconds<br>ets:update_counter(counters, c4, [{2, 0, '$1'}, {2, {'-', '$1'}, {3, 0, '$2'}, {3, {'-', TS, '$2'}}])<br><br>…a bit bizarre perhaps, but still.<br><br>Comments?<br><br>BR,<br>Ulf W<br><br><br>Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.<br>http://feuerlabs.com<br><br><br><br>_______________________________________________<br>erlang-questions mailing list<br><a
 ymailto="mailto:erlang-questions@erlang.org" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br><br><br></div> </div> </div> </blockquote></div>   </div></body></html>