/** * Alias for {@link List#empty()} * * @param <T> Component type of element. * @return A singleton instance of empty {@link List} */ public static <T> Seq<T> Seq() { return List.empty(); }
/** * Alias for {@link List#empty()} * * @param <T> Component type of element. * @return A singleton instance of empty {@link List} */ public static <T> List<T> List() { return List.empty(); }
@Override public Seq<?> toSeq() { return List.empty(); }
/** * Returns a new Node containing the given value and having no children. * * @param value A value * @param <T> Value type * @return A new Node instance. */ static <T> Node<T> of(T value) { return new Node<>(value, io.vavr.collection.List.empty()); }
/** * Converts this to a {@link List}. * * @return A new {@link List}. */ default List<T> toList() { return ValueModule.toTraversable(this, List.empty(), List::of, List::ofAll); }
/** * Returns a List containing {@code n} times the given {@code element} * * @param <T> Component type of the List * @param n The number of elements in the List * @param element The element * @return A List of size {@code n}, where each element is the given {@code element}. */ static <T> List<T> fill(int n, T element) { return Collections.fillObject(n, element, empty(), List::of); }
@Override default Iterator<List<T>> crossProduct(int power) { return Collections.crossProduct(empty(), this, power); }
/** * Returns a List containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param <T> Component type of the List * @param n The number of elements in the List * @param f The Function computing element values * @return A List consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ static <T> List<T> tabulate(int n, Function<? super Integer, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return Collections.tabulate(n, f, empty(), List::of); }
@Override default <U> List<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); List<U> list = empty(); for (T t : this) { list = list.prepend(mapper.apply(t)); } return list.reverse(); }
@Override default <U> List<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); List<U> list = empty(); for (T t : this) { for (U u : mapper.apply(t)) { list = list.prepend(u); } } return list.reverse(); }
/** * Creates a Queue that contains the elements of the given {@link java.util.stream.Stream}. * * @param javaStream A {@link java.util.stream.Stream} * @param <T> Component type of the Stream. * @return A Queue containing the given elements in the same order. */ public static <T> Queue<T> ofAll(java.util.stream.Stream<? extends T> javaStream) { Objects.requireNonNull(javaStream, "javaStream is null"); return new Queue<>(io.vavr.collection.List.ofAll(javaStream), io.vavr.collection.List.empty()); }
@Override default List<T> reverse() { return (length() <= 1) ? this : foldLeft(empty(), List::prepend); }
@Override public io.vavr.collection.List<T> toList() { io.vavr.collection.List<T> results = io.vavr.collection.List.empty(); for (PriorityQueue<T> queue = this; !queue.isEmpty(); ) { final Tuple2<T, PriorityQueue<T>> dequeue = queue.dequeue(); results = results.prepend(dequeue._1); queue = dequeue._2; } return results.reverse(); }
@Override default Stream<T> dropRight(int n) { if (n <= 0) { return this; } else { return DropRight.apply(take(n).toList(), List.empty(), drop(n)); } }
@Override default List<T> dropRight(int n) { if (n <= 0) { return this; } if (n >= length()) { return empty(); } return ofAll(iterator().dropRight(n)); }
@Override default List<T> takeRight(int n) { if (n <= 0) { return empty(); } if (n >= length()) { return this; } return reverse().take(n).reverse(); }
static <T> Iterator<T> reverseIterator(Iterable<T> iterable) { if (iterable instanceof java.util.List) { return reverseListIterator((java.util.List<T>) iterable); } else if (iterable instanceof Seq) { return ((Seq<T>) iterable).reverseIterator(); } else { return List.<T>empty().pushAll(iterable).iterator(); } }
@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()); } } }
static <T> List<List<T>> apply(List<T> elements, int k) { if (k == 0) { return List.of(List.empty()); } else { return elements.zipWithIndex().flatMap( t -> apply(elements.drop(t._2 + 1), (k - 1)).map(c -> c.prepend(t._1)) ); } } }
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())); } } }