An object containing an ordered sequence of elements.
This class is the object equivalent of List.
List
The function used to compute the default value for a listing element given its index.
The number of elements in this listing.
Tells if this listing is empty, that is, has zero elements.
The index of the last element in this listing (same as length - 1).
length - 1
Returns -1 for an empty list.
-1
Tells if this listing has no duplicate elements.
isUnique
Facts:
new Listing { 1; 2; 3 }.isDistinct !new Listing { 1; 2; 1 }.isDistinct
Removes duplicate elements from this listing, preserving the first occurrence.
unique
new Listing { 1; 2; 3 }.distinct == new Listing { 1; 2; 3 } new Listing { 1; 2; 1 }.distinct == new Listing { 1; 2 }
The first element in this listing.
Throws if this listing is empty.
new Listing { 1 ; 2 ; 3 }.first == 1 import("pkl:test").catch(() -> new Listing {}.first)
Same as first but returns null if this listing is empty.
first
null
The last element in this listing.
new Listing { 1 ; 2 ; 3 }.last == 3 import("pkl:test").catch(() -> new Listing {}.last)
Same as last but returns null if this listing is empty.
last
The single element in this listing.
Throws if this listing does not have exactly one element.
new Listing { 1 }.single == 1 import("pkl:test").catch(() -> new Listing {}.single) import("pkl:test").catch(() -> new Listing { 1 ; 2 ; 3 }.single)
Same as single but returns null if this collection is empty or has more than one element.
single
Returns the class of this.
this
Returns a string representation of this.
This method is used to convert the values of string interpolation expressions to strings.
Returns this |> transform if this is non-null, and null otherwise.
this |> transform
This method is the complement of the ?? operator and the equivalent of an Option type's map and flatMap methods.
??
Option
map
flatMap
Returns the element at index.
index
Returns null if index is outside the bounds of this listing.
new Listing { 3 ; 9 ; 6 }.getOrNull(0) == 3 new Listing { 3 ; 9 ; 6 }.getOrNull(1) == 9 new Listing { 3 ; 9 ; 6 }.getOrNull(2) == 6 new Listing { 3 ; 9 ; 6 }.getOrNull(-1) == null new Listing { 3 ; 9 ; 6 }.getOrNull(3) == null new Listing { 3 ; 9 ; 6 }.getOrNull(99) == null
Tells if this listing has no elements that are duplicates after applying selector.
selector
isUniqueBy
new Listing { "a"; "ab"; "abc" }.isDistinctBy((it) -> it.length) !new Listing { "a"; "ab"; "c" }.isDistinctBy((it) -> it.length)
Removes elements that are duplicates after applying selector from this listing, preserving the first occurrence.
uniqueBy
new Listing { "a"; "ab"; "abc" }.distinctBy((it) -> it.length) == new Listing { "a"; "ab"; "abc" } new Listing { "a"; "ab"; "c" }.distinctBy((it) -> it.length) == new Listing { "a"; "ab" }
Tells if predicate holds for every element of this listing.
predicate
Returns true for an empty listing.
true
Tells if predicate holds for at least one element of this listing.
Returns false for an empty listing.
false
Tests if element is contained in this listing.
element
new Listing { 1 ; 2 ; 3 }.contains(1) new Listing { 1 ; 2 ; 3 }.contains(2) new Listing { 1 ; 2 ; 3 }.contains(3) !new Listing { 1 ; 2 ; 3 }.contains(4)
Folds this listing in iteration order using operator, starting with initial.
operator
initial
The first parameter of operator is the zero-based index of the current element.
Converts the elements of this listing to strings and concatenates them inserting separator between elements.
separator
Converts this listing to a List.
Converts this listing to a Set.
Set
An object containing an ordered sequence of elements.