All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dm.jrt.Main Maven / Gradle / Ivy

There is a newer version: 5.9.0
Show newest version
package com.github.dm.jrt;

import com.github.dm.jrt.function.BiFunction;
import com.github.dm.jrt.function.Consumer;
import com.github.dm.jrt.function.Function;
import com.github.dm.jrt.function.FunctionalRoutine;
import com.github.dm.jrt.function.Functions;

import static com.github.dm.jrt.function.Functions.onOutput;
import static com.github.dm.jrt.util.TimeDuration.seconds;

public class Main {

    public static void main(final String[] args) {

        Functions.builder().buildRoutine().thenParallelMap(new Function() {

            public Integer apply(final String s) {

                return Integer.parseInt(s);
            }
        }).thenParallelMap(new Function() {

            public Integer apply(final Integer integer) {

                return integer * integer;
            }
        }).thenSyncMap(new Function() {

            public SumData apply(final Integer integer) {

                return new SumData(integer, 1);
            }
        }).thenAsyncAccumulate(new BiFunction() {

            public SumData apply(final SumData data1, final SumData data2) {

                return new SumData(data1.sum + data2.sum, data1.count + data2.count);
            }
        }).thenSyncMap(new Function() {

            public Double apply(final SumData sumData) {

                return Math.sqrt((double) sumData.sum / sumData.count);
            }
        }).asyncCall(args).passTo(onOutput(new Consumer() {

            public void accept(final Double rms) {

                System.out.println(rms);
            }
        })).afterMax(seconds(1)).checkComplete();


        final FunctionalRoutine meanRoutine = //
                Functions.builder()
                         .buildRoutine()
                         .thenParallelMap(new Function() {

                             public Integer apply(final String s) {

                                 return Integer.parseInt(s);
                             }
                         })
                         .thenParallelMap(new Function() {

                             public Integer apply(final Integer integer) {

                                 return integer * integer;
                             }
                         })
                         .thenSyncMap(new Function() {

                             public SumData apply(final Integer integer) {

                                 return new SumData(integer, 1);
                             }
                         })
                         .thenAsyncAccumulate(new BiFunction() {

                             public SumData apply(final SumData data1, final SumData data2) {

                                 return new SumData(data1.sum + data2.sum,
                                                    data1.count + data2.count);
                             }
                         })
                         .thenSyncMap(new Function() {

                             public Double apply(final SumData sumData) {

                                 return Math.sqrt((double) sumData.sum / sumData.count);
                             }
                         });

        final Double rms = meanRoutine.asyncCall(args).afterMax(seconds(1)).next();
        System.out.println(rms);
        System.exit(0);
    }

    private static class SumData {

        private final int count;

        private final int sum;

        private SumData(final int sum, final int count) {

            this.sum = sum;
            this.count = count;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy