Top Description Methods
io.vavr.collection

public Interface Foldable<T>

Known Direct Subinterfaces
io.vavr.collection.Traversable
Type Parameters
<T>
Component type of this foldable
Imports
io.vavr.control.Option, java.util.NoSuchElementException, .Objects, java.util.function.BiFunction

Interface of foldable data structures.

Example:


// = "123"
Stream.of("1", "2", "3").fold("", (a1, a2) -> a1 + a2);
Author
Daniel Dietrich

Method Summary

Modifier and TypeMethod and Description
public default T

Returns:

a folded value
fold
(T
A zero element to start with.
zero
,
BiFunction<? super T, ? super T, ? extends T>
A function which combines elements.
combine
)

Folds this elements using the given associative binary operator, starting with zero and successively calling combine.

public <
the type to fold over
U
>
U

Returns:

a folded value
foldLeft
(U
A zero element to start with.
zero
,
BiFunction<? super U, ? super T, ? extends U>
A function which combines elements.
combine
)

Folds this elements from the left, starting with zero and successively calling combine.

public <
the type of the folded value
U
>
U

Returns:

a folded value
foldRight
(U
A zero element to start with.
zero
,
BiFunction<? super T, ? super U, ? extends U>
A function which combines elements.
combine
)

Folds this elements from the right, starting with zero and successively calling combine.

public default T

Returns:

the reduced value.
reduce
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op.

public T

Returns:

the reduced value.
reduceLeft
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op from the left.

public Option<T>

Returns:

Some of reduced value or None if the Foldable is empty.
reduceLeftOption
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op from the left.

public default Option<T>

Returns:

Some of reduced value or None if the Foldable is empty.
reduceOption
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op.

public T

Returns:

the reduced value.
reduceRight
(BiFunction<? super T, ? super T, ? extends T>
An operation of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op from the right.

public Option<T>

Returns:

Some of reduced value or None.
reduceRightOption
(BiFunction<? super T, ? super T, ? extends T>
An operation of type T
op
)

Accumulates the elements of this Foldable by successively calling the given operation op from the right.

Method Detail

foldback to summary
public default T fold(T zero, BiFunction<? super T, ? super T, ? extends T> combine)

Folds this elements using the given associative binary operator, starting with zero and successively calling combine. The order in which the elements are combined is non-deterministic.

The methods fold, foldLeft and foldRight differ in how the elements are combined:

  • foldLeft(Object, BiFunction) associates to the left
  • foldRight(Object, BiFunction) associates to the right
  • fold takes an associative combine operation because the traversal of elements is unordered/non-deterministic. The associativity guarantees that in each case the result will be the same, it does not matter in which order the elements are combined. Generally binary operators aren't associative, i.e. the result may differ if elements are combined in a different order.

    We say that this Foldable and the associative combine operation form a Monoid.

Example:
 // = 6
Set(1, 2, 3).fold(0, (a, b) -> a + b);
 
Parameters
zero:T

A zero element to start with.

combine:BiFunction<? super T, ? super T, ? extends T>

A function which combines elements.

Returns:T

a folded value

Exceptions
NullPointerException:
if combine is null
foldLeftback to summary
public <U> U foldLeft(U zero, BiFunction<? super U, ? super T, ? extends U> combine)

Folds this elements from the left, starting with zero and successively calling combine.

Example:

 // = "cba!"
List("a", "b", "c").foldLeft("!", (xs, x) -> x + xs)
 
Parameters
<U>
the type to fold over
zero:U

A zero element to start with.

combine:BiFunction<? super U, ? super T, ? extends U>

A function which combines elements.

Returns:U

a folded value

Exceptions
NullPointerException:
if combine is null
foldRightback to summary
public <U> U foldRight(U zero, BiFunction<? super T, ? super U, ? extends U> combine)

Folds this elements from the right, starting with zero and successively calling combine.

Example:

 // = "!cba"
List("a", "b", "c").foldRight("!", (x, xs) -> xs + x)
 
Parameters
<U>
the type of the folded value
zero:U

A zero element to start with.

combine:BiFunction<? super T, ? super U, ? extends U>

A function which combines elements.

Returns:U

a folded value

Exceptions
NullPointerException:
if combine is null
reduceback to summary
public default T reduce(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op. The order of element iteration is undetermined.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

A BiFunction of type T

Returns:T

the reduced value.

Exceptions
NoSuchElementException:
if this is empty
NullPointerException:
if op is null
reduceLeftback to summary
public T reduceLeft(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op from the left.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

A BiFunction of type T

Returns:T

the reduced value.

Exceptions
NoSuchElementException:
if this is empty
NullPointerException:
if op is null
reduceLeftOptionback to summary
public Option<T> reduceLeftOption(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op from the left.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

A BiFunction of type T

Returns:Option<T>

Some of reduced value or None if the Foldable is empty.

Exceptions
NullPointerException:
if op is null
reduceOptionback to summary
public default Option<T> reduceOption(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op. The order of element iteration is undetermined.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

A BiFunction of type T

Returns:Option<T>

Some of reduced value or None if the Foldable is empty.

Exceptions
NullPointerException:
if op is null
reduceRightback to summary
public T reduceRight(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op from the right.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

An operation of type T

Returns:T

the reduced value.

Exceptions
NoSuchElementException:
if this is empty
NullPointerException:
if op is null
reduceRightOptionback to summary
public Option<T> reduceRightOption(BiFunction<? super T, ? super T, ? extends T> op)

Accumulates the elements of this Foldable by successively calling the given operation op from the right.

Parameters
op:BiFunction<? super T, ? super T, ? extends T>

An operation of type T

Returns:Option<T>

Some of reduced value or None.

Exceptions
NullPointerException:
if op is null