title: KDB - Temporal function results become input for function on subsequent row
body: <p>I've been working with KDB to create temporal data from a set of inputs that represents segments functions of a model. The challenge I'm facing is that for a particular ID, there are be several segments where the last value of the temporal results from the first segment becomes an input (but not always) to the subsequent segment.</p>
  <pre><code>//Create sample table
  t:([id:`AAA`AAA`AAA`BBB`CCC`CCC;seg:1 2 3 1 1 2];aa: 1500 0n 0n 40 900 0N;bb:150 200 30 40 10 15;cc: .40 .25 .35 .5 .35 .45; Fname:`Fx`Fy`Fy`Fy`Fz`Fz);
  
  </code></pre>
  <p>The simple dummy functions below return 5 periods of data but actually each function can return several thousand data points</p>
  <pre><code>//Dummy functions to generate temporal data
  Fx:{[aa;bb;cc] (aa%bb)*(1-exp(neg cc*1+til 5))*100};
  Fy:{[aa;bb;cc] (aa%cc)*(1*(1-exp(neg cc*1+til 5)))};
  Fz:{[aa;bb;cc] (aa%bb)*(1-exp(neg cc*1+til 5))};
  </code></pre>
  <p>When I run the result for each function, we can see that where we are missing aa on a few segments.  The aa should be the prev (last t[result]) from the prior segment (i.e. <code>aa = 864.6647 for AAA seg 2</code> and <code>aa= 74.36035f for CCC seg 2)</code></p>
  <pre><code>show update result:first[Call_Function]'[aa;bb;cc] by Call_Function from t
  
  id  seg| aa   bb  cc   Fname result
  -------| ----------------------------------------------------------------
  AAA 1  | 1500 150 0.4  Fx    329.68   550.671  698.8058 798.1035 864.6647
  AAA 2  |      200 0.25 Fy
  AAA 3  |      30  0.35 Fy
  BBB 1  | 40   40  0.5  Fy    31.47755 50.56964 62.14959 69.17318 73.4332
  CCC 1  | 900  10  0.35 Fz    26.57807 45.30732 58.5056  67.80627 74.36035
  CCC 2  |      15  0.45 Fz
  </code></pre>
  <p>I've played around with trying to reference the prior segment <code>prev(last(t[result])</code> but the list result isn't referential. Similarly, I understand the <code>/ (over)</code> iterator would be useful but I've been unsuccessful implementing it.</p>
  <p>I thought about breaking this up into several steps (all the segment 1's then 2's then so on) and then append them all to a final table. Similarly, I'd like to keep track of each segments cumulative values and temporal counts(time) to pass to functions as limiters as well as so referencing the prev row successfully has multiple uses.</p>
  <p>Ultimately, once populated, I'll ungroup the table to get it into an output similar to below which I could then re-sort if needed.</p>
  <pre><code>q)show ungroup t  
  id  seg aa   bb  cc   Fname result  
  ------------------------------------
  AAA 1   1500 150 0.4  Fx    329.68
  AAA 1   1500 150 0.4  Fx    550.671
  AAA 1   1500 150 0.4  Fx    698.8058
  AAA 1   1500 150 0.4  Fx    798.1035
  AAA 1   1500 150 0.4  Fx    864.6647
  AAA 2        200 0.25 Fy
  AAA 2        200 0.25 Fy
  AAA 2        200 0.25 Fy
  AAA 2        200 0.25 Fy
  AAA 2        200 0.25 Fy
  AAA 3        30  0.35 Fy
  AAA 3        30  0.35 Fy
  AAA 3        30  0.35 Fy
  AAA 3        30  0.35 Fy
  AAA 3        30  0.35 Fy
  BBB 1   40   40  0.5  Fy    31.47755
  BBB 1   40   40  0.5  Fy    50.56964
  BBB 1   40   40  0.5  Fy    62.14959
  BBB 1   40   40  0.5  Fy    69.17318
  BBB 1   40   40  0.5  Fy    73.4332
  </code></pre>