Search in sources :

Example 36 with BinaryOperator

use of java.util.function.BinaryOperator in project sandbox by backpaper0.

the class MapReduceExample method main.

public static void main(final String[] args) {
    final PrintWriter out = new PrintWriter(System.out);
    final List<String> list = Arrays.asList("foo", "bar", "baz", "hoge", "foobar");
    final Function<String, Integer> mapper = s -> {
        out.printf("mapper: %s%n", Thread.currentThread().getName());
        return s.length();
    };
    final BinaryOperator<Integer> reducer = (a, b) -> {
        out.printf("reducer: %s%n", Thread.currentThread().getName());
        return a + b;
    };
    final Integer result = MapReducer.mapReduce(list, mapper, reducer);
    out.printf("result: %s%n", result);
    out.flush();
}
Also used : PrintWriter(java.io.PrintWriter) Arrays(java.util.Arrays) List(java.util.List) CountedCompleter(java.util.concurrent.CountedCompleter) Function(java.util.function.Function) BinaryOperator(java.util.function.BinaryOperator) PrintWriter(java.io.PrintWriter)

Example 37 with BinaryOperator

use of java.util.function.BinaryOperator in project sandbox by backpaper0.

the class ScanCollectorTest method scan.

/**
 * <tt>[t<sub>0</sub>, t<sub>1</sub>, t<sub>2</sub> ... t<sub>n</sub>]</tt>というストリームに対して
 * <pre>
 * r<sub>0</sub> = init
 * r<sub>1</sub> = fn(r<sub>0</sub>, t<sub>0</sub>)
 * r<sub>2</sub> = fn(r<sub>1</sub>, t<sub>1</sub>)
 * r<sub>3</sub> = fn(r<sub>2</sub>, t<sub>2</sub>)
 * .
 * .
 * .
 * r<sub>n+1</sub> = fn(r<sub>n</sub>, t<sub>n</sub>)
 * </pre>
 * と処理して<tt>[r<sub>0</sub>, r<sub>1</sub>, r<sub>2</sub> ... r<sub>n+1</sub>]</tt>というリストを返します。
 *
 * @param init
 * @param fn
 * @return
 */
static <T, R> Collector<T, ?, List<R>> scan(final R init, final BiFunction<R, T, R> fn) {
    final Supplier<LinkedList<R>> supplier = () -> new LinkedList<>(Collections.singletonList(init));
    final BiConsumer<LinkedList<R>, T> accumulator = (acc, t) -> acc.add(fn.apply(acc.getLast(), t));
    final BinaryOperator<LinkedList<R>> combiner = (acc1, acc2) -> {
        acc1.addAll(acc2);
        return acc1;
    };
    final Function<LinkedList<R>, List<R>> finisher = acc -> acc;
    final Characteristics characteristics = Characteristics.IDENTITY_FINISH;
    return Collector.of(supplier, accumulator, combiner, finisher, characteristics);
}
Also used : IntStream(java.util.stream.IntStream) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BinaryOperator(java.util.function.BinaryOperator) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Assertions(org.junit.jupiter.api.Assertions) BiConsumer(java.util.function.BiConsumer) Characteristics(java.util.stream.Collector.Characteristics) Collector(java.util.stream.Collector) LinkedList(java.util.LinkedList) Collections(java.util.Collections) Characteristics(java.util.stream.Collector.Characteristics) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 38 with BinaryOperator

use of java.util.function.BinaryOperator in project TuringJavaSE3rdBatch by mrthetkhine.

the class ReduceDemo method main.

public static void main(String[] args) {
    Random random = new Random();
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 10; i++) {
        list.add(random.nextInt(10) + 1);
    }
    for (Integer i : list) {
        System.out.println("Item " + i);
    }
    BinaryOperator<Integer> acc = (a, b) -> {
        System.out.println("A " + a + " B " + b);
        return a < b ? a : b;
    };
    Optional<Integer> sum = list.stream().reduce(acc);
    System.out.println("Min " + sum.get());
}
Also used : Optional(java.util.Optional) Random(java.util.Random) BinaryOperator(java.util.function.BinaryOperator) ArrayList(java.util.ArrayList) Random(java.util.Random) ArrayList(java.util.ArrayList)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)38 List (java.util.List)20 ArrayList (java.util.ArrayList)12 Arrays (java.util.Arrays)12 Function (java.util.function.Function)10 Test (org.junit.jupiter.api.Test)10 Collections (java.util.Collections)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 BiFunction (java.util.function.BiFunction)7 Map (java.util.Map)6 Collector (java.util.stream.Collector)6 CommonUtils.checkArgument (com.amazon.randomcutforest.CommonUtils.checkArgument)4 CommonUtils.checkNotNull (com.amazon.randomcutforest.CommonUtils.checkNotNull)4 CommonUtils.toDoubleArray (com.amazon.randomcutforest.CommonUtils.toDoubleArray)4 CommonUtils.toFloatArray (com.amazon.randomcutforest.CommonUtils.toFloatArray)4 AnomalyAttributionVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor)4 AnomalyScoreVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyScoreVisitor)4 DynamicAttributionVisitor (com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor)4