Package org.protelis.lang.datatype
Interface Tuple
-
- All Superinterfaces:
java.lang.Comparable<Tuple>,java.lang.Iterable<java.lang.Object>,java.io.Serializable
- All Known Implementing Classes:
ArrayTupleImpl
public interface Tuple extends java.lang.Iterable<java.lang.Object>, java.io.Serializable, java.lang.Comparable<Tuple>
Implementation of mathematical tuples as indexed objects T = [element0, element1, element2 ...] .
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Tupleappend(java.lang.Object element)Add an element to the end of a tuple.booleancontains(java.lang.Object element)Tests whether the tuple has an element equal to its argument.booleancontainsAll(java.lang.Iterable<?> element)Tests whether the tuple has an element equal to its argument.Tuplefilter(java.util.function.Predicate<java.lang.Object> fun)Given a tuple and aPredicate, returns a new tuple containing only the elements that match the predicate.Tuplefilter(ExecutionContext ctx, FunctionDefinition fun)Given a tuple and a predicate, returns a new tuple containing only the elements that match the predicate.TupleflatMap(java.util.function.Function<java.lang.Object,Tuple> mapper)Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.default TupleflatMap(ExecutionContext context, FunctionDefinition mapper)Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.java.lang.Objectfold(java.lang.Object initial, java.util.function.BinaryOperator<java.lang.Object> fun)Performs a reduction operation, that maps a tuple to a single value.default java.lang.Objectfold(ExecutionContext ctx, java.lang.Object initial, FunctionDefinition fun)Performs a reduction operation, that maps a tuple to a single value.java.lang.Objectget(int i)Retrieve the object at one of a tuple's indices.java.lang.Objecthead()intindexof(java.lang.Object element)Searches for an element in a tuple, returning its index if found.Tupleinsert(int i, java.lang.Object element)Add an element to an arbitrary location within a tuple, moving all elements after it down.Tupleintersection(Tuple t)Set arithmetic: compute intersection of set of tuple elements with set of elements of another tuple.booleanisEmpty()Tuplemap(java.util.function.Function<java.lang.Object,java.lang.Object> fun)Maps a tuple to another same-length tuple by applying a function to each element.Tuplemap(ExecutionContext ctx, FunctionDefinition fun)Maps a tuple to another same-length tuple by applying a function to each element.java.lang.Objectmax(java.lang.Object def)TuplemergeAfter(Tuple tuple)Append elements of another tuple to the end of this tuple.java.lang.Objectmin(java.lang.Object def)Tupleprepend(java.lang.Object element)Add an element to the start of a tuple.java.lang.Objectreduce(java.lang.Object defVal, java.util.function.BinaryOperator<java.lang.Object> fun)Performs a reduction operation, that maps a tuple to a single value.default java.lang.Objectreduce(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)Performs a reduction operation, that maps a tuple to a single value.Tupleset(int i, java.lang.Object element)Replace an element in a tuple.intsize()Tuplesort()Tuplesubtract(Tuple t)Set arithmetic: compute which elements in this tuple are not in another tuple.TuplesubTuple(int i, int j)Produces a new Tuple containing the elements between the start and end indices.TuplesubTupleEnd(int i)Produces a new Tuple containing all elements from i onward.TuplesubTupleStart(int i)Produces a new Tuple containing the first i elements.Tupletail()java.lang.Object[]toArray()Tupleunion(Tuple t)Set arithmetic: compute union of set of tuple elements with set of elements of another tuple.Tupleunwrap(int i)For all elements that are tuples, substitute the ith sub-element.Tuplezip(Tuple other)Zips two tuples together, creating a Tuple of 2-ples.
-
-
-
Method Detail
-
append
Tuple append(java.lang.Object element)
Add an element to the end of a tuple. Equivalent to insert(size, element).- Parameters:
element- Element to be added- Returns:
- A new tuple with equal to the current plus the new element added to the end
-
contains
boolean contains(java.lang.Object element)
Tests whether the tuple has an element equal to its argument.- Parameters:
element- Element to test whether it is equal to any element in the tuple- Returns:
- true if the tuple contains an element equal to "element"
-
containsAll
boolean containsAll(java.lang.Iterable<?> element)
Tests whether the tuple has an element equal to its argument.- Parameters:
element- Element to test whether it is equal to any element in the tuple- Returns:
- true if the tuple contains an element equal to "element"
-
filter
Tuple filter(ExecutionContext ctx, FunctionDefinition fun)
Given a tuple and a predicate, returns a new tuple containing only the elements that match the predicate.- Parameters:
ctx- the current execution contextfun- the predicate- Returns:
- a new tuple containing only the elements that match the predicate
-
filter
Tuple filter(java.util.function.Predicate<java.lang.Object> fun)
Given a tuple and aPredicate, returns a new tuple containing only the elements that match the predicate.- Parameters:
fun- thePredicate- Returns:
- a new tuple containing only the elements that match the
Predicate
-
flatMap
default Tuple flatMap(ExecutionContext context, FunctionDefinition mapper)
Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.- Parameters:
context- the ProtelisExecutionContextmapper- the mapping function- Returns:
- a new tuple whose elements are the elements generated by the application of the provided funcion to each element of the tuple, in order.
-
flatMap
Tuple flatMap(java.util.function.Function<java.lang.Object,Tuple> mapper)
Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.- Parameters:
mapper- the mapping function- Returns:
- a new tuple whose elements are the elements generated by the application of the provided funcion to each element of the tuple, in order.
-
fold
java.lang.Object fold(java.lang.Object initial, java.util.function.BinaryOperator<java.lang.Object> fun)Performs a reduction operation, that maps a tuple to a single value.- Parameters:
initial- the base value of the computationfun- theBinaryOperatorthat will be applied to each couple of values- Returns:
- a single value of this tuple, or the default value if empty
-
fold
default java.lang.Object fold(ExecutionContext ctx, java.lang.Object initial, FunctionDefinition fun)
Performs a reduction operation, that maps a tuple to a single value.- Parameters:
ctx- the current execution contextinitial- the base value of the computationfun- theFunctionDefinitionthat will be applied to each couple of values- Returns:
- a single value of this tuple, or the default value if empty
-
get
java.lang.Object get(int i)
Retrieve the object at one of a tuple's indices.- Parameters:
i- Zero-based index of the object to be retrieved- Returns:
- The object at index i
-
head
java.lang.Object head()
- Returns:
- the first element of the tuple if present, throws an exception otherwise
- Throws:
java.lang.ArrayIndexOutOfBoundsException- if there are no elements in the tuple
-
indexof
int indexof(java.lang.Object element)
Searches for an element in a tuple, returning its index if found.- Parameters:
element- The object to be searched for- Returns:
- The first index where the search object is found, or -1 if not found
-
insert
Tuple insert(int i, java.lang.Object element)
Add an element to an arbitrary location within a tuple, moving all elements after it down.- Parameters:
i- Zero-based index at which the element is to be addedelement- Element to be added- Returns:
- A new tuple with equal to the current plus the new element added at the given index
-
intersection
Tuple intersection(Tuple t)
Set arithmetic: compute intersection of set of tuple elements with set of elements of another tuple.- Parameters:
t- tuple to take intersection with- Returns:
- a new tuple containing elements appearing in both tuples, in arbitrary order
-
isEmpty
boolean isEmpty()
- Returns:
- Returns true iff the size of the tuple is zero
-
map
Tuple map(ExecutionContext ctx, FunctionDefinition fun)
Maps a tuple to another same-length tuple by applying a function to each element.- Parameters:
ctx- the current execution contextfun- the function to apply- Returns:
- a new tuple containing the values
-
map
Tuple map(java.util.function.Function<java.lang.Object,java.lang.Object> fun)
Maps a tuple to another same-length tuple by applying a function to each element.- Parameters:
fun- the function to apply- Returns:
- a new tuple containing the values
-
max
java.lang.Object max(java.lang.Object def)
- Parameters:
def- the return in case max is not defined- Returns:
- the biggest element of this tuple, if defined, or default otherwise
-
mergeAfter
Tuple mergeAfter(Tuple tuple)
Append elements of another tuple to the end of this tuple.- Parameters:
tuple- tuple to be added- Returns:
- a new tuple
-
min
java.lang.Object min(java.lang.Object def)
- Parameters:
def- the return in case min is not defined- Returns:
- the smallest element of this tuple, if defined, or default otherwise
-
prepend
Tuple prepend(java.lang.Object element)
Add an element to the start of a tuple. Equivalent to insert(0, element).- Parameters:
element- Element to be added- Returns:
- A new tuple with equal to the current plus the new element added to the start
-
reduce
default java.lang.Object reduce(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)
Performs a reduction operation, that maps a tuple to a single value.- Parameters:
ctx- the current execution contextdefVal- the value that will be returned if the tuple is emptyfun- theBinaryOperatorthat will be applied to each couple of values- Returns:
- a single value of this tuple, or the default value if empty
-
reduce
java.lang.Object reduce(java.lang.Object defVal, java.util.function.BinaryOperator<java.lang.Object> fun)Performs a reduction operation, that maps a tuple to a single value.- Parameters:
defVal- the value that will be returned if the tuple is emptyfun- theBinaryOperatorthat will be applied to each couple of values- Returns:
- a single value of this tuple, or the default value if empty
-
set
Tuple set(int i, java.lang.Object element)
Replace an element in a tuple.- Parameters:
i- Zero-based index at which the element is to be replacedelement- Element to replace the current element- Returns:
- A new tuple with the element at the ith index replaced by "element"
-
size
int size()
- Returns:
- Number of elements in the tuple
-
sort
Tuple sort()
- Returns:
- a sorted version of this tuple
-
subtract
Tuple subtract(Tuple t)
Set arithmetic: compute which elements in this tuple are not in another tuple.- Parameters:
t- tuple to subtract- Returns:
- a new tuple containing elements not in t, in arbitrary order
-
subTuple
Tuple subTuple(int i, int j)
Produces a new Tuple containing the elements between the start and end indices. The start index is inclusive, the end index exclusive.- Parameters:
i- start indexj- end index- Returns:
- a new tuple containing elements i through j-1
-
subTupleEnd
Tuple subTupleEnd(int i)
Produces a new Tuple containing all elements from i onward. Equivalent to subTuple(i,Tuple.size()).- Parameters:
i- First element to take for new tuple- Returns:
- a new tuple containing elements i through size-1
-
subTupleStart
Tuple subTupleStart(int i)
Produces a new Tuple containing the first i elements. Equivalent to subTuple(0,i).- Parameters:
i- Number of elements to take- Returns:
- a new tuple containing elements 0 through i-1
-
tail
Tuple tail()
- Returns:
- a tuple with all the element of this tuple except the first
- Throws:
java.lang.ArrayIndexOutOfBoundsException- if there are no elements in the tuple
-
toArray
java.lang.Object[] toArray()
- Returns:
- an array with a copy of the contents of the tuple
-
union
Tuple union(Tuple t)
Set arithmetic: compute union of set of tuple elements with set of elements of another tuple.- Parameters:
t- tuple to take union with- Returns:
- a new tuple containing elements appearing in either tuple, in arbitrary order
-
unwrap
Tuple unwrap(int i)
For all elements that are tuples, substitute the ith sub-element. For example: [[1,2],3,[4,5],6].unwrap(1) produces [2,3,5,6].- Parameters:
i- index of sub-element to substitute- Returns:
- a new tuple with substituted elements
-
zip
Tuple zip(Tuple other)
Zips two tuples together, creating a Tuple of 2-ples. In case the tuple size differs, the zipping tuple will create a tuple whose length matches the smaller one, discarding elements of the longer one: [1, 2, 3].zip([4, 5]) returns [[1, 4], [2, 5]]- Parameters:
other- the tuple to zip this one with- Returns:
- A tuple of pairs (tuples of two elements) containing elements with the same index in this tuple and the provided one.
-
-