/** * Returns the first element of this vector. * * @return the first element of this vector. */ public A _1() { return head._1(); }
/** * Returns a stream of the elements of this vector. * * @return a stream of the elements of this vector. */ public Stream<A> toStream() { return Stream.cons(head._1(), tail::toStream); }
/** * Returns a stream of the elements of this vector. * * @return a stream of the elements of this vector. */ public Stream<A> toStream() { return Stream.cons(head._1(), tail::toStream); }
/** * Traversable instance of P1 for Option * * @param f The function that produces Option * @return An Option of P1<B> */ public final <B> Option<P1<B>> traverseOption(final F<A, Option<B>> f){ return f.f(_1()).map(P::p); }
/** * Traversable instance of P1 for Stream * * @param f The function that produces Stream * @return An Stream of P1<B> */ public final <B> Stream<P1<B>> traverseStream(final F<A, Stream<B>> f){ return f.f(_1()).map(P::p); }
/** * Drops the given number of elements from the head of this stream if they are available. * * @param i The number of elements to drop from the head of this stream. * @return A stream with a length the same, or less than, this stream. */ public final Stream<A> drop(final int i) { Stream<A> xs = this; for (int c = 0; xs.isNotEmpty() && c < i; xs = xs.tail()._1()) c++; return xs; }
/** * Traversable instance of P1 for Either * * @param f The function produces Either * @return An Either of P1<B> */ public final <B, X> Either<X, P1<B>> traverseEither(final F<A, Either<X, B>> f){ return f.f(_1()).right().map(P::p); }
/** * Attempts to move the focus to the previous element, or throws an error if there are no more elements. * * @return A zipper with the focus moved one element to the left, if there are elements to the left of * focus, otherwise throws an error. */ public Zipper<A> tryPrevious() { if (left.isEmpty()) throw new Error("Tried previous at the beginning of a zipper."); else return zipper(left.tail()._1(), left.head(), right.cons(focus)); }
/** * Navigtes to the first child of the current location. * * @return A new tree zipper focused on the first child of the current node, or none if the node has no children. */ public Option<TreeZipper<A>> firstChild() { final Stream<Tree<A>> ts = tree.subForest()._1(); return ts.isEmpty() ? Option.none() : some(treeZipper(ts.head(), Stream.nil(), ts.tail()._1(), downParents())); }
/** * Appends the given stream to this stream. * * @param as The stream to append to this one. * @return A new stream that has appended the given stream. */ public final Stream<A> append(final Stream<A> as) { return isEmpty() ? as : cons(head(), () -> tail()._1().append(as)); }
/** * Turn a stream of functions into a function returning a stream. * * @param fs The stream of functions to sequence into a single function that returns a stream. * @return A function that, when given an argument, applies all the functions in the given stream to it * and returns a stream of the results. */ public static <A, B> F<B, Stream<A>> sequence_(final Stream<F<B, A>> fs) { return fs.foldRight((baf, p1) -> Function.bind(baf, p1._1(), curry((a, stream) -> cons(a, p(stream)))), Function .constant(Stream.nil())); }
/** * Returns the first <code>n</code> elements from the head of this stream. * * @param n The number of elements to take from this stream. * @return The first <code>n</code> elements from the head of this stream. */ public final Stream<A> take(final int n) { return n <= 0 || isEmpty() ? Stream.nil() : cons(head(), () -> n <= 1 ? Stream.nil() : tail()._1().take(n - 1)); }
/** * Applies a stream of comonadic functions to this promise, returning a stream of values. * * @param fs A stream of functions to apply to this promise. * @return A stream of the results of applying the given stream of functions to this promise. */ public <B> Stream<B> sequenceW(final Stream<F<Promise<A>, B>> fs) { return fs.isEmpty() ? Stream.nil() : Stream.cons(fs.head().f(this), () -> sequenceW(fs.tail()._1())); }
/** * Navigates to the left sibling of the current location. * * @return A new tree zipper focused on the left sibling of the current node, * or none if there are no siblings on the left. */ public Option<TreeZipper<A>> left() { return lefts.isEmpty() ? Option.none() : some(treeZipper(lefts.head(), lefts.tail()._1(), rights.cons(tree), parents)); }
/** * Returns a parser that produces an element from the stream if it is available and fails otherwise. * * @param e The error to fail with if no element is available. * @return A parser that produces an element from the stream if it is available and fails otherwise. */ public static <I, E> Parser<Stream<I>, I, E> element(final F0<E> e) { return parser(is -> is.isEmpty() ? Validation.fail(e.f()) : Validation.success(result(is.tail()._1(), is.head()))); }
/** * Returns a stream of all prefixes of this stream. A stream is considered a prefix of itself in tnis context. * * @return a stream of the prefixes of this stream, starting with the stream itself. */ public final Stream<Stream<A>> inits() { final Stream<Stream<A>> nil = cons(Stream.nil(), Stream::nil); return isEmpty() ? nil : nil.append(() -> tail()._1().inits().map(Stream.<A>cons_().f(head()))); }
/** * Returns all the elements of the queue with the highest (same) priority. */ public List<P2<K, A>> topN() { return toStream().uncons( List.nil(), top -> tail -> List.cons(top, tail._1().takeWhile(compose(equal.eq(top._1()), P2.__1())).toList()) ); }
/** * Transforms a stream of pairs into a stream of first components and a stream of second components. * * @param xs The stream of pairs to transform. * @return A stream of first components and a stream of second components. */ public static <A, B> P2<Stream<A>, Stream<B>> unzip(final Stream<P2<A, B>> xs) { return xs.foldRight((p, ps) -> { final P2<Stream<A>, Stream<B>> pp = ps._1(); return p(cons(p._1(), p(pp._1())), cons(p._2(), p(pp._2()))); }, p(Stream.nil(), Stream.nil())); }