consume

consume, function

def process consume(
    available: number;
    requested: number)  : number

Returns, for each line, the amount taken from available to satisfy the cumulative requested demand when processed sequentially with scan.

Examples

table Items[Id] = with
  [| as Id, as Stock |]
  [| "a", 10 |]
  [| "b", 7 |]

table Orders = with
  [| as OId, as Qty |]
  [| "a", 1 |]
  [| "a", 1 |]
  [| "a", 2 |]
  [| "a", 1 |]
  [| "b", 5 |]
  [| "b", 10 |]
  [| "b", 25 |]

expect Orders.Id = Orders.OId

Orders.ServedFromStock = consume(Orders.Qty; Items.Stock) by Id scan auto

show table "Orders" with
  Orders.Id
  Orders.Qty
  Orders.ServedFromStock

Output:

Id Qty ServedFromStock
a 1 1
a 1 1
a 2 2
a 1 1
b 5 5
b 10 2
b 25 0

Remarks

consume does not mutate Items.Stock; it only returns the consumed quantities. As a process, consume must be called with scan or sort. It is typically used with scan to return a vector aligned with the input table.

Errors

Calling consume without scan or sort when assigning into the same table produces the usual self-aggregation error.

See also

User Contributed Notes
0 notes + add a note