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.
Build
Builds a new collection.
Collection.Build(values: anything ...)
Parameters
values
: Values to add to the collection.
Collection.Build(1,2,3,4,5) // A collection with 5 elements.
Contains
Tests if some value is contained in a collection.
Collection.Contains(list: collection, value: anything)
Parameters
list
: The collection.
value
: The value to search for.
Collection.Contains(Collection.Build(1,2,3), 2) // true
Count
Counts the number of elements in a collection.
Collection.Count(collection: collection)
Parameters
collection
: The collection to count elements.
Collection.Count(
Collection.Build(1,2,3)
) // 3
Distinct
Removes duplicate elements of a collection.
Collection.Distinct(collection: collection)
Parameters
collection
: The collection to remove duplicate elements from.
Empty
Creates an empty collection.
Collection.Empty(type: type)
Parameters
type
: The type of the elements of the empty collection.
Collection.Empty(type int) // Empty collection of 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.
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)
// Returns a collection similar to:
// [ { 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.
Collection.Exists(
Collection.Build(1,2,3),
v -> v >= 2
) // 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.
// 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}]
// }
// ]
//
// Then the following code:
let data = Json.Read("example.json")
in
Collection.Explode(data, r -> r.numbers)
// Returns a collection similar to:
// [
// {
// 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.
Collection.Filter(
Collection.Build(1,2,3),
v -> v >= 2
) // Collection.Build(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.
Collection.FindFirst(
Collection.Build(1,2,3),
v -> v >= 2
) // 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.
Collection.FindLast(
Collection.Build(1,2,3),
v -> v <= 2
) // 2
First
Selects the first element of a collection.
Collection.First(collection: collection)
Parameters
collection
: The collection to select the first element from.
Collection.First(Collection.Build(2, 3, 1)) // 2
From
Builds a collection from the items of a list.
Collection.From(list: list)
Parameters
list
: The list to build the collection from.
Collection.FromList(List.Build(1, 2, 3)) // 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.
Join
Joins two collections given a join condition.
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 receives a row with elements from both collections and returns true for the elements to join.
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, r -> r.v == r.n)
// Returns a collection similar to:
// [ { v: 1, n: 1, name: "One" }, { v: 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.
Collection.Last(Collection.Build(3, 1, 2)) // 2
Max
Finds the largest element in a collection.
Collection.Max(collection: collection)
Parameters
collection
: The collection to find the largest element from.
Collection.Max(Collection.Build(2, 3, 1)) // 3
Min
Finds the smallest element in a collection.
Collection.Min(collection: collection)
Parameters
collection
: The collection to find the smallest element from.
Collection.Min(Collection.Build(3, 1, 2)) // 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.
Collection.MkString(
Collection.Build("a", "b", "c"),
start="(", sep=":", end=")") // "(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".
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.
Collection.Sum(Collection.Build(3, 1, 2)) // 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.
Collection.Take(Collection.Build(3, 1, 2), 3) // [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.
Collection.Transform(
Collection.Build(1,2,3),
v -> v * 10
) // Collection.Build(10, 20, 30)
Union
Merge the input collections into one.
Collection.Union(collection: collection ...)
Parameters
collection
: The collections to union.
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.
Collection.Zip(
Collection.Build(1,2,3),
Collection.Build("a", "b", "c")
) // Collection.Build({1, "a"}, {2, "b"}, {3, "c"})