collections
The library collections
implements functions for basic manipulation of collections, such as lists and sets.
returns the size of the collection |
|
returns |
|
returns |
|
returns an element from the list |
|
returns a copy of the list |
|
returns a sublist of the list |
|
returns a copy of the list |
|
returns a list representation of the collection |
|
returns a set representation of the collection |
|
returns a string representation of the collection |
|
returns a copy of the collection |
|
returns a copy of the collection |
|
returns a copy of the list |
|
returns the union of collections |
|
returns a copy of the collection |
|
returns a copy of the collection |
|
parses an input string |
|
parses an input string |
size
Returns the size of the collection X
.
size(X)
Where:
-
X
is the collection whose size is to be returned.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:size(Y).
@output("result").
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 collectionX
.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:contains(Y, 2).
@output("result").
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 inX
.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X=c:containsAll(Y, [1, 2]).
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:get(Y, 1).
@output("result").
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 positionY
.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:set(Y, 1, 4).
@output("result").
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).
@library("c:", "collections").
alist([1, 2, 3, 4]).
result(X) :- alist(Y), X = c:subList(Y, 1, 3).
@output("result").
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)
@library("c:", "collections").
sortList([2,3,1]).
result(X) :- listToSort(Y), X = c:sort(Y).
@output("result").
result([1, 2, 3])
@library("c:", "collections").
sortList([2,3,1]).
result(X) :- listToSort(Y), X = c:sort(Y, "desc").
@output("result").
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.
@library("c:", "collections").
acollection({1, 2, 3}).
result(X) :- acollection(Y), X = c:asList(Y).
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 2, 3]).
result(X) :- alist(Y), X = c:asSet(Y).
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:join(Y, ", ", "[", "]").
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:add(Y, 4).
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 3, 2]).
result(X) :- alist(Y), X = c:remove(Y, 2).
@output("result").
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.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:removeAt(Y, 1).
@output("result").
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.
@library("c:", "collections").
alist([1, 2]).
result(X) :- alist(Y), X = c:union(Y, [3, 4]).
@output("result").
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 withX
.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:intersection(Y, [2, 3, 4]).
@output("result").
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 fromX
.
@library("c:", "collections").
alist([1, 2, 3]).
result(X) :- alist(Y), X = c:difference(Y, [2, 3]).
@output("result").
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.
@library("c:", "collections").
astring("[1,2,3,2]").
result(X) :- astring(Y), X = c:parse_set(Y).
@output("result").
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.
@library("c:", "collections").
astring("[1,2,3]").
result(X) :- astring(Y), X = c:parse_list(Y).
@output("result").
result([1, 2, 3])