Sequence
interface Sequence implements Collection, ArrayAccess (View source)
A Sequence describes the behaviour of values arranged in a single, linear dimension. Some languages refer to this as a "List". It’s similar to an array that uses incremental integer keys, with the exception of a few characteristics:
- Values will always be indexed as [0, 1, 2, …, size - 1].
- Only allowed to access values by index in the range [0, size - 1].
Use cases:
- Wherever you would use an array as a list (not concerned with keys).
- A more efficient alternative to SplDoublyLinkedList and SplFixedArray.
Methods
Ensures that enough memory is allocated for a required capacity.
Updates all values by applying a callback function to each value in the sequence.
Returns the current capacity.
Determines if the sequence contains all values.
Creates a new sequence using a callable to determine which values to include.
Returns the index of the value, or FALSE if not found.
Returns the first value in the sequence.
Returns the value at a given index.
Inserts values into the sequence at a given index.
Joins all values together as a string using an optional separator between each value.
Returns the last value in the sequence.
Returns the result of applying a callback function to each value in the sequence.
Removes and returns the last value.
Adds values to the end of the sequence.
Reduces the sequence to a single value using a callback function.
Removes and returns a value by index.
Reverses the sequence in-place.
Returns a reversed copy of the sequence.
Rotates the sequence by a given number of rotations, which is equivalent to successively calling $sequence->push($sequence->shift()) if the number of rotations is positive, or $sequence->unshift($sequence->pop()) if negative.
Updates a value at a given index.
Removes and returns the first value.
Creates a sub-sequence of a given range.
Sorts the sequence in-place, using an optional comparator function.
Returns a sorted copy, using an optional comparator function.
Returns the sum of all values in the sequence.
Adds values to the front of the sequence, moving all the current values forward to make room for the new values.
Details
int
count()
Count elements of an object
Traversable
getIterator()
Retrieve an external iterator
mixed
jsonSerialize()
Specify data which should be serialized to JSON
void
clear()
Removes all values from the collection.
TValue>
copy()
Returns a shallow copy of the collection.
bool
isEmpty()
Returns whether the collection is empty.
array
toArray()
Converts the collection to an array.
Note: Casting to an array is not supported yet.
bool
offsetExists(TKey $offset)
Whether a offset exists
mixed
offsetGet(TKey $offset)
Offset to retrieve
void
offsetSet(TKey $offset, TValue $value)
Offset to set
void
offsetUnset(TKey $offset)
Offset to unset
void
allocate(int $capacity)
Ensures that enough memory is allocated for a required capacity.
This removes the need to reallocate the internal as values are added.
void
apply(callable $callback)
Updates all values by applying a callback function to each value in the sequence.
int
capacity()
Returns the current capacity.
bool
contains(TValue ...$values)
Determines if the sequence contains all values.
TValue>
filter(callable|null $callback = null)
Creates a new sequence using a callable to determine which values to include.
int|false
find(TValue $value)
Returns the index of the value, or FALSE if not found.
TValue
first()
Returns the first value in the sequence.
TValue
get(int $index)
Returns the value at a given index.
void
insert(int $index, TValue ...$values)
Inserts values into the sequence at a given index.
string
join(string $glue = '')
Joins all values together as a string using an optional separator between each value.
TValue
last()
Returns the last value in the sequence.
Sequence
map(callable $callback)
Returns the result of applying a callback function to each value in the sequence.
Sequence
merge(TValue2> $values)
Returns the result of adding all given values to the sequence.
TValue
pop()
Removes and returns the last value.
void
push(TValue ...$values)
Adds values to the end of the sequence.
TCarry
reduce(callable $callback, TCarry $initial = null)
Reduces the sequence to a single value using a callback function.
TValue
remove(int $index)
Removes and returns a value by index.
void
reverse()
Reverses the sequence in-place.
TValue>
reversed()
Returns a reversed copy of the sequence.
void
rotate(int $rotations)
Rotates the sequence by a given number of rotations, which is equivalent to successively calling $sequence->push($sequence->shift()) if the number of rotations is positive, or $sequence->unshift($sequence->pop()) if negative.
void
set(int $index, TValue $value)
Updates a value at a given index.
TValue
shift()
Removes and returns the first value.
TValue>
slice(int $index, int $length = null)
Creates a sub-sequence of a given range.
void
sort(callable|null $comparator = null)
Sorts the sequence in-place, using an optional comparator function.
TValue>
sorted(callable|null $comparator = null)
Returns a sorted copy, using an optional comparator function.
float|int
sum()
Returns the sum of all values in the sequence.
Note: Arrays and objects are considered equal to zero when calculating the sum.
void
unshift(TValue ...$values)
Adds values to the front of the sequence, moving all the current values forward to make room for the new values.