Selectors

Selectors in Envision are aggregators that select one of the input values and return it, with the properties outlined below.

There are five selectors in the Envision language:

Consult the documentation of each selector for more information.

Properties

First, the returned value is always taken either from the input, or from the default:

table T = with
  [| as A, as B |]
  [| "hello1", "a" |]
  [| "hello2", "a" |]
  [| "hello3", "b" |]
  [| "world1", "b" |]
  [| "world2", "c" |]

table G[gdim] = by T.B

where T.B != "c"
  show table "" a1b4 with
    gdim
    first(T.A) sort T.A
    group by gdim

The above code results in the following table:

gdim first(T.A)
a hello1
b hello3
c

(Notice that the value for c is the empty text value – the default value for text.)

default can be used to specify the default value to use when the group is empty. For example, first(T.A) sort T.A default "x" would result in the following table:

gdim first(T.A)
a hello1
b hello3
c x

Second, selectors can be called on a value of any type. In particular, they are the only aggregators that can take tuple arguments:

table T = with
  [| as A, as B, as C |]
  [| "a", "e",   4    |]
  [| "b", "f",   2    |]
  [| "c", "g",   1    |]
  [| "d", "h",   3    |]

x, y = first((T.A, T.B)) sort T.C

// c, g
show summary "" with x, y

where x, y = first((T.A, T.B)) sort T.C is equvalent to x = first(T.A) sort T.C and then y = first(T.B) sort T.C.

Third, selectors can be assigned to a named secondary dimension of a table:

table T[t] = with
  [| as N, as t |]
  [| 1,    "a"  |]
  [| 2,    "b"  |]
  [| 3,    "c"  |]
  [| 4,    "d"  |]
  [| 5,    "e"  |]

table U = with
  [| as N, as A |]
  [| 1,    "e"  |]
  [| 2,    "f"  |]
  [| 3,    "g"  |]
  [| 4,    "h"  |]
  [| 5,    "i"  |]

U.t = whichever(T.t) by T.N at U.N default fail

show table "" with U.t, U.A

The output table will be:

t A
a e
b f
c g
d h
e i

Here, whichever(T.t) by T.N at U.N selects the value of T.t that corresponds to each value of T.N, and assigns it to a line in U with the same value of U.N as T.N. Although the selector whichever is free to choose any value from the input group, in this case it chooses the only value of T.t that corresponds to each line of T; this is because vector T.N contains only distinct elements.

However, unambiguous selection is not a strict requirement of dimension assignment. The only thing that matters is that the expression on the right side of the assignment is statically known to always return a value that is valid for the dimension t. In the above example, this is true because:

  1. whichever is a selector, meaning that it always returns a value either from its input or the default.
  2. whichever is fed with T.t, whose values are always valid for that dimension (trivially).
  3. default fail, if triggered, does not return a value at all, so it does never return a value that is not valid for the dimension t either.

Remarks

Selectors cannot be used with scan.

See also

User Contributed Notes
0 notes + add a note