whichever

whichever(T.A : ‘a) 🡒 ‘a, aggregator

The aggregator returns one value for each group, if the group is not empty. If the group is empty, the default value for the data type is used.

Example:

table T = with
  [| as A, as B |]
  [| "hello", "a" |]
  [| "hello", "a" |]
  [| "hello", "b" |]
  [| "world", "b" |]
  [| "world", "c" |]

table G[gdim] = by T.B

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

The above code may result in the following table:

gdim whichever(T.A)
a hello
b hello
c

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

Whereas whichever is a completely deterministic aggregator (i.e., running the script twice over the same data yields the same result), the ordering of the data should not be relied upon. In other words, changing the script or upgrading to a newer version of Envision can alter the result of whichever.

This aggregator is intended as a faster alternative to first or last, for situations where an ordering of data elements is not essential by design.

See also

whichever, table creation

The contextual keyword whichever modifies the by table creation statement.

When the modifier whichever is present, table creation discards all lines for every group, except one arbitrarily chosen. The vectors of the originating table are copied into the newly created table.

Example:

table T = with
  [| as Code, as X |]
  [| "FR",    1    |]
  [| "US",    2    |]
  [| "UK",    3    |]
  [| "FR",    4    |]

table Countries[c] = whichever by T.Code

show table "Countries" a1b3 with c, Countries.X 

The resulting table may appear as shown below, where the value 4 corresponding to the "FR" group was arbitrarily dropped:

c X
FR 1
UK 3
US 2

In the above script, removing the whichever modifier prevents the script from compiling. Indeed, if there were several items in the "FR" group, Countries.X would be ambiguous, and thus Envision would not be able to determine which value to use.

In practice, the whichever by idiom is useful in situations where the choice of a duplicate has no effect, such as when all the lines are identical across all the vectors. Again, keep in mind that changing the script or upgrading to a newer version of Envision can result in different behaviour.

See also

User Contributed Notes
0 notes + add a note