net.sf.staccatocommons.defs.reduction
Interface Reduction<A,B>

Type Parameters:
B -
A -

public interface Reduction<A,B>

A Reduction are aggregate-functions-like objects that encapsulates a computation with effect that processes each element of a sequence of objects, calculating intermediate, accumulative results. The last accumulation is the final result of the reduction over such sequence.

Reductions can be applied to potentially any sequence of objects: Iterables, InputStreams, etc.

Although Reductions implement aggregation in an impure manner - its newAccumulator() message will return a mutable Accumulator - they are sharable, since Reduction itself are stateless

Since:
1.2
Author:
flbulgarelli
See Also:
SideEffectFree, Function

Method Summary
 Accumulator<A,B> newAccumulator()
          Answers a new, reseted accumulator to perform the reduction
<D> Reduction<D,B>
of(Applicable<D,A> function)
          Composes this reduction with a function, by returning a new Reduction that will apply this reduction to the results of applying the given function to each element For example, the following code: Reductions.
<D> Reduction<A,D>
then(Applicable<B,D> function)
          Composes the given function with this reduction, by returning a new Reduction that applies the function to the resulting element of the reduction For example, the following code: Reductions.
 

Method Detail

newAccumulator

@NonNull
Accumulator<A,B> newAccumulator()
Answers a new, reseted accumulator to perform the reduction

Returns:
a new, non null, Accumulator

then

<D> Reduction<A,D> then(Applicable<B,D> function)
Composes the given function with this reduction, by returning a new Reduction that applies the function to the resulting element of the reduction For example, the following code:
 Reductions.<Strin> max().then(Strings.length())
 
will return a reduction that computes the length of the maximum - lexicographically - string it processes:
  accumulator = lengthOfMax.newAccumulator();
  accumulator.accumulate("aaa");
  accumulator.accumulate("bbbbb");
  accumulator.accumulate("dd");
  
  accumulator.value() // 2, because length of "dd", the max string,  is 2
 

Parameters:
function -
Returns:
a new reduction

of

<D> Reduction<D,B> of(Applicable<D,A> function)
Composes this reduction with a function, by returning a new Reduction that will apply this reduction to the results of applying the given function to each element For example, the following code:
 Reductions.<Integer> max().of(Strings.length())
 
will return a reduction that computes the maximum length of the strings it processes:
  accumulator = maxLength.newAccumulator();
  accumulator.accumulate("aaa");
  accumulator.accumulate("bbbbb");
  accumulator.accumulate("dd");
  
  accumulator.value() // 5, because "bbbbb" has the max length, which is 5
 

Parameters:
function -
Returns:
a new reduction


Get Staccatocommons at SourceForge.net. Fast, secure and Free Open Source software downloads