/** * Returns the product-4 representation of this zipper. * * @return the product-4 representation of this zipper. */ public P4<Tree<A>, Stream<Tree<A>>, Stream<Tree<A>>, Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>> p() { return P.p(tree, lefts, rights, parents); }
private static <A> Stream<Tree<A>> combChildren(final Stream<Tree<A>> ls, final Tree<A> t, final Stream<Tree<A>> rs) { return ls.foldLeft(compose(flip(Stream.cons()), P.p1()), Stream.cons(t, P.p(rs))); }
/** * Append the given element to this stream to product a new stream. * * @param a The element to append. * @return A new stream with the given element at the end. */ public final Stream<A> snoc(final A a) { return snoc(p(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 A a1, final A a2) { return V2.p(P.p(a1, a2)); }
/** * Puts elements in a vector-3. * * @param a1 An element to put in a vector. * @param a2 An element to put in a vector. * @param a3 An element to put in a vector. * @return The vector-3. */ public static <A> V3<A> v(final A a1, final A a2, final A a3) { return V3.p(P.p(a1, a2, a3)); }
/** * Returns the success value or fails with the given error message. * * @param err The error message to fail with. * @return The success value. */ public final T successE(final String err) { return e.right().valueE(p(err)); }
/** * Inserts the given key and value association into the tree map. * If the given key is already mapped to a value, the old value is replaced with the given one. * * @param k The key to insert. * @param v The value to insert. * @return A new tree map with the given value mapped to the given key. */ public TreeMap<K, V> set(final K k, final V v) { return new TreeMap<>(tree.insert(p(k, Option.some(v)))); }
/** * Zips this list with the index of its element as a pair. * * @return A new list with the same length as this list. */ public final List<P2<A, Integer>> zipIndex() { return zipWith(range(0, length()), a -> i -> p(a, i)); }
/** * Applies the given function to all subtrees of this tree, returning a tree of the results (comonad pattern). * * @param f A function to bind across all the subtrees of this tree. * @return A new tree, with the results of applying the given function to each subtree of this tree. The result * of applying the function to the entire tree is the root label, and the results of applying to the * root's children are labels of the root's subforest, etc. */ public <B> Tree<B> cobind(final F<Tree<A>, B> f) { return unfoldTree((Tree<A> t) -> P.p(f.f(t), t.subForest())).f(this); }
/** * Returns the minimum (key, value) pair in the tree if the tree is not empty. */ public Option<P2<K, V>> min() { return tree.min().map(p -> p(p._1(), p._2().some())); }
/** * Splits this sequence into a pair of sequences at the given position. This is a O(log(n)) operation. * * @return Pair: the subsequence containing elements with indices less than <code>i</code> * and the subsequence containing elements with indices greater than or equal to <code>i</code>. */ public P2<Seq<A>, Seq<A>> split(final int i) { final P2<FingerTree<Integer, A>, FingerTree<Integer, A>> lr = ftree.split(index -> index > i); return P.p(new Seq<>(lr._1()), new Seq<>(lr._2())); }
/** modify polymorphically the target of a {@link PPrism} with an Applicative function */ public final F<S, V2<T>> modifyV2F(final F<A, V2<B>> f) { return s -> getOrModify(s).either( t -> V2.p(P.p(t, t)), t -> f.f(t).map(this::reverseGet) ); }
/** * Partitions the list into the list of fails and the list of successes */ public static <A, B> P2<List<A>, List<B>> partition(List<Validation<A, B>> list) { return p( list.filter(Validation::isFail).map(v -> v.fail()), list.filter(Validation::isSuccess).map(v -> v.success()) ); }
/** * Creates a {@link P6} by adding the given element to the current {@link P4} * * @param el the element to append * @return A {@link P6} containing the original {@link P4} with the extra element added at the end */ public final <E, F> P6<A, B, C, D, E, F> append(P2<E, F> el) { return P.p(_1(), _2(), _3(), _4(), el._1(), el._2()); }
public final <B> Promise<List<B>> traversePromise(final F<A, Promise<B>> f) { return foldRight( (a, acc) -> f.f(a).bind(b -> acc.fmap(bs -> bs.cons(b))), Promise.promise(Strategy.idStrategy(), p(List.nil()))); }
@Override public F<S, Promise<S>> modifyPromiseF(final F<A, Promise<A>> f) { return s -> getOption.f(s).<Promise<S>> option( () -> Promise.promise(Strategy.idStrategy(), P.p(s)), t -> f.f(t).fmap(b -> set.f(b).f(s)) ); }
/** * Returns the transformation equivalent for the stream show. */ public static <A> F<Stream<A>, Stream<Character>> streamShow_(final Show<A> sa, String start, String sep, String end) { return as -> join(as.map(sa.show_()).intersperse(fromString(sep)).cons(fromString(start)).snoc(p(fromString(end)))); }
P3<Option<Digit<V, A>>, A, Option<Digit<V, A>>> split1(final F<V, Boolean> predicate, final V acc) { final Measured<V, A> m = measured(); final MakeTree<V, A> mk = mkTree(m); final V acc1 = m.sum(acc, m.measure().f(as._1())); if (predicate.f(acc1)) { return P.p(none(), as._1(), some(mk.two(as._2(), as._3()))); } else if (predicate.f(m.sum(acc1, m.measure().f(as._2())))) { return P.p(some(mk.one(as._1())), as._2(), some(mk.one(as._3()))); } else { return P.p(some(mk.two(as._1(), as._2())), as._3(), none()); } }
/** * Turns a Stream of promises into a single promise of a Stream. * * @param s The strategy with which to sequence the promises. * @param as The Stream of promises to transform. * @return A single promise for the given Stream. */ public static <A> Promise<Stream<A>> sequence(final Strategy<Unit> s, final Stream<Promise<A>> as) { return join(foldRightS(s, curry((Promise<A> o, P1<Promise<Stream<A>>> p) -> o.bind(a -> p._1().fmap(Stream.<A>cons_().f(a)))), promise(s, p(Stream.nil()))).f(as)); }