TupleOps

io.github.cakelier.tuples.space.TupleOps
See theTupleOps companion object
trait TupleOps[-T <: G, E, G]

A type-class representing the operations that must be supported by a tuple-like object.

Objects representing tuples, being so, must provide some operations that are expected to work on them. A tuple must allow to peek its internal structure in a predictable way, giving access to its number of elements, the elements at any given position, its head or its tail, to the initial part of it or the ending element. A tuple must also provide operations to update it, returning a new tuple, in a similar fashion to a sequence. So, elements can be appended, dropped, filtered, mapped, flat-mapped, taken, folded or an action can be performed for each one of them. At last, a tuple can be concatenated to another tuple, it can be converted to a Seq, existing a bijective relationship between them, split into two or zipped with another.

Type parameters

E

the concrete type of an element in the tuple

G

the abstract type of tuple to which the concrete type belongs

T

the concrete type of tuple that must support these operations

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

infix def :#(t: T, v: E): G

Appends an element to the end of the tuple, making it grow one element from the end. If an empty tuple is used for appending an element, the resulting tuple has only one element which is also its head.

Appends an element to the end of the tuple, making it grow one element from the end. If an empty tuple is used for appending an element, the resulting tuple has only one element which is also its head.

Value parameters

t

the tuple to which appending an element

v

the element to be appended

Attributes

Returns

a new tuple with the given element appended to it

def :##(t1: T, t2: G): G

Concatenates the second tuple after the first, with the resulting tuple having as arity the sum of both of them. If either of the tuples is an empty tuple, this operation returns the other, as concatenating an empty tuple has no effect.

Concatenates the second tuple after the first, with the resulting tuple having as arity the sum of both of them. If either of the tuples is an empty tuple, this operation returns the other, as concatenating an empty tuple has no effect.

Value parameters

t1

the tuple to which concatenate the second

t2

the tuple to be concatenated to the first

Attributes

Returns

a new tuple with the second one added to the end of the first one

def arity(t: T): Int

Returns the arity, i.e. the number of elements of the tuple. An empty tuple returns an arity of 0.

Returns the arity, i.e. the number of elements of the tuple. An empty tuple returns an arity of 0.

Value parameters

t

the tuple for which calculating the arity

Attributes

Returns

the arity of the tuple

def drop(t: T, n: Int): G

Drops the first elements of the given tuple in a given number, returning a tuple with the same elements as the given one, except the ones at the beginning. If the given value exceeds the arity of the tuple or it is a negative number, no operation is performed and the given tuple is returned unchanged.

Drops the first elements of the given tuple in a given number, returning a tuple with the same elements as the given one, except the ones at the beginning. If the given value exceeds the arity of the tuple or it is a negative number, no operation is performed and the given tuple is returned unchanged.

Value parameters

n

the number of elements to be dropped from the beginning

t

the tuple from which dropping the first elements

Attributes

Returns

the given tuple with the first elements at the beginning dropped, if possible, the unchanged tuple otherwise

def elem(t: T, n: Int): Option[E]

Returns the element at the n-th position in the tuple, if one exists. If no element exists, either because the position exceeds the arity of the tuple or it is a negative number, a None is returned.

Returns the element at the n-th position in the tuple, if one exists. If no element exists, either because the position exceeds the arity of the tuple or it is a negative number, a None is returned.

Value parameters

n

the position from which extracting an element

t

the tuple from which extracting an element

Attributes

Returns

a Some containing the element at the n-th position in the tuple if it exists, a None otherwise

def filter(t: T, p: E => Boolean): G

Returns a copy of the given tuple where only the elements passing the given predicate used as filter are kept. If all elements make the predicate return true, the original tuple is returned. If no element make the predicate return true, an empty tuple is returned.

Returns a copy of the given tuple where only the elements passing the given predicate used as filter are kept. If all elements make the predicate return true, the original tuple is returned. If no element make the predicate return true, an empty tuple is returned.

Value parameters

p

the predicate to be used as filter

t

the tuple on which applying the given filter

Attributes

Returns

a tuple with only the elements of the given one passing the predicate used as filter

def flatMap(t: T, f: E => G): G

Returns a tuple where the given function has been applied for every element in the given tuple. This operation maps elements of the tuple into tuples, so a flattening operation, or concatenation, is then performed so as to obtain a tuple as a result. The flattening happens only on one level, so if a tuple containing a tuple is returned by the function, the inner tuple will be an element of the resulting tuple.

Returns a tuple where the given function has been applied for every element in the given tuple. This operation maps elements of the tuple into tuples, so a flattening operation, or concatenation, is then performed so as to obtain a tuple as a result. The flattening happens only on one level, so if a tuple containing a tuple is returned by the function, the inner tuple will be an element of the resulting tuple.

Value parameters

f

the function to be applied on each element

t

the tuple on which applying the given function on each element

Attributes

Returns

a tuple where each element in it has been transformed according to the given function

def foldLeft[A](t: T, z: A, a: (A, E) => A): A

Returns an element which is the result of accumulating all elements of the given tuple from left to right, using the given element as a starting point and the given function as the accumulator. Being so, the accumulator will specify how to combine the elements folded so far into a single value with a new element of the tuple. If the given tuple is empty the starting element will be returned.

Returns an element which is the result of accumulating all elements of the given tuple from left to right, using the given element as a starting point and the given function as the accumulator. Being so, the accumulator will specify how to combine the elements folded so far into a single value with a new element of the tuple. If the given tuple is empty the starting element will be returned.

Type parameters

A

the type of the resulting element of this function

Value parameters

a

the accumulation function

t

the tuple on which applying the folding operation

z

the starting element of the folding operation

Attributes

Returns

all elements in the given tuple folded into one, starting from the given element and combining them once at a time using the given accumulation function

def foldRight[A](t: T, z: A, a: (E, A) => A): A

Returns an element which is the result of accumulating all elements of the given tuple from right to left, using the given element as a starting point and the given function as the accumulator. Being so, the accumulator will specify how to combine the elements folded so far into a single value with a new element of the tuple. If the given tuple is empty the starting element will be returned.

Returns an element which is the result of accumulating all elements of the given tuple from right to left, using the given element as a starting point and the given function as the accumulator. Being so, the accumulator will specify how to combine the elements folded so far into a single value with a new element of the tuple. If the given tuple is empty the starting element will be returned.

Type parameters

A

the type of the resulting element of this function

Value parameters

a

the accumulation function

t

the tuple on which applying the folding operation

z

the starting element of the folding operation

Attributes

Returns

all elements in the given tuple folded into one, starting from the given element and combining them once at a time using the given accumulation function

def foreach(t: T, f: E => Unit): Unit

Executes the given action for each element of the given tuple, allowing for side-effect-ful operations on the tuple. If the given tuple is an empty tuple, the action is performed zero times.

Executes the given action for each element of the given tuple, allowing for side-effect-ful operations on the tuple. If the given tuple is an empty tuple, the action is performed zero times.

Value parameters

f

the action to be performed

t

the tuple on which executing the action for each element

Attributes

def head(t: T): Option[E]

Returns the head of the given tuple, so its first element. If the tuple is empty, a None is returned.

Returns the head of the given tuple, so its first element. If the tuple is empty, a None is returned.

Value parameters

t

the tuple from which getting its head

Attributes

Returns

a Some containing the head of the given tuple, if it exists, a None otherwise

def init(t: T): G

Returns the initial part of the given tuple, so all its elements except its last. If the tuple is empty or contains only one element, its tail is also empty, so an empty tuple is returned.

Returns the initial part of the given tuple, so all its elements except its last. If the tuple is empty or contains only one element, its tail is also empty, so an empty tuple is returned.

Value parameters

t

the tuple from which getting its initial part

Attributes

Returns

the initial part of the given tuple

def last(t: T): Option[E]

Returns the last element of the given tuple, if it exists. If the tuple is empty a None is returned.

Returns the last element of the given tuple, if it exists. If the tuple is empty a None is returned.

Value parameters

t

the tuple from which getting its last element

Attributes

Returns

a Some containing the last element of the given tuple, if it exists, a None otherwise

def map(t: T, f: E => E): G

Returns a tuple where the given function has been applied for every element in the given tuple. This operation maps elements of the tuple into elements of the tuple, which means that if the function returns a tuple, this is not concatenated, but inserted into the result as any other element.

Returns a tuple where the given function has been applied for every element in the given tuple. This operation maps elements of the tuple into elements of the tuple, which means that if the function returns a tuple, this is not concatenated, but inserted into the result as any other element.

Value parameters

f

the function to be applied on each element

t

the tuple on which applying the given function on each element

Attributes

Returns

a tuple where each element in it has been transformed according to the given function

def split(t: T, n: Int): G

Splits the given tuple into two at the given index, returning a tuple made of two tuples, which concatenated make the original tuple. The splitting is always made to include the element at the given index in the second tuple, so an index of 0 will always return a tuple made of an empty tuple followed by the original tuple. If the given index is negative, the original tuple is returned. If an empty tuple is given to split, a tuple made of two empty tuples is then returned.

Splits the given tuple into two at the given index, returning a tuple made of two tuples, which concatenated make the original tuple. The splitting is always made to include the element at the given index in the second tuple, so an index of 0 will always return a tuple made of an empty tuple followed by the original tuple. If the given index is negative, the original tuple is returned. If an empty tuple is given to split, a tuple made of two empty tuples is then returned.

Value parameters

n

the index at which splitting the tuple

t

the tuple to split

Attributes

Returns

a tuple made of two tuples, which concatenated will yield the original tuple

def tail(t: T): G

Returns the tail of the given tuple, so all elements excluding its first one. If the tuple is empty, its tail is also empty, so an empty tuple is returned.

Returns the tail of the given tuple, so all elements excluding its first one. If the tuple is empty, its tail is also empty, so an empty tuple is returned.

Value parameters

t

the tuple from which getting its tail

Attributes

Returns

the tail of the given tuple

def take(t: T, n: Int): G

Returns the given tuple with only the first given number of elements kept, while the ones exceeding the count are dropped. If a value of 0 is supplied, an empty tuple is returned. If a negative value is supplied, the original tuple is returned.

Returns the given tuple with only the first given number of elements kept, while the ones exceeding the count are dropped. If a value of 0 is supplied, an empty tuple is returned. If a negative value is supplied, the original tuple is returned.

Value parameters

n

the number of elements to take

t

the tuple from which taking its first elements

Attributes

Returns

the given tuple with only the first given number of elements kept

def toSeq(t: T): Seq[E]

Returns the given tuple as a Seq, keeping the elements in the same order as in the tuple. If the given tuple is an empty tuple, an empty Seq is returned.

Returns the given tuple as a Seq, keeping the elements in the same order as in the tuple. If the given tuple is an empty tuple, an empty Seq is returned.

Value parameters

t

the tuple to be converted into a Seq

Attributes

Returns

a Seq containing the same elements of the tuple in the same order

def zip(t1: T, t2: G): G

Returns a tuple which elements are tuples made by an element of the given first tuple and an element of the given second tuple. The elements are coupled one by one by their corresponding positions. This means that, if one tuple has an arity greater than the other, the exceeding elements are dropped and not coupled. This means also that if one of the two given tuples is an empty tuple, then the returned tuple is an empty tuple.

Returns a tuple which elements are tuples made by an element of the given first tuple and an element of the given second tuple. The elements are coupled one by one by their corresponding positions. This means that, if one tuple has an arity greater than the other, the exceeding elements are dropped and not coupled. This means also that if one of the two given tuples is an empty tuple, then the returned tuple is an empty tuple.

Value parameters

t1

the first tuple to be zipped

t2

the second tuple to be zipped

Attributes

Returns

a tuple which elements are tuples which couple the corresponding elements of the two given tuples