collections

The library collections implements functions for basic manipulation of collections, such as lists and sets.

size

returns the size of the collection

contains

returns true if Y is in X, false otherwise

containsAll

returns true if collection X contains all the elements of collection Y, false otherwise

get

returns an element from the list X at position Y

set

returns a copy of the list X, in which an element at position Y is replaced by Z

[_subList]

returns a sublist of the list X from the range (Y, Z)

sort

returns a copy of the list X with elements sorted in ascending order

asList

returns a list representation of the collection X

asSet

returns a set representation of the collection X

join

returns a string representation of the collection X with the prefix B, postfix C, and A as a separator

add

returns a copy of the collection X with the element Y added to the collection

remove

returns a copy of the collection X in which all occurrences of the element Y are removed

removeAt

returns a copy of the list X in which an element at position Y is removed

union

returns the union of collections X and Y

intersection

returns a copy of the collection X which contains elements from the collection Y

difference

returns a copy of the collection X which does not contain elements from the collection Y

parse_set

parses an input string X into a set of substrings that are comma separated in X

parse_list

parses an input string X into a list of substrings that are comma separated in X

size

Returns the size of the collection X.

size(X)

Where:

  • X is the collection whose size is to be returned.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:size(Y).
@output("result").
Expected results
result(3)

contains

Returns true if Y is in X, false otherwise.

contains(X, Y)

Where:

  • X is the collection to be checked.

  • Y is the element to check for in the collection X.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:contains(Y, 2).
@output("result").
Expected results
result(#T)

containsAll

Returns true if collection X contains all the elements of collection Y, false otherwise.

containsAll(X, Y)

Where:

  • X is the collection to be checked.

  • Y is the collection of elements to check for in X.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X=c:containsAll(Y, [1, 2]).
@output("result").
Expected results
result(#T)

get

Returns an element from the list X at position Y.

get(X, Y)

Where:

  • X is the list from which to retrieve the element.

  • Y is the position of the element to retrieve.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:get(Y, 1).
@output("result").
Expected results
result(2)

set

Returns a copy of the list X, in which an element at position Y is replaced by Z.

set(X, Y, Z)

Where:

  • X is the list to be modified.

  • Y is the position of the element to replace.

  • Z is the new element to place at position Y.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:set(Y, 1, 4).
@output("result").
Expected results
result([1, 4, 3])

subList

Returns a sublist of the list X from the range (Y, Z).

subList(X, Y, Z)

Where:

  • X is the list from which to retrieve the sublist.

  • Y is the starting position of the sublist (inclusive).

  • Z is the ending position of the sublist (exclusive).

Example
@library("c:", "collections").
alist([1, 2, 3, 4]).
result(X) :- alist(Y), X = c:subList(Y, 1, 3).
@output("result").
Expected results
result([2, 3])

sort

Returns a copy of the list X with elements sorted in ascending order.

sort(X, Y)

Where:

  • X is the list to be sorted.

  • Y (optional) type of sorting, can be either: "asc" (ascending), "desc" (descending)

Example
@library("c:", "collections").
sortList([2,3,1]).
result(X) :- listToSort(Y), X = c:sort(Y).
@output("result").
Expected results
result([1, 2, 3])
Example
@library("c:", "collections").
sortList([2,3,1]).
result(X) :- listToSort(Y), X = c:sort(Y, "desc").
@output("result").
Expected results
result([3, 2, 1])

asList

Returns a list representation of the collection X.

asList(X)

Where:

  • X is the collection to be converted to a list.

Example
@library("c:", "collections").
acollection({1, 2, 3}).
result(X) :- acollection(Y), X = c:asList(Y).
@output("result").
Expected results
result([1, 2, 3])

asSet

Returns a set representation of the collection X.

asSet(X)

Where:

  • X is the collection to be converted to a set.

Example
@library("c:", "collections").
alist([1, 2, 2, 3]).
result(X) :- alist(Y), X = c:asSet(Y).
@output("result").
Expected results
result({1, 2, 3})

join

Returns a string representation of the collection X with the prefix B, postfix C, and A as a separator between string-serialized elements from X. B and C are optional parameters.

join(X, A, B, C)

Where:

  • X is the collection to be joined into a string.

  • A is the separator between elements.

  • B is the prefix of the resulting string.

  • C is the postfix of the resulting string.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:join(Y, ", ", "[", "]").
@output("result").
Expected results
result("[1, 2, 3]")

add

Returns a copy of the collection X with the element Y added to the collection. In case X is a list, Y is added to the end of X.

add(X, Y)

Where:

  • X is the collection to be modified.

  • Y is the element to add to the collection.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:add(Y, 4).
@output("result").
Expected results
result([1, 2, 3, 4])

remove

Returns a copy of the collection X in which all occurrences of the element Y are removed.

remove(X, Y)

Where:

  • X is the collection to be modified.

  • Y is the element to remove from the collection.

Example
@library("c:", "collections").
alist([1, 2, 3, 2]).
result(X) :- alist(Y), X = c:remove(Y, 2).
@output("result").
Expected results
result([1, 3])

removeAt

Returns a copy of the list X in which an element at position Y is removed.

removeAt(X, Y)

Where:

  • X is the list to be modified.

  • Y is the position of the element to remove.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:removeAt(Y, 1).
@output("result").
Expected results
result([1, 3])

union

Returns the union of collections X and Y. In case both X and Y are lists, Y is appended to X.

union(X, Y)

Where:

  • X is the first collection.

  • Y is the second collection.

Example
@library("c:", "collections").
alist([1, 2]).
result(X) :- alist(Y), X = c:union(Y, [3, 4]).
@output("result").
Expected results
result([1, 2, 3, 4])

intersection

Returns a copy of the collection X which contains elements from the collection Y.

intersection(X, Y)

Where:

  • X is the first collection.

  • Y is the collection of elements to intersect with X.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:intersection(Y, [2, 3, 4]).
@output("result").
Expected results
result([2, 3])

difference

Returns a copy of the collection X which does not contain elements from the collection Y.

difference(X, Y)

Where:

  • X is the first collection.

  • Y is the collection of elements to remove from X.

Example
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:difference(Y, [2, 3]).
@output("result").
Expected results
result([1])

parse_set

Parses an input string X into a set of substrings that are comma separated in X.

parse_set(X)

Where:

  • X is the input string to be parsed into a set.

Example
@library("c:", "collections").
astring("[1,2,3,2]").
result(X) :- astring(Y), X = c:parse_set(Y).
@output("result").
Expected results
result({1, 2, 3})

parse_list

Parses an input string X into a list of substrings that are comma separated in X.

parse_list(X)

Where:

  • X is the input string to be parsed into a list.

Example
@library("c:", "collections").
astring("[1,2,3]").
result(X) :- astring(Y), X = c:parse_list(Y).
@output("result").
Expected results
result([1, 2, 3])