@Override default List<T> appendAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return List.<T> ofAll(elements).prependAll(this); }
@SuppressWarnings("unchecked") @Override public Queue<T> prependAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty() && elements instanceof Queue) { return (Queue<T>) elements; } else { final io.vavr.collection.List<T> newFront = front.prependAll(elements); return (newFront == front) ? this : new Queue<>(newFront, rear); } }
@Override default List<T> leftPadTo(int length, T element) { final int actualLength = length(); if (length <= actualLength) { return this; } else { return prependAll(Iterator.continually(element).take(length - actualLength)); } }
@Override default List<T> insertAll(int index, Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (index < 0) { throw new IndexOutOfBoundsException("insertAll(" + index + ", elements)"); } List<T> preceding = Nil.instance(); List<T> tail = this; for (int i = index; i > 0; i--, tail = tail.tail()) { if (tail.isEmpty()) { throw new IndexOutOfBoundsException("insertAll(" + index + ", elements) on List of length " + length()); } preceding = preceding.prepend(tail.head()); } List<T> result = tail.prependAll(elements); for (T next : preceding) { result = result.prepend(next); } return result; }
@SuppressWarnings("unchecked") static <T, U> Tree<U> flatMap(Node<T> node, Function<? super T, ? extends Iterable<? extends U>> mapper) { final Tree<U> mapped = ofAll(mapper.apply(node.getValue())); if (mapped.isEmpty()) { return empty(); } else { final io.vavr.collection.List<Node<U>> children = (io.vavr.collection.List<Node<U>>) (Object) node .getChildren() .map(child -> flatMap(child, mapper)) .filter(Tree::nonEmpty); return of(mapped.get(), children.prependAll(mapped.getChildren())); } }
/** * Reduces many {@code Validation} instances into a single {@code Validation} by transforming an * {@code Iterable<Validation<? extends T>>} into a {@code Validation<Seq<T>>}. * * @param <E> value type in the case of invalid * @param <T> value type in the case of valid * @param values An iterable of Validation instances. * @return A valid Validation of a sequence of values if all Validation instances are valid * or an invalid Validation containing an accumulated List of errors. * @throws NullPointerException if values is null */ static <E, T> Validation<Seq<E>, Seq<T>> sequence(Iterable<? extends Validation<? extends Seq<? extends E>, ? extends T>> values) { Objects.requireNonNull(values, "values is null"); List<E> errors = List.empty(); List<T> list = List.empty(); for (Validation<? extends Seq<? extends E>, ? extends T> value : values) { if (value.isInvalid()) { errors = errors.prependAll(value.getError().reverse()); } else if (errors.isEmpty()) { list = list.prepend(value.get()); } } return errors.isEmpty() ? valid(list.reverse()) : invalid(errors.reverse()); }
@Override default List<T> appendAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return List.<T> ofAll(elements).prependAll(this); }
@SuppressWarnings("unchecked") @Override public Queue<T> prependAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty() && elements instanceof Queue) { return (Queue<T>) elements; } else { final io.vavr.collection.List<T> newFront = front.prependAll(elements); return (newFront == front) ? this : new Queue<>(newFront, rear); } }
@Override default List<T> leftPadTo(int length, T element) { final int actualLength = length(); if (length <= actualLength) { return this; } else { return prependAll(Iterator.continually(element).take(length - actualLength)); } }
@Override default List<T> insertAll(int index, Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (index < 0) { throw new IndexOutOfBoundsException("insertAll(" + index + ", elements)"); } List<T> preceding = Nil.instance(); List<T> tail = this; for (int i = index; i > 0; i--, tail = tail.tail()) { if (tail.isEmpty()) { throw new IndexOutOfBoundsException("insertAll(" + index + ", elements) on List of length " + length()); } preceding = preceding.prepend(tail.head()); } List<T> result = tail.prependAll(elements); for (T next : preceding) { result = result.prepend(next); } return result; }
@SuppressWarnings("unchecked") static <T, U> Tree<U> flatMap(Node<T> node, Function<? super T, ? extends Iterable<? extends U>> mapper) { final Tree<U> mapped = ofAll(mapper.apply(node.getValue())); if (mapped.isEmpty()) { return empty(); } else { final io.vavr.collection.List<Node<U>> children = (io.vavr.collection.List<Node<U>>) (Object) node .getChildren() .map(child -> flatMap(child, mapper)) .filter(Tree::nonEmpty); return of(mapped.get(), children.prependAll(mapped.getChildren())); } }
/** * Reduces many {@code Validation} instances into a single {@code Validation} by transforming an * {@code Iterable<Validation<? extends T>>} into a {@code Validation<Seq<T>>}. * * @param <E> value type in the case of invalid * @param <T> value type in the case of valid * @param values An iterable of Validation instances. * @return A valid Validation of a sequence of values if all Validation instances are valid * or an invalid Validation containing an accumulated List of errors. * @throws NullPointerException if values is null */ static <E, T> Validation<Seq<E>, Seq<T>> sequence(Iterable<? extends Validation<? extends Seq<? extends E>, ? extends T>> values) { Objects.requireNonNull(values, "values is null"); List<E> errors = List.empty(); List<T> list = List.empty(); for (Validation<? extends Seq<? extends E>, ? extends T> value : values) { if (value.isInvalid()) { errors = errors.prependAll(value.getError().reverse()); } else if (errors.isEmpty()) { list = list.prepend(value.get()); } } return errors.isEmpty() ? valid(list.reverse()) : invalid(errors.reverse()); }