/** * Print each value in this Folds to the console in turn (left-to-right) */ default void printOut() { stream().printOut(); }
/** * Collect the collectable into a {@link LinkedHashSet}. */ default Set<T> toSet(){ return stream().collect(Collectors.toSet()); } default <R1, R2, A1, A2> Tuple2<R1, R2> collect(Collector<? super T, A1, R1> c1, Collector<? super T, A2, R2> c2) {
/** * <pre> * {@code * assertEquals("123".length(),ReactiveSeq.of(1, 2, 3).join().length()); * } * </pre> * * @return Stream as concatenated String */ default String join() { return stream().join(); }
/** * <pre> * {@code * assertEquals("1, 2, 3".length(), ReactiveSeq.of(1, 2, 3).join(", ").length()); * } * </pre> * * @return Stream as concatenated String */ default String join(final String sep) { return stream().join(sep); }
/** * <pre> * {@code * assertEquals("^1|2|3$".length(), of(1, 2, 3).join("|", "^", "$").length()); * } * </pre> * * @return Stream as concatenated String */ default String join(final String sep, final String start, final String end) { return stream().join(sep, start, end); }
/** * Print each value in this Folds to the error console in turn (left-to-right) */ default void printErr() { stream().printErr(); }
/** * Write each element within this Folds in turn to the supplied PrintWriter * * @param writer PrintWriter to tell to */ default void print(final PrintWriter writer) { stream().print(writer); }
default double sumDouble(ToDoubleFunction<T> fn){ return stream().mapToDouble(fn).sum(); } default long sumLong(ToLongFunction<T> fn){
default boolean noneMatch(final Predicate<? super T> c) { return !stream().filter(c) .findFirst() .isPresent(); }
default ReactiveSeq<Tuple2<T,Integer>> occurances(){ return ReactiveSeq.deferFromStream(() -> { Map<T, Integer> map = stream().collect(Collectors.toMap(k -> k, v -> 1, (a, b) -> a + b)); return map.entrySet().stream(); }).map(e->Tuple.tuple(e.getKey(),e.getValue())); }
default Option<T> mode(){ Map<T,Integer> map = stream().collect(Collectors.toMap(k->k, v->1,(a, b)->a+b)); return ReactiveSeq.fromIterable(map.entrySet()) .maxBy(k -> k.getValue()) .map(t -> t.getKey()); } default ReactiveSeq<Tuple2<T,Integer>> occurances(){
default Seq<Tuple2<T,BigDecimal>> withPercentiles(){ Seq<T> list = stream().seq(); int precision = new Double(Math.log10(list.size())).intValue(); return list.zipWithIndex().map(t -> t.map2(idx -> { double d = (idx / new Double(list.size())); return new BigDecimal((d*100),new MathContext(precision)); })); } /*
default Maybe<Long> indexOfSlice(Iterable<? extends T> slice){ LazySeq<? extends T> ls = LazySeq.fromIterable(slice); Predicate<? super Seq<? super T>> pred = in -> in.equals(ls); return stream().sliding(ls.size(),1).indexOf(pred); } default Maybe<Long> lastIndexOfSlice(Iterable<? extends T> slice){
default Maybe<Long> lastIndexOfSlice(Iterable<? extends T> slice){ LazySeq<? extends T> ls = LazySeq.fromIterable(slice); Predicate<? super Seq<? super T>> pred = in -> in.equals(ls); return stream().sliding(ls.size(),1).lastIndexOf(pred); }
default double stdDeviation(ToDoubleFunction<T> fn){ Seq<T> list = stream().seq(); double avg = list.collect(Collectors.<T>averagingDouble(fn)); return Math.sqrt( list.stream().mapToDouble(fn) .map(i->i-avg) .map(i->i*i) .average() .getAsDouble()); }
default Maybe<Long> lastIndexOf(Predicate<? super T> pred){ return stream().zipWithIndex() .filter(p->pred.test(p._1())) .takeRight(1) .takeOne() .map(v->v._2()); } default Maybe<Long> indexOfSlice(Iterable<? extends T> slice){
default <R1, R2, A1, A2> Tuple2<R1, R2> collect(Collector<? super T, A1, R1> c1, Collector<? super T, A2, R2> c2) { return stream().collect(Collector.of(() -> Tuple.tuple(c1.supplier().get(),c2.supplier().get()), (t2, next) -> { c1.accumulator().accept(t2._1(), next); c2.accumulator().accept(t2._2(), next); },(t2, t2b) -> Tuple.tuple(c1.combiner().apply(t2._1(), t2b._1()),c2.combiner().apply(t2._2(), t2b._2())), t2 -> Tuple.tuple(c1.finisher().apply(t2._1()),c2.finisher().apply(t2._2())))); } default <R1, R2, R3, A1, A2, A3> Tuple3<R1, R2, R3> collect(Collector<? super T, A1, R1> c1, Collector<? super T, A2, R2> c2, Collector<? super T, A3, R3> c3) {
default <R1, R2, R3, A1, A2, A3> Tuple3<R1, R2, R3> collect(Collector<? super T, A1, R1> c1, Collector<? super T, A2, R2> c2, Collector<? super T, A3, R3> c3) { return stream().collect(Collector.of(() -> Tuple.tuple(c1.supplier().get(),c2.supplier().get(),c3.supplier().get()), (t3, next) -> { c1.accumulator().accept(t3._1(), next); c2.accumulator().accept(t3._2(), next); c3.accumulator().accept(t3._3(), next); },(t3, t3b) -> Tuple.tuple(c1.combiner().apply(t3._1(), t3b._1()),c2.combiner().apply(t3._2(), t3b._2()),c3.combiner().apply(t3._3(), t3b._3())), t3 -> Tuple.tuple(c1.finisher().apply(t3._1()),c2.finisher().apply(t3._2()),c3.finisher().apply(t3._3())))); }
default double populationVariance(ToDoubleFunction<T> fn){ Seq<T> list = stream().seq(); double avg = list.collect(Collectors.<T>averagingDouble(fn)); return (list.map(t -> fn.applyAsDouble(t)) .map(t -> t - avg) .map(t -> t * t) .sumDouble(i -> i)/(list.size())); }
default double variance(ToDoubleFunction<T> fn){ Seq<T> list = stream().seq(); double avg = list.collect(Collectors.<T>averagingDouble(fn)); return (list.map(t -> fn.applyAsDouble(t)) .map(t -> t - avg) .map(t -> t * t) .sumDouble(i -> i))/(list.size()-1); } default double populationVariance(ToDoubleFunction<T> fn){