/** * Returns an empty stream. * * @return An empty stream. */ public static <A> P1<Stream<A>> nil_() { return P.lazy(Nil::new); }
/** * Swaps the elements around in this product. * * @return A new product-2 with the elements swapped. */ public final P2<B, A> swap() { return P.lazy(P2.this::_2, P2.this::_1); }
/** * Provides a thunk that throws an error using the given message when evaluated. * * @param s The message to fail with. * @return A thunk that throws an error using the given message when evaluated. */ public static <A> P1<A> error_(final String s) { return P.lazy(() -> { throw new Error(s); }); }
/** * Turns an optional P1 into a lazy option. */ public static <A> P1<Option<A>> sequence(final Option<P1<A>> o) { return P.lazy(() -> o.map(P1::_1)); }
/** * Promotes the given function so that it returns its value in a P1. * * @param f A function to have its result wrapped in a P1. * @return A function whose result is wrapped in a P1. */ public static <A, B> F<A, P1<B>> curry(final F<A, B> f) { return a -> P.lazy(() -> f.f(a)); }
/** * Puts elements in a vector-2. * * @param a1 An element to put in a vector. * @param a2 An element to put in a vector. * @return The vector-2. */ public static <A> V2<A> v(final F0<A> a1, final F0<A> a2) { return V2.p(P.lazy(a1, a2)); }
/** * Duplicates this product on the first element, and maps the given function across the duplicate (Comonad pattern). * * @param k A function to map over the duplicated product. * @return A new product with the result of the given function applied to this product as the first element, * and with the second element intact. */ public final <C> P2<C, B> cobind(final F<P2<A, B>, C> k) { P2<A, B> self = this; return P.lazy(() -> k.f(self), self::_2); }
/** * Suspends the given computation in a thunk. * * @param a A trampoline suspended in a thunk. * @return A trampoline whose next step runs the given thunk. */ public static <A> Trampoline<A> suspend(final F0<Trampoline<A>> a) { return new Suspend<>(P.lazy(a)); }
/** * Map the second element of the product. * * @param f The function to map with. * @return A product with the given function applied. */ public final <X> P2<A, X> map2(final F<B, X> f) { return P.lazy(P2.this::_1, () -> f.f(P2.this._2())); }
/** * Returns the tail of this stream if there is one or the given argument if this stream is empty. * * @param as The argument to return if this stream is empty. * @return The tail of this stream if there is one or the given argument if this stream is empty. */ public final P1<Stream<A>> orTail(final F0<Stream<A>> as) { return isEmpty() ? P.lazy(as) : tail(); }
public static P1<Unit> f(Effect0 e) { return P.lazy(() -> { e.f(); return unit(); }); }
/** * Map the first element of the product. * * @param f The function to map with. * @return A product with the given function applied. */ public final <X> P2<X, B> map1(final F<A, X> f) { P2<A, B> self = this; return P.lazy(() -> f.f(self._1()), self::_2); }
/** * Binds the given function across a promise of this promise (Comonad pattern). * * @param f A function to apply within a new promise of this promise. * @return A new promise of the result of applying the given function to this promise. */ public <B> Promise<B> cobind(final F<Promise<A>, B> f) { return promise(s, P.lazy(() -> f.f(Promise.this))); }
/** * Binds the given function to the value in a product-1 with a final join. * * @param f A function to apply to the value in a product-1. * @return The result of applying the given function to the value of given product-1. */ public final <B> P1<B> bind(final F<A, P1<B>> f) { P1<A> self = this; return P.lazy(() -> f.f(self._1())._1()); }
@SuppressWarnings("unchecked") public static <A, Z extends Exception> P1<Validation<Z, Unit>> f(TryEffect0<Z> t) { return P.lazy(() -> { try { t.f(); return Validation.success(Unit.unit()); } catch (Exception e) { return Validation.fail((Z) e); } }); }
/** * Performs a right-fold reduction across this stream. This function uses O(length) stack space. * * @param f The function to apply on each element of the stream. * @param b The beginning value to start the application from. * @return The final result after the right-fold reduction. */ public final <B> B foldRight(final F2<A, P1<B>, B> f, final B b) { return isEmpty() ? b : f.f(head(), P.lazy(() -> tail()._1().foldRight(f, b))); }
/** * A lazy semigroup for binary products. * * @param sa A semigroup for the product's first type. * @param sb A semigroup for the product's second type. * @return A semigroup for binary products. */ public static <A, B> Semigroup<P2<A, B>> p2Semigroup(final Semigroup<A> sa, final Semigroup<B> sb) { return semigroupDef((a1, a2) -> P.lazy(() -> sa.sum(a1._1(), a2._1()), () -> sb.sum(a1._2(), a2._2()))); }
private static <V, A> FingerTree<V, A> deepR(final Measured<V, A> measured, final Option<Digit<V, A>> rOpt, final FingerTree<V, Node<V, A>> m, final Digit<V, A> l) { return rOpt.option( P.lazy(() -> m.isEmpty() ? l.toTree() : mkTree(measured).deep(l, m.init(), m.last().toDigit())), (F<Digit<V, A>, FingerTree<V, A>>) r -> mkTree(measured).deep(l, m, r) ); }
public Tree<C> f(final Tree<A> as, final Tree<B> bs) { final F2<Tree<A>, Tree<B>, Tree<C>> self = this; return node(f.f(as.root(), bs.root()), P.lazy(() -> streamM(self).f(as.subForest()._1(), bs.subForest()._1()))); } };
public Either<P1<Trampoline<A>>, A> resume() { return left(sub.resume().either(p -> p.map(ot -> ot.fold( o -> o.foldNormal(cont, t -> t._1().bind(cont)), c -> codense(c.sub, o -> c.cont.f(o).bind(cont)) ) ), o -> P.lazy(() -> cont.f(o)))); } }