Interface Foldable<T>
- Type Parameters:
T- Component type of this foldable
- 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:
AbstractIterator, AbstractMultimap, AbstractQueue, Array, BitSetModule.AbstractBitSet, BitSetModule.BitSet1, BitSetModule.BitSet2, BitSetModule.BitSetIterator, BitSetModule.BitSetN, CharSeq, HashArrayMappedTrieModule.LeafNodeIterator, HashMap, HashMultimap, HashSet, IteratorModule.CachedIterator, IteratorModule.ConcatIterator, IteratorModule.DistinctIterator, IteratorModule.EmptyIterator, IteratorModule.GroupedIterator, LinkedHashMap, LinkedHashMultimap, LinkedHashSet, List.Cons, List.Nil, PriorityQueue, Queue, Stream.Cons, Stream.Empty, StreamModule.AppendElements, StreamModule.ConsImpl, StreamModule.FlatMapIterator, StreamModule.StreamIterator, Tree.Empty, Tree.Node, TreeMap, TreeMultimap, TreeSet, Vector
public interface Foldable<T>
Interface of foldable data structures.
Example:
// = "123"
Stream.of("1", "2", "3").fold("", (a1, a2) -> a1 + a2);
-
Method Summary
Modifier and TypeMethodDescriptiondefault TFolds this elements using the given associative binary operator, starting withzeroand successively callingcombine.<U> UfoldLeft(U zero, BiFunction<? super U, ? super T, ? extends U> combine) Folds this elements from the left, starting withzeroand successively callingcombine.<U> UfoldRight(U zero, BiFunction<? super T, ? super U, ? extends U> combine) Folds this elements from the right, starting withzeroand successively callingcombine.default Treduce(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationop.reduceLeft(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationopfrom the left.reduceLeftOption(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationopfrom the left.reduceOption(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationop.reduceRight(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationopfrom the right.reduceRightOption(BiFunction<? super T, ? super T, ? extends T> op) Accumulates the elements of this Foldable by successively calling the given operationopfrom the right.
-
Method Details
-
fold
Folds this elements using the given associative binary operator, starting withzeroand successively callingcombine. The order in which the elements are combined is non-deterministic.The methods
fold,foldLeftandfoldRightdiffer in how the elements are combined:foldLeft(Object, BiFunction)associates to the leftfoldRight(Object, BiFunction)associates to the right-
foldtakes 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.
// = 6 Set(1, 2, 3).fold(0, (a, b) -> a + b);- Parameters:
zero- A zero element to start with.combine- A function which combines elements.- Returns:
- a folded value
- Throws:
NullPointerException- ifcombineis null
-
foldLeft
Folds this elements from the left, starting withzeroand successively callingcombine.Example:
// = "cba!" List("a", "b", "c").foldLeft("!", (xs, x) -> x + xs)- Type Parameters:
U- the type to fold over- Parameters:
zero- A zero element to start with.combine- A function which combines elements.- Returns:
- a folded value
- Throws:
NullPointerException- ifcombineis null
-
foldRight
Folds this elements from the right, starting withzeroand successively callingcombine.Example:
// = "!cba" List("a", "b", "c").foldRight("!", (x, xs) -> xs + x)- Type Parameters:
U- the type of the folded value- Parameters:
zero- A zero element to start with.combine- A function which combines elements.- Returns:
- a folded value
- Throws:
NullPointerException- ifcombineis null
-
reduce
Accumulates the elements of this Foldable by successively calling the given operationop. The order of element iteration is undetermined.- Parameters:
op- A BiFunction of type T- Returns:
- the reduced value.
- Throws:
NoSuchElementException- if this is emptyNullPointerException- ifopis null
-
reduceOption
Accumulates the elements of this Foldable by successively calling the given operationop. The order of element iteration is undetermined.- Parameters:
op- A BiFunction of type T- Returns:
- Some of reduced value or None if the Foldable is empty.
- Throws:
NullPointerException- ifopis null
-
reduceLeft
Accumulates the elements of this Foldable by successively calling the given operationopfrom the left.- Parameters:
op- A BiFunction of type T- Returns:
- the reduced value.
- Throws:
NoSuchElementException- if this is emptyNullPointerException- ifopis null
-
reduceLeftOption
Accumulates the elements of this Foldable by successively calling the given operationopfrom the left.- Parameters:
op- A BiFunction of type T- Returns:
- Some of reduced value or None if the Foldable is empty.
- Throws:
NullPointerException- ifopis null
-
reduceRight
Accumulates the elements of this Foldable by successively calling the given operationopfrom the right.- Parameters:
op- An operation of type T- Returns:
- the reduced value.
- Throws:
NoSuchElementException- if this is emptyNullPointerException- ifopis null
-
reduceRightOption
Accumulates the elements of this Foldable by successively calling the given operationopfrom the right.- Parameters:
op- An operation of type T- Returns:
- Some of reduced value or None.
- Throws:
NullPointerException- ifopis null
-