Collection
Library of functions for the collection type.
Functions
Avg
Computes the average value of a collection of numbers.
Collection.Avg(collection: collection ...)
Parameters
collection
: The collection to compute the average on.
Returns
decimal
: The average value of the collection.
Collection.Avg(Collection.Build(1,2,3,4,5))
// Result:
// 3
Build
Builds a new collection.
Collection.Build(values: anything ...)
Parameters
values
: Values to add to the collection.
Returns
collection
: The new collection.
Collection.Build(1,2,3,4,5)
// Result:
// [1,2,3,4,5]
Contains
Tests if some value is contained in a collection.
Collection.Contains(collection: collection, value: anything)
Parameters
-
collection
: The collection. -
value
: The value to search for.
Returns
bool
: True if the value is contained in the collection, false otherwise.
Collection.Contains(Collection.Build(1,2,3), 2)
// Result:
// true
Count
Counts the number of elements in a collection.
Collection.Count(collection: collection)
Parameters
collection
: The collection to count elements.
Returns
long
: The number of elements in the collection.
Collection.Count(
Collection.Build(1,2,3)
)
// Result:
// 3
Distinct
Removes duplicate elements of a collection.
Collection.Distinct(collection: collection)
Parameters
collection
: The collection to remove duplicate elements from.
Returns
collection
: The new collection.
Collection.Distinct(
Collection.Build(1,2,2,3,3,3)
)
// Result:
// [1, 2, 3]
Empty
Creates an empty collection.
Collection.Empty(type: type)
Parameters
type
: The type of the elements of the empty collection.
Returns
collection
: The empty collection.
Collection.Empty(type int)
EquiJoin
Joins two collections with an equality condition.
Collection.EquiJoin(first: collection, second: collection, firstKey: function, secondKey: function)
Parameters
-
first
: The first collection to join. -
second
: The second collection to join. -
firstKey
: The join condition function, which receives a row with elements from the first collection and returns the key to perform the equality join condition on. -
secondKey
: The join condition function, which receives a row with elements from the second collection and returns the key to perform the equality join condition on.
Returns
collection
: A new collection built from joining both collections.
let
first = Collection.Build( {v: 1}, {v: 2}, {v: 3} ),
second = Collection.Build( {n: 1, name: "One"}, {n: 2, name: "Two"} )
in
Collection.EquiJoin(first, second, a -> a.v, b -> b.n)
// Result:
// [ { v: 1, n: 1, name: "One" }, { v: 2, n: 2, name: "Two" } ]
EquiJoin
is a more efficient and scalable than Join
, so use it when possible.
Exists
Tests whether a predicate holds for at least one element of a collection.
Collection.Exists(collection: collection, predicate: function)
Parameters
-
collection
: The collection. -
predicate
: The function predicate.
Returns
bool
: A boolean indicating whether the predicate holds for at least one element of the collection.
Collection.Exists(
Collection.Build(1,2,3),
v -> v >= 2
)
// Result:
// true
Explode
Moves elements of a nested collection into elements of the parent collection.
Collection.Explode(collection: collection, nested: collection)
Parameters
-
collection
: The collection to explode elements to. -
nested
: The collection to explode elements from.
Returns
collection
: The collection with elements from the nested collection exploded into elements of the parent collection.
// Suppose you have the following JSON data:
// [
// {
// "title": "Less than 2",
// "numbers": [{"v": 0}, {"v": 1}]
// },
// {
// "title": "More than 2",
// "numbers": [{"v": 3}, {"v": 4}]
// }
// ]
//
let data = Json.Read("example.json")
in
Collection.Explode(data, r -> r.numbers)
// Result:
// [
// {
// title: "Less than 2",
// numbers: [{v: 0}, {v: 1}],
// v: 0,
// },
// {
// title: "Less than 2",
// numbers: [{v: 0}, {v: 1}],
// v: 1,
// },
// {
// title: "More than 2",
// numbers: [{v: 3}, {v: 4}],
// v: 3
// },
// {
// title: "More than 2",
// numbers: [{v: 3}, {v: 4}],
// v: 4
// }
// ]
Filter
Selects all elements of a collection that satisfy a predicate.
Collection.Filter(collection: collection, predicate: function)
Parameters
-
collection
: The collection to filter. -
predicate
: The function predicate, which receives an element of a collection and must return true/false whether the element is to be selected or not.
Returns
collection
: The filtered collection.
Collection.Filter(
Collection.Build(1,2,3),
v -> v >= 2
)
// Result:
// [2,3]
FindFirst
Returns the first element of a collection that satisfies a predicate.
Collection.FindFirst(collection: collection, predicate: function)
Parameters
-
collection
: The collection. -
predicate
: The function predicate to apply to the elements.
Returns
- The first element in the collection that satisfies the predicate.
Collection.FindFirst(
Collection.Build(1,2,3),
v -> v >= 2
)
// Result:
// 2
FindLast
Returns the last element of a collection that satisfies a predicate.
Collection.FindLast(collection: collection, predicate: function)
Parameters
-
collection
: The collection. -
predicate
: The function predicate to apply to the elements.
Returns
- The last element in the collection that satisfies the predicate.
Collection.FindLast(
Collection.Build(1,2,3),
v -> v <= 2
)
// Result:
// 2
First
Selects the first element of a collection.
Collection.First(collection: collection)
Parameters
collection
: The collection to select the first element from.
Returns
number
: The first element in the collection.
Collection.First(Collection.Build(2, 3, 1))
// Result:
// 2
From
Builds a collection from the items of a list.
Collection.From(list: list)
Parameters
list
: The list to build the collection from.
Returns
collection
: The collection built from the list.
Collection.FromList(List.Build(1, 2, 3))
// Result:
// Collection.Build(1, 2, 3)
GroupBy
Partitions the input collection according to a key function.
Collection.GroupBy(collection: collection, predicate: function)
Parameters
-
collection
: The collection to partition. -
predicate
: The partition function, which receives an elements of the collection and returns the key to partition it by.
Returns
collection
: A collection of pairs, where the first element is the key and the second element is the collection of elements with that key.
Join
Joins two collections given a join condition, into a collection of records that includes the fields from both input collections.
Collection.Join(first: collection, second: collection, condition: function)
Parameters
-
first
: The first collection to join. -
second
: The second collection to join. -
condition
: The join condition function, which applies to a pair of elements, one from each collection, and returns true if they should be joined.
Returns
collection
: A new collection of records that includes the fields from both input collections.
let
first = Collection.Build( {v: 1}, {v: 2}, {v: 3} ),
second = Collection.Build( {n: 1, name: "One"}, {n: 2, name: "Two"} )
in
Collection.Join(first, second, (row1, row2) -> row1.v == row2.n)
// Result:
// [ { v: 1, n: 1, name: "One" }, { v: 2, n: 2, name: "Two" } ]
// items of a collection that isn't a collection of records, appear in the joined record
// with an automatically generated field name (here: _1).
let
first = Collection.Build(1,2,3),
second = Collection.Build( {n: 1, name: "One"}, {n: 2, name: "Two"} )
in
Collection.Join(first, second, (v, row2) -> v == row2.n)
// Result:
// [ { _1: 1, n: 1, name: "One" }, { _1: 2, n: 2, name: "Two" } ]
Last
Selects the last element of a collection.
Collection.Last(collection: collection)
Parameters
collection
: The collection to select the last element from.
Returns
- The last element in the collection.
Collection.Last(Collection.Build(3, 1, 2))
// Result:
// 2
Max
Finds the largest element in a collection.
Collection.Max(collection: collection)
Parameters
collection
: The collection to find the largest element from.
Returns
number
: The largest element in the collection.
Collection.Max(Collection.Build(2, 3, 1))
// Result:
// 3
Min
Finds the smallest element in a collection.
Collection.Min(collection: collection)
Parameters
collection
: The collection to find the smallest element from.
Returns
number
: The smallest element in the collection.
Collection.Min(Collection.Build(3, 1, 2))
// Result:
// 1
MkString
Concatenates all elements of a collection in a string using start, end, and separator strings.
Collection.MkString(collection: collection, start: optional string, sep: optional string, end: optional string)
Parameters
-
collection
: A collection. -
start
: The starting string. -
sep
: The separator string. -
end
: The ending string.
Returns
string
: A string containing all elements of the collection separated by the separator string.
Collection.MkString(
Collection.Build("a", "b", "c"),
start="(", sep=":", end=")")"
// Result:
// "(a:b:c)"
OrderBy
Orders this collection according to the key functions and orderings passed as parameters.
Collection.OrderBy(collection: collection, key: function ..., order: string ...)
Parameters
-
collection
: The collection to order. -
key
: The key ordering function, which receives an element of the collection and returns the key. -
order
: The order: "ASC" or "DESC".
Returns
collection
: The ordered collection.
Collection.OrderBy(movies, m -> m.country, "ASC", m -> m.budget, "DESC")
Sum
Sums all elements of a collection.
Collection.Sum(collection: collection)
Parameters
collection
: The collection to sum elements from.
Returns
number
: The sum of all elements in the collection.
Collection.Sum(Collection.Build(3, 1, 2))
// Result:
// 6
Take
Selects first N elements of a collection.
Collection.Take(collection: collection, n: int)
Parameters
-
collection
: The collection to select the first N elements from. -
n
: The number of elements to select from the collection.
Returns
collection
: The first N elements in the collection.
Collection.Take(Collection.Build(3, 1, 2), 3)
// Result:
// [3, 1]
Transform
Builds a new collection by applying a function to each element of a collection.
Collection.Transform(collection: collection, function: function)
Parameters
-
collection
: The collection to read. -
function
: The mapping function, which receives an element of the collection and returns a new element for the new collection.
Returns
collection
: The new collection.
Collection.Transform(
Collection.Build(1,2,3),
v -> v * 10
)
// Result:
// [10, 20, 30]
Union
Merge, i.e. union, the input collections into one.
Collection.Union(collection: collection ...)
Parameters
collection
: The collections to union.
Returns
collection
: The union of the collections.
Collection.Union([1, 2, 3], [4, 5, 6])
// Result:
// [1, 2, 3, 4, 5, 6]
Zip
Turns two collections into one by combining their corresponding elements in pairs until the shortest collection is exhausted.
Collection.Zip(collection1: collection, collection2: collection)
Parameters
-
collection1
: A collection. -
collection2
: A collection.
Returns
collection
: A collection of pairs of elements from the two collections.
Collection.Zip(
Collection.Build(1,2,3),
Collection.Build("a", "b", "c")
)
// Result:
// Collection.Build({1, "a"}, {2, "b"}, {3, "c"})