/** * Returns the head element without modifying the List. * * @return the first element * @throws java.util.NoSuchElementException if this List is empty */ default T peek() { if (isEmpty()) { throw new NoSuchElementException("peek of empty list"); } return head(); }
/** * Performs an action on the head element of this {@code List}. * * @param action A {@code Consumer} * @return this {@code List} */ @Override default List<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@Override default List<T> dropWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); List<T> list = this; while (!list.isEmpty() && predicate.test(list.head())) { list = list.tail(); } return list; }
@Override default int indexOf(T element, int from) { int index = 0; for (List<T> list = this; !list.isEmpty(); list = list.tail(), index++) { if (index >= from && Objects.equals(list.head(), element)) { return index; } } return -1; }
@Override public T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty " + stringPrefix()); } else { return front.head(); } }
@Override default int lastIndexOf(T element, int end) { int result = -1, index = 0; for (List<T> list = this; index <= end && !list.isEmpty(); list = list.tail(), index++) { if (Objects.equals(list.head(), element)) { result = index; } } return result; }
/** * Write an object to a serialization stream. * * @param s An object serialization stream. * @throws java.io.IOException If an error occurs writing to the stream. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeInt(list.length()); for (List<T> l = list; !l.isEmpty(); l = l.tail()) { s.writeObject(l.head()); } }
static <T> Tuple2<List<T>, List<T>> splitByPredicateReversed(List<T> source, Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); List<T> init = Nil.instance(); List<T> tail = source; while (!tail.isEmpty() && !predicate.test(tail.head())) { init = init.prepend(tail.head()); tail = tail.tail(); } return Tuple.of(init, tail); } }
@Override public T last() { return rear.isEmpty() ? front.last() : rear.head(); }
private void drawAux(String indent, StringBuilder builder) { builder.append(value); for (io.vavr.collection.List<Node<T>> it = children; !it.isEmpty(); it = it.tail()) { final boolean isLast = it.tail().isEmpty(); builder.append('\n') .append(indent) .append(isLast ? "└──" : "├──"); it.head().drawAux(indent + (isLast ? " " : "│ "), builder); } }
/** * Removes the head element from this List. * * @return a tuple containing the head element and the remaining elements of this List * @throws java.util.NoSuchElementException if this List is empty */ default Tuple2<T, List<T>> pop2() { if (isEmpty()) { throw new NoSuchElementException("pop2 of empty list"); } return Tuple.of(head(), tail()); }
@Override default List<T> takeWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); List<T> result = Nil.instance(); for (List<T> list = this; !list.isEmpty() && predicate.test(list.head()); list = list.tail()) { result = result.prepend(list.head()); } return result.length() == length() ? this : result.reverse(); }
/** * Returns the head element without modifying the List. * * @return {@code None} if this List is empty, otherwise a {@code Some} containing the head element */ default Option<T> peekOption() { return isEmpty() ? Option.none() : Option.some(head()); }
@Override default List<T> removeFirst(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); List<T> init = empty(); List<T> tail = this; while (!tail.isEmpty() && !predicate.test(tail.head())) { init = init.prepend(tail.head()); tail = tail.tail(); } if (tail.isEmpty()) { return this; } else { return init.foldLeft(tail.tail(), List::prepend); } }
/** * Removes the head element from this List. * * @return {@code None} if this List is empty, otherwise {@code Some} {@code Tuple} containing the head element and the remaining elements of this List */ default Option<Tuple2<T, List<T>>> pop2Option() { return isEmpty() ? Option.none() : Option.some(Tuple.of(head(), pop())); }
@Override default List<T> take(int n) { if (n <= 0) { return empty(); } if (n >= length()) { return this; } List<T> result = Nil.instance(); List<T> list = this; for (int i = 0; i < n; i++, list = list.tail()) { result = result.prepend(list.head()); } return result.reverse(); }
static <T> Stream<T> apply(io.vavr.collection.List<T> front, io.vavr.collection.List<T> rear, Stream<T> remaining) { if (remaining.isEmpty()) { return remaining; } else if (front.isEmpty()) { return apply(rear.reverse(), io.vavr.collection.List.empty(), remaining); } else { return Stream.cons(front.head(), () -> apply(front.tail(), rear.prepend(remaining.head()), remaining.tail())); } } }
@Override default Tuple2<List<T>, List<T>> splitAt(int n) { if (isEmpty()) { return Tuple.of(empty(), empty()); } else { List<T> init = Nil.instance(); List<T> tail = this; while (n > 0 && !tail.isEmpty()) { init = init.prepend(tail.head()); tail = tail.tail(); n--; } return Tuple.of(init.reverse(), tail); } }
static <T> Stream<Node<T>> traverseInOrder(Node<T> node) { if (node.isLeaf()) { return Stream.of(node); } else { final io.vavr.collection.List<Node<T>> children = node.getChildren(); return children .tail() .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traverseInOrder(child))) .prepend(node) .prependAll(traverseInOrder(children.head())); } }
@Override default Tuple2<List<T>, List<T>> splitAtInclusive(Predicate<? super T> predicate) { if (isEmpty()) { return Tuple.of(empty(), empty()); } else { final Tuple2<List<T>, List<T>> t = SplitAt.splitByPredicateReversed(this, predicate); if (t._2.isEmpty() || t._2.tail().isEmpty()) { return Tuple.of(this, empty()); } else { return Tuple.of(t._1.prepend(t._2.head()).reverse(), t._2.tail()); } } }