Interface Foldable<T>
- Type Parameters:
T- the type of elements contained in this foldable structure
- All Known Subinterfaces:
BitSet<T>,IndexedSeq<T>,Iterator<T>,LinearSeq<T>,List<T>,Map<K,,V> Multimap<K,,V> Seq<T>,Set<T>,SortedMap<K,,V> SortedMultimap<K,,V> SortedSet<T>,Stream<T>,Traversable<T>,Tree<T>
- All Known Implementing Classes:
Array,CharSeq,HashMap,HashMultimap,HashSet,LinkedHashMap,LinkedHashMultimap,LinkedHashSet,List.Cons,List.Nil,PriorityQueue,Queue,Stream.Cons,Stream.Empty,Tree.Empty,Tree.Node,TreeMap,TreeMultimap,TreeSet,Vector
Folding is the process of combining the elements of a structure using a provided function, typically accumulating a result.
Example:
// Concatenates all elements into a single String: "123"
Stream.of("1", "2", "3")
.fold("", (acc, element) -> acc + element);
- Author:
- Daniel Dietrich
-
Method Summary
Modifier and TypeMethodDescriptiondefault TFolds the elements of this structure using the given associative binary operator, starting with the providedzeroelement and successively applyingcombine.<U> UfoldLeft(U zero, @NonNull BiFunction<? super U, ? super T, ? extends U> combine) Folds the elements of this structure from the left, starting with the givenzerovalue and successively applying thecombinefunction to each element.<U> UfoldRight(U zero, @NonNull BiFunction<? super T, ? super U, ? extends U> combine) Folds the elements of this structure from the right, starting with the givenzerovalue and successively applying thecombinefunction to each element.default Treduce(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable by repeatedly applying the given binary operationop.reduceLeft(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable from the left by successively applying the given operationop.reduceLeftOption(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable from the left by successively applying the given operationop.reduceOption(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable by repeatedly applying the given binary operationop.reduceRight(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable from the right by successively applying the given operationop.reduceRightOption(@NonNull BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Foldable from the right by successively applying the given operationop.
-
Method Details
-
fold
Folds the elements of this structure using the given associative binary operator, starting with the providedzeroelement and successively applyingcombine.The order in which elements are combined is non-deterministic. Therefore,
combinemust be associative to guarantee a consistent result regardless of traversal order.The fold operations differ in how elements are combined:
foldLeft(Object, BiFunction): combines elements from left to right.foldRight(Object, BiFunction): combines elements from right to left.fold: requires an associative combine operation, as the element traversal is unordered. Associativity ensures the result is the same regardless of combination order. Note that most binary operators are not associative, so the result may vary if elements are combined in a different order.Together, this
Foldableand the associativecombineoperation form a Monoid.
// Result: 6 Set.of(1, 2, 3).fold(0, (a, b) -> a + b);- Parameters:
zero- the initial value to start folding withcombine- the function to combine two elements- Returns:
- the folded result
- Throws:
NullPointerException- ifcombineis null
-
foldLeft
Folds the elements of this structure from the left, starting with the givenzerovalue and successively applying thecombinefunction to each element.Folding from the left means that elements are combined in the order they are encountered, associating each step with the accumulated result so far.
Example:
// Result: "cba!" List.of("a", "b", "c").foldLeft("!", (acc, x) -> x + acc);- Type Parameters:
U- the type of the accumulated result- Parameters:
zero- the initial value to start folding withcombine- a function that combines the accumulated value and the next element- Returns:
- the folded result
- Throws:
NullPointerException- ifcombineis null
-
foldRight
Folds the elements of this structure from the right, starting with the givenzerovalue and successively applying thecombinefunction to each element.Folding from the right means that elements are combined starting from the last element and associating each step with the accumulated result so far.
Example:
// Result: "!cba" List.of("a", "b", "c").foldRight("!", (x, acc) -> acc + x);- Type Parameters:
U- the type of the accumulated result- Parameters:
zero- the initial value to start folding withcombine- a function that combines the next element and the accumulated value- Returns:
- the folded result
- Throws:
NullPointerException- ifcombineis null
-
reduce
Reduces the elements of this Foldable by repeatedly applying the given binary operationop.The order in which elements are combined is non-deterministic, so
opshould be associative to guarantee a consistent result.This method throws
NoSuchElementExceptionif the Foldable is empty.- Parameters:
op- a binary function to combine two elements- Returns:
- the reduced result
- Throws:
NoSuchElementException- if this Foldable is emptyNullPointerException- ifopis null
-
reduceOption
Reduces the elements of this Foldable by repeatedly applying the given binary operationop.The order of element combination is non-deterministic, so
opshould be associative to guarantee a consistent result.- Parameters:
op- a binary function to combine two elements- Returns:
- an
Optioncontaining the reduced result, orOption.none()if this Foldable is empty - Throws:
NullPointerException- ifopis null
-
reduceLeft
Reduces the elements of this Foldable from the left by successively applying the given operationop.Elements are combined in encounter order, starting from the left.
- Parameters:
op- a binary function to combine two elements- Returns:
- the reduced result
- Throws:
NoSuchElementException- if this Foldable is emptyNullPointerException- ifopis null
-
reduceLeftOption
Reduces the elements of this Foldable from the left by successively applying the given operationop.Returns an
Optioninstead of throwing an exception if the Foldable is empty.- Parameters:
op- a binary function to combine two elements- Returns:
- an
Optioncontaining the reduced result, orOption.none()if empty - Throws:
NullPointerException- ifopis null
-
reduceRight
Reduces the elements of this Foldable from the right by successively applying the given operationop.Elements are combined starting from the rightmost element.
- Parameters:
op- a binary function to combine two elements- Returns:
- the reduced result
- Throws:
NoSuchElementException- if this Foldable is emptyNullPointerException- ifopis null
-
reduceRightOption
Reduces the elements of this Foldable from the right by successively applying the given operationop.Returns an
Optioninstead of throwing an exception if the Foldable is empty.- Parameters:
op- a binary function to combine two elements- Returns:
- an
Optioncontaining the reduced result, orOption.none()if empty - Throws:
NullPointerException- ifopis null
-