Skip to content

Algorithm

Spice
import "std/data/algorithm";

Functions

filter

Spice
public f<Vector<T>> filter<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<Vector<R>> map<T, R>(const Vector<T>& items, f<R>(const T&) transform)

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
public f<R> reduce<T, R>(const Vector<T>& items, const R& initialValue, f<R>(const R&, const T&) combine)

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
public p forEach<T>(const Vector<T>& items, p(const T&) action)

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
public f<Optional<T>> find<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<bool> any<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<bool> all<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<bool> none<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<long> count<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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
public f<bool> contains<T>(const Vector<T>& items, const T& value)

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
public f<Optional<T>> min<T>(const Vector<T>& items, f<bool>(const T&, const T&) isLess)

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
public f<Optional<T>> max<T>(const Vector<T>& items, f<bool>(const T&, const T&) isLess)

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
public f<Pair<Vector<T>, Vector<T>>> partition<T>(const Vector<T>& items, f<bool>(const T&) predicate)

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