Sequence Functions
Higher-Order Functions
Higher-order functions are functions, which can take functions as parameters. E.g. a function applyFunc of type Function<Function<Date, Number>, Date> is a higher-order function expecting a function and a date as parameter. The following table lists some exemplary and valid applications of this function, whereas each of them yields to the same result:
Exemplary Expression | Description |
---|---|
let getDay = (d:Date) => d.day in applyFunc(getDay, Today) |
If there is a defined function, you might pass the function by its identifier. |
applyFunc((d:Date) => d.day, Today) |
You can pass the function also as (typed) lambda expression. |
applyFunc(d =>d.day, Today) |
In most cases, the type of the lambda expression can be inferred by MxL (e.g., by knowing the parameter type of the applyFunc). Hence, in this case, you can omit the parameter types of the lambda expression. |
applyFunc(day, Today) |
You can even further shorten the expression by using an implicit lambda as parameter. Since the applyFunc function knows the type of its first parameter, but discovers that day is neiter a function nor a known identifier, it will try to interpret the parameter expression as the method stub of a lambda and the unknown identifier as member of an implicit parameter: <implicit param> => <implicit param>.day . If this yields to the expected parameter type, the application of applyFunc is valid. |
Also note
- A function returing a boolean value is called predicate, e.g.
Function<Object, Boolean>
orFunction<Number,Number,Boolean>
. - A function which simply returns its only parameter is called the Identity function.
Standard Sequence Functions
An integral purpose of MxLis querying an underlying data model. Hence, based on the sequence of objects of an arbitrary type, MxL has to be able to apply certain filters, projections, aggregations, etc. onto this sequence. In the following, MxL’s standard sequence functions, which are inspired by Microsoft’s Standard Query Operators.
In the following sections, we assume that all functions are applied on Sequences of type Sequence<T>
, whereas T, U, K, V, and C represent arbitrary MxL types.
Query Functions
Name | Parameters | Returns | Description |
---|---|---|---|
select |
map: Function<T,U> |
Sequence<U> |
Applies the map-function to each element of the source sequence and returns a sequence containing the results of each individual application. |
selectMany |
map: Function<T,Sequence<U>> |
Sequence<U> |
Similar to the select-function, however, in selectMany, the map-function returns a sequence for each element. The concatenation of all sequences forms the result of the selectMany-function. |
where |
pred: Function<T,Boolean> |
Sequence<T> |
Filters the source list by the given predicate, i.e. all elements fulfilling the predicate remain in the sequence. |
groupby |
keySel: Function<T,K> groupMap: Function<Sequence<T>,V>? |
Map<K,V> |
Groups the elements of the source list by the keySel-Function and applies the (optional) groupMap-function (if not provided, it is the identity function) on the elements of each single group. |
orderby |
keySel: Function<T,Object>? descending: Boolean? |
Sequence<T> |
Sorts the source sequence by the (optional) keySel, whereas a natural order will be applied. The (optional) descending parameter determines, if the elements should be ordered ascending (“lowest first”) or descending (“highest first”). |
Quantifier Functions
Quantifier functions are sequence functions returning either true or false. Most of them are well-known in mathematics:
Name | Parameters | Returns | Description |
---|---|---|---|
any |
pred: Function<T,Boolean> |
Boolean |
Returns true , if at least one element of the source sequence fulfils the given predicate, otherwise false . |
all |
pred: Function<T,Boolean> |
Boolean |
Returns true , if each element of the source sequence fulfils the given predicate, otherwise false . |
none |
pred: Function<T,Boolean> |
Boolean |
Returns true , if no element of the source sequence fulfils the given predicate, otherwise false . |
contains |
element: T |
Boolean |
Returns true , if the given element is contained in the source sequence, otherwise false . |
isEmpty |
Boolean |
Returns true , if the source sequence has no elements, otherwise false . |
|
isNotEmpty |
Boolean |
Returns true , if the source sequence has at least one element, otherwise false . |
Set Functions
Set functions are functions producing a sequence (of same type as source sequence) based on the presence or absence of equivalent element within the same or another sequence.
Name | Parameters | Returns | Description |
---|---|---|---|
distinct |
Sequence<T> |
Removes all duplicates of the source sequence. | |
except |
other: Sequence<U> |
Sequence<T> |
Returns a sequence with elements which are contained in the source sequence, but not in the other one. |
intersect |
other: Sequence<U> |
Sequence<T> |
Returns a sequence with elements which are contained in the source sequence and in the other one. |
concat |
other: Sequence<U> |
Sequence<C> |
Concatenates the source sequence with the other one, i.e. the resulting sequence contains all elements of the source sequence, followed by all elements of the second one. The type C represents the most-specific type which T and U have in common. |
Element Functions
Element functions choose a certain element of the source sequence. If this element does not exist, the function throws an exception.
Name | Parameters | Returns | Description |
---|---|---|---|
first |
pred: Function<T,Boolean>? |
T |
Returns the first element of the source sequence (or the first element satisfying the predicate). If the source sequence is empty, this function throws an exception. |
last |
pred: Function<T,Boolean>? |
T |
Returns the last element of the source sequence (or the last element satisfying the predicate). If the source sequence is empty, this function throws an exception. |
single |
pred: Function<T,Boolean>? |
T |
Returns the only element of the source sequence (or the only element satisfying the predicate). If the sequence is either empty or has more than one element (or more than one element satisfying the predicate), this function throws an exception. |
Partitioning Functions
Element functions choose a certain element of the source sequence. If this element does not exist, the function throws an exception.
Name | Parameters | Returns | Description |
---|---|---|---|
rest |
Sequence<T> |
Returns the source sequence without the first element. | |
take |
n: Number |
Sequence<T> |
Returns a sequence with the first n elements of the source sequence. |
takeWhile |
pred: Function<T,Boolean> |
Sequence<T> |
Returns all elements of the source sequence until an element does not satisfy the predicate. |
skip |
n: Number |
Sequence<T> |
Returns a sequence without the first n elements of the source sequence. |
skipWhile |
pred: Function<T,Boolean> |
Sequence<T> |
Skips all elements of the source sequence as long as these elements satisfy the predicate, and returns the rest. |
Aggregation Functions
Aggregation functions fold up all elements of the source sequence to a single value.
Name | Parameters | Returns | Description |
---|---|---|---|
count |
pred: Function<T,Boolean>? |
Number |
Counts all elements of the source sequence (or counts the elements satisfying the predicate). |
ratio |
pred: Function<T,Boolean> |
Number |
Returns a number between 0 and 1 representing the ratio of elements fulfilling the given predicate. |
sum |
map: Function<T,Number>? |
Number |
Sums up all numbers of the source sequence. The optional map-function may select a numerical member of each element. |
average |
map: Function<T,Number>? |
Number |
Computes the average of all numbers of the source sequence. The optional map-function may select a numerical member of each element. |
max |
map: Function<T,Object>? |
Object |
Determines the maximal value of the source sequence. The optional map-function may select a criterium for the selection of the maximum value. |
min |
map: Function<T,Object>? |
Object |
Determines the minimal value of the source sequence. The optional map-function may select a criterium for the selection of the minimum value. |
argMax |
map: Function<T,Object>? |
T |
Determines the element with the maximal value of the source sequence, which is determined by the map-function. |
argMin |
map: Function<T,Object>? |
T |
Determines the element with the minimal value of the source sequence, which is determined by the map-function. |
aggregate |
map: Function<U,T,U> seed: U |
U |
Fold-operator, which aggregates the current sequence to a single value by the given func-function. The func-function is invoked for the result of its previous invokation and each of the source sequence’s elements. For the first invokation of the func-function, the seed value is used. The result of the last invokation of the func-function is the result of the aggregate-function. Example: aggregate((a,b) => a+b, 0) would emulate the sum-function, whereas aggregate((a,b) => a + 1, 0) would emulate the count-function |