/** * @return A Stream that repeats the value stored in this Value over and over */ default ReactiveSeq<T> generate(T alt) { return asSupplier(alt).generate(); } }
@Override public boolean hasNext() { return !complete[0] && fold(p->true,()->false); }
/** * Print each value in this Folds to the console in turn (left-to-right) */ default void printOut() { System.out.println(mkString()); }
default Function0<T> asSupplier(T alt){ return ()-> orElse(alt); } default boolean isPresent(){
/** * @param throwable Exception to use if this Value is zero * @return Try that has the same value as this Value or the provided Exception */ default <X extends Throwable> Try<T, X> toTry(final X throwable) { return Try.fromEither(toTry().asEither().mapLeft(t->throwable)); }
default NonEmptyList<T> nonEmptyListGet(Supplier<T> alt){ return NonEmptyList.of(orElseGet(alt)); }
default <R, A> R collect(final Collector<? super T, A, R> collector) { return stream().collect(collector); }
@Override public void request(final long n) { if (n < 1) { sub.onError(new IllegalArgumentException( "3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0.")); } if (!running.compareAndSet(true, false)) { return; } try { Iterator<T> it = iterator(); if(it.hasNext()) { T value = it.next(); if (!cancelled.get()) sub.onNext(value); } } catch (final Throwable t) { sub.onError(t); } try { sub.onComplete(); } finally { } }
@Test public void collect(){ List<Integer> lst = of(1).collect(Collectors.toList()); assertEquals(new Integer(1), lst.get(0)); } }
/** * Test for equivalence * null eqv to absent, embedded value equivalency, non-values converted to values before testing *. * <pre> * {@code * * Stream.of(Maybe.of(2)) * .filter(eqv(Maybe.of(2))) * .forEach(System.out::println); * * //Maybe[2] * * Stream.of(2) * .filter(eqv(Maybe.of(2))) * .forEach(System.out::println); * * //2 (passes filter as equivalent to Maybe[2]) * }</pre> * * @param value * @return */ public static <V> Predicate<Value<? super V>> eqv(final Value<? super V> value) { return test -> test == null ? value == null ? true : !value.toMaybe() .isPresent() : test.toMaybe() .equals(value.toMaybe()); }
default T fold(final Monoid<T> reducer) { return orElse(reducer.zero()); }
/** * @param throwable Exception to use if this Value is zero * @return Try that has the same value as this Value or the provided Exception */ default <X extends Throwable> Try<T, X> toTry(final X throwable) { return Try.fromEither(toTry().asEither().mapLeft(t->throwable)); }
default NonEmptyList<T> nonEmptyListGet(Supplier<T> alt){ return NonEmptyList.of(orElseGet(alt)); }
default <R, A> R collect(final Collector<? super T, A, R> collector) { return stream().collect(collector); }
@Override public void request(final long n) { if (n < 1) { sub.onError(new IllegalArgumentException( "3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0.")); } if (!running.compareAndSet(true, false)) { return; } try { Iterator<T> it = iterator(); if(it.hasNext()) { T value = it.next(); if (!cancelled.get()) sub.onNext(value); } } catch (final Throwable t) { sub.onError(t); } try { sub.onComplete(); } finally { } }
@Override public T next() { complete[0]=true; return fold(p->p,()->null); } };
/** * <pre> * {@code * import static cyclops2.function.Predicates.valuePresent; * * Seq.of(Maybe.ofNullable(null),Maybe.just(1),null) * .filter(valuePresent()); * * //Seq[Maybe[1]] * } * </pre> * @return A Predicate that checks if it's input is a cyclops2-react Value (which also contains a present value) */ public static <T> Predicate<T> valuePresent() { return t -> t instanceof Value ? ((Value) t).toMaybe() .isPresent() : false; }
/** * Use the value stored in this Value to seed a Stream generated from the provided function * * @param fn Function to generate a Stream * @return Stream generated from a seed value (the Value stored in this Value) and the provided function */ default ReactiveSeq<T> iterate(final UnaryOperator<T> fn,T alt) { return asSupplier(alt).iterate(fn); }
default Function0<T> asSupplier(T alt){ return ()-> orElse(alt); } default boolean isPresent(){