vector

vector, keyword

The vector keyword declares a user-defined vector function when it appears after def.

‘def vector name«tables»(args) with’, vector function definition

A vector function binds table parameters between << and >>. Inside the function body, expect table declares the tables and dimensions required by the function.

Unlike pure and process functions, a vector function body may use table-prefixed vectors and may aggregate from one declared table into another.

The returned values must be vectors defined on the declared tables. A vector function may return a single vector or a tuple of vectors.

When returning a tuple, all returned vectors must belong to the same table.

Examples

def vector daysAfterFirstSale<<Products, Sales>>(
  Sales.Date : date) with
  expect table Products[sku]
  expect table Sales expect [sku]
  Products.FirstSale = first(Sales.Date) sort Sales.Date
  Sales.Age = Sales.Date - Products.FirstSale
  return Sales.Age

table Products[sku] = with
  [| as sku, as Label |]
  [| "shirt", "Shirt" |]
  [| "hat", "Hat" |]

table Sales = with
  [| as ProductSku, as Date, as Qty |]
  [| "shirt", date(2024, 1, 3), 2 |]
  [| "shirt", date(2024, 1, 8), 1 |]
  [| "hat", date(2024, 1, 5), 3 |]

keep where Sales.sku = Sales.ProductSku

Sales.DaysAfterFirst = daysAfterFirstSale<<Products, Sales>>(Sales.Date)

show table "Sales lines" with
  Sales.ProductSku
  Sales.Date
  Sales.Qty
  Sales.DaysAfterFirst

See also

User Contributed Notes
0 notes + add a note