Class Reduced<T>

  • Type Parameters:
    T - Scalar type
    All Implemented Interfaces:
    Scalar<T>

    public final class Reduced<T>
    extends Object
    implements Scalar<T>
    Reduces iterable via BiFunc.
    
     new Reduced<>(
         (first, last) -> first + last,
         new IterableOf<>(() -> 1L, () -> 2L, () -> 3L, () -> 4L)
     ).value() // returns 10L
     

    Here is how you can use it to find one of items according to the specified BiFunc:

    
     final String apple = new Reduced<>(
         (first, last) -> first,
         new IterableOf<Scalar<String>>(
             () -> "Apple",
             () -> "Banana",
             () -> "Orange"
         )
     ).value();
     final String orange = new Reduced<>(
         (first, last) -> last,
         new IterableOf<Scalar<String>>(
             () -> "Apple",
             () -> "Banana",
             () -> "Orange"
         )
     ).value();
     

    There is no thread-safety guarantee.

    This class implements Scalar, which throws a checked Exception. This may not be convenient in many cases. To make it more convenient and get rid of the checked exception you can use the Unchecked decorator. Or you may use IoChecked to wrap it in an IOException.

    Since:
    0.30
    • Constructor Detail

      • Reduced

        @SafeVarargs
        public Reduced​(BiFunc<? super T,​? super T,​? extends T> reduce,
                       T... values)
        Ctor.
        Parameters:
        reduce - Reducing function
        values - Values to be wrapped as scalars
      • Reduced

        public Reduced​(Iterable<T> values,
                       BiFunc<? super T,​? super T,​? extends T> reduce)
        Ctor.
        Parameters:
        values - Values to be wrapped as scalars
        reduce - Reducing function
        Since:
        0.55.0
      • Reduced

        public Reduced​(BiFunc<? super T,​? super T,​? extends T> reduce,
                       Iterable<? extends Scalar<? extends T>> scalars)
        Ctor.
        Parameters:
        reduce - Reducing function
        scalars - The scalars
    • Method Detail

      • value

        public T value()
                throws Exception
        Description copied from interface: Scalar
        Convert it to the value.
        Specified by:
        value in interface Scalar<T>
        Returns:
        The value
        Throws:
        Exception - If fails