Interface Foldable<T>

  • 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 operation combiner, starting with the start and successively calling the combiner.
      <U> U foldLeft​(U start, java.util.function.BiFunction<? super U,​? super T,​? extends U> combiner)
      Folds the elements from the left, starting with the start value and combining the result by successively calling the combiner 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 the start value and combining the result by successively calling the combiner.
      default T reduce​(java.util.function.BiFunction<? super T,​? super T,​? extends T> reducer)
      Convenience method for 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.
    • 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 operation combiner, starting with the start and successively calling the combiner.
        Parameters:
        start - the start value for the fold operation
        combiner - the operation used to combine elements
        Returns:
        the folded value
        Throws:
        java.lang.NullPointerException - if combiner 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 the start value and combining the result by successively calling the combiner 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 operation
        combiner - the operation used to combine elements
        Returns:
        the folded value
        Throws:
        java.lang.NullPointerException - if combiner 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 the start value and combining the result by successively calling the combiner.

        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 operation
        combiner - the operation used to combine elements
        Returns:
        the folded value
        Throws:
        java.lang.NullPointerException - if combiner is null
      • reduce

        default T reduce​(java.util.function.BiFunction<? super T,​? super T,​? extends T> reducer)
        Convenience method for reduceLeft.
        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 - if reducer is null
        See Also:
        foldLeft(Object, BiFunction)