-
- 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 Tfold(T start, java.util.function.BiFunction<? super T,? super T,? extends T> combiner)Folds the elements using the given binary operationcombiner, starting with thestartand successively calling thecombiner.<U> UfoldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)Folds the elements from the left, starting with thestartvalue and combining the result by successively calling thecombineroperation.<U> UfoldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)Folds the elements from the right, starting with thestartvalue and combining the result by successively calling thecombiner.default Treduce(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)Convenience method forreduceLeft.TreduceLeft(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 thereduceruntil 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 thestartand 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- ifcombineris 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 thestartvalue and combining the result by successively calling thecombineroperation.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- ifcombineris 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 thestartvalue 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- ifcombineris 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
reduceruntil 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- ifreduceris null- See Also:
foldLeft(Object, BiFunction)
-
-