Algorithm¶
| Spice | |
|---|---|
Functions¶
filter¶
| Spice | |
|---|---|
Build a new vector containing only the elements of items for which predicate returns true.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to filter |
predicate |
f<bool>(const T&) |
Function evaluated for each element; an element is kept when it returns true |
Returns: Vector<T> — A new vector holding the kept elements, in iteration order
map¶
| Spice | |
|---|---|
Build a new vector by applying transform to every element of items.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to map over |
transform |
f<R>(const T&) |
Function applied to each element to produce the mapped value |
Returns: Vector<R> — A new vector holding the transformed elements, in iteration order
reduce¶
| Spice | |
|---|---|
Combine all elements of items into a single value, left to right, starting from initialValue.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to reduce |
initialValue |
const R& |
Seed value combined with the first element |
combine |
f<R>(const R&, const T&) |
Function combining the running accumulator with the next element |
Returns: R — The final accumulated value
forEach¶
| Spice | |
|---|---|
Invoke action once for every element of items, in iteration order.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to iterate over |
action |
p(const T&) |
Procedure invoked for each element |
find¶
| Spice | |
|---|---|
Find the first element of items for which predicate returns true.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to search |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: Optional<T> — The first matching element, or an empty Optional if none match
any¶
| Spice | |
|---|---|
Check whether at least one element of items satisfies predicate.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to check |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: bool — True if any element matches, false if items is empty or none match
all¶
| Spice | |
|---|---|
Check whether every element of items satisfies predicate.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to check |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: bool — True if all elements match (vacuously true if items is empty), false otherwise
none¶
| Spice | |
|---|---|
Check whether no element of items satisfies predicate.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to check |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: bool — True if no element matches (vacuously true if items is empty), false otherwise
count¶
| Spice | |
|---|---|
Count how many elements of items satisfy predicate.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to inspect |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: long — The number of matching elements
contains¶
| Spice | |
|---|---|
Check whether items contains an element equal to value.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to search |
value |
const T& |
Value to look for |
Returns: bool — True if an equal element is present, false otherwise
min¶
| Spice | |
|---|---|
Find the smallest element of items, according to isLess.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to search |
isLess |
f<bool>(const T&, const T&) |
Strict less-than comparator; isLess(a, b) returns true when a comes before b |
Returns: Optional<T> — The smallest element, or an empty Optional if items is empty
max¶
| Spice | |
|---|---|
Find the largest element of items, according to isLess.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to search |
isLess |
f<bool>(const T&, const T&) |
Strict less-than comparator; isLess(a, b) returns true when a comes before b |
Returns: Optional<T> — The largest element, or an empty Optional if items is empty
partition¶
| Spice | |
|---|---|
Split items into two vectors according to predicate.
Parameters
| Name | Type | Description |
|---|---|---|
items |
const Vector<T>& |
Vector to partition |
predicate |
f<bool>(const T&) |
Function evaluated for each element |
Returns: Pair<Vector<T>, Vector<T>> — A pair of (matching elements, non-matching elements), each in iteration order