-
- Type Parameters:
T
- the type of the component
- All Known Subinterfaces:
Collection<T>
,Iterator<T>
,List<T>
,Map<K,T>
,Pipeline<T>
,Sequence<T>
,Set<T>
,Traversable<T>
,Tree<T>
,Tree.NodeCollection<T>
public interface Foldable<T>
In functional programming, fold (also termed reduce, accumulate, aggregate, compress, or inject) refers to a family of higher-order functions that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value.
This interface indicates that the implementation supports the folding operations.
- Since:
- 0.0.3
- See Also:
- Fold operation explained
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default T
fold(T start, java.util.function.BiFunction<? super T,? super T,? extends T> combiner)
Folds the elements using the given binary operationcombiner
, starting with thestart
and successively calling thecombiner
.<U> U
foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.<U> U
foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)
Folds the elements from the right, starting with thestart
value and combining the result by successively calling thecombiner
.default T
reduce(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Convenience method forreduceLeft
.T
reduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Reduce the collection from the left, starting with the first element in the collection and successively calling thereducer
until there are no more elements.
-
-
-
Method Detail
-
fold
default T fold(T start, java.util.function.BiFunction<? super T,? super T,? extends T> combiner)
Folds the elements using the given binary operationcombiner
, starting with thestart
and successively calling thecombiner
.- Parameters:
start
- the start value for the fold operationcombiner
- the operation used to combine elements- Returns:
- the folded value
- Throws:
java.lang.NullPointerException
- ifcombiner
is null- See Also:
foldLeft(Object, BiFunction)
-
foldLeft
<U> U foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.Example:
// result "!test" List("t", "e", "s", "t").foldLeft("!", (x, y) -> x + y)
- Type Parameters:
U
- the type of value returned- Parameters:
start
- the start value for the fold operationcombiner
- the operation used to combine elements- Returns:
- the folded value
- Throws:
java.lang.NullPointerException
- ifcombiner
is null
-
foldRight
<U> U foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)
Folds the elements from the right, starting with thestart
value and combining the result by successively calling thecombiner
.Example:
// result "tset!" List("t", "e", "s", "t").foldRight("!", (x, y) -> x + y)
- Type Parameters:
U
- the targeted type of combination method- Parameters:
start
- the start value for the fold operationcombiner
- the operation used to combine elements- Returns:
- the folded value
- Throws:
java.lang.NullPointerException
- ifcombiner
is null
-
reduce
default T reduce(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Convenience method forreduceLeft
.- Parameters:
reducer
- the operation to use to reduce the elements to one value- Returns:
- the reduced value
- See Also:
reduceLeft(BiFunction)
-
reduceLeft
T reduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Reduce the collection from the left, starting with the first element in the collection and successively calling the
reducer
until there are no more elements.Example:
// result "test" List("t", "e", "s", "t").reduceLeft((x, y) -> x + y)
This operation is similar to the foldLeft(Object, BiFunction) where the first element is the head of the elements.
- Parameters:
reducer
- the operation to use to reduce the elements to one value- Returns:
- the reduced value
- Throws:
java.lang.NullPointerException
- ifreducer
is null- See Also:
foldLeft(Object, BiFunction)
-
-