![JAR search and dependency download from the Maven repository](/logo.png)
io.vavr.collection.List Maven / Gradle / Ivy
Show all versions of vavr Show documentation
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2021 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vavr.collection;
import io.vavr.PartialFunction;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.Tuple3;
import io.vavr.collection.List.Nil;
import io.vavr.collection.ListModule.Combinations;
import io.vavr.collection.ListModule.SplitAt;
import io.vavr.control.Option;
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import static io.vavr.collection.JavaConverters.ChangePolicy.IMMUTABLE;
import static io.vavr.collection.JavaConverters.ChangePolicy.MUTABLE;
import static io.vavr.collection.JavaConverters.ListView;
/**
* An immutable {@code List} is an eager sequence of elements. Its immutability makes it suitable for concurrent programming.
*
* A {@code List} is composed of a {@code head} element and a {@code tail} {@code List}.
*
* There are two implementations of the {@code List} interface:
*
*
* - {@link Nil}, which represents the empty {@code List}.
* - {@link Cons}, which represents a {@code List} containing one or more elements.
*
*
* A {@code List} is a {@code Stack} in the sense that it stores elements allowing a last-in-first-out (LIFO) retrieval.
*
* Stack API:
*
*
* - {@link #peek()}
* - {@link #peekOption()}
* - {@link #pop()}
* - {@link #popOption()}
* - {@link #pop2()}
* - {@link #pop2Option()}
* - {@link #push(Object)}
* - {@link #push(Object[])}
* - {@link #pushAll(Iterable)}
*
*
* Methods to obtain a {@code List}:
*
*
*
* // factory methods
* List.empty() // = List.of() = Nil.instance()
* List.of(x) // = new Cons<>(x, Nil.instance())
* List.of(Object...) // e.g. List.of(1, 2, 3)
* List.ofAll(Iterable) // e.g. List.ofAll(Stream.of(1, 2, 3)) = 1, 2, 3
* List.ofAll(<primitive array>) // e.g. List.of(new int[] {1, 2, 3}) = 1, 2, 3
*
* // int sequences
* List.range(0, 3) // = 0, 1, 2
* List.rangeClosed(0, 3) // = 0, 1, 2, 3
*
*
*
* Note: A {@code List} is primarily a {@code Seq} and extends {@code Stack} for technical reasons (so {@code Stack} does not need to wrap {@code List}).
*
* If operating on a {@code List}, please prefer
*
*
* - {@link #prepend(Object)} over {@link #push(Object)}
* - {@link #prependAll(Iterable)} over {@link #pushAll(Iterable)}
* - {@link #tail()} over {@link #pop()}
* - {@link #tailOption()} over {@link #popOption()}
*
*
* Factory method applications:
*
*
*
* List<Integer> s1 = List.of(1);
* List<Integer> s2 = List.of(1, 2, 3);
* // = List.of(new Integer[] {1, 2, 3});
*
* List<int[]> s3 = List.ofAll(1, 2, 3);
* List<List<Integer>> s4 = List.ofAll(List.of(1, 2, 3));
*
* List<Integer> s5 = List.ofAll(1, 2, 3);
* List<Integer> s6 = List.ofAll(List.of(1, 2, 3));
*
* // cuckoo's egg
* List<Integer[]> s7 = List.<Integer[]> of(new Integer[] {1, 2, 3});
*
*
*
* Example: Converting a String to digits
*
*
*
* // = List(1, 2, 3)
* List.of("123".toCharArray()).map(c -> Character.digit(c, 10))
*
*
*
* See Okasaki, Chris: Purely Functional Data Structures (p. 7 ff.). Cambridge, 2003.
*
* @param Component type of the List
* @author Daniel Dietrich
*/
public interface List extends LinearSeq {
long serialVersionUID = 1L;
/**
* Returns a {@link java.util.stream.Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link List}.
*
* @param Component type of the List.
* @return A io.vavr.collection.List Collector.
*/
static Collector, List> collector() {
final Supplier> supplier = ArrayList::new;
final BiConsumer, T> accumulator = ArrayList::add;
final BinaryOperator> combiner = (left, right) -> {
left.addAll(right);
return left;
};
final Function, List> finisher = List::ofAll;
return Collector.of(supplier, accumulator, combiner, finisher);
}
/**
* Returns the single instance of Nil. Convenience method for {@code Nil.instance()} .
*
* Note: this method intentionally returns type {@code List} and not {@code Nil}. This comes handy when folding.
* If you explicitly need type {@code Nil} use {@linkplain Nil#instance()}.
*
* @param Component type of Nil, determined by type inference in the particular context.
* @return The empty list.
*/
static List empty() {
return Nil.instance();
}
/**
* A {@code List} is computed synchronously.
*
* @return false
*/
@Override
default boolean isAsync() {
return false;
}
@Override
boolean isEmpty();
/**
* A {@code List} is computed eagerly.
*
* @return false
*/
@Override
default boolean isLazy() {
return false;
}
/**
* Narrows a widened {@code List extends T>} to {@code List}
* by performing a type-safe cast. This is eligible because immutable/read-only
* collections are covariant.
*
* @param list A {@code List}.
* @param Component type of the {@code List}.
* @return the given {@code list} instance as narrowed type {@code List}.
*/
@SuppressWarnings("unchecked")
static List narrow(List extends T> list) {
return (List) list;
}
/**
* Returns a singleton {@code List}, i.e. a {@code List} of one element.
*
* @param element An element.
* @param The component type
* @return A new List instance containing the given element
*/
static List of(T element) {
return new Cons<>(element, Nil.instance());
}
/**
* Creates a List of the given elements.
*
*
* List.of(1, 2, 3, 4)
* = Nil.instance().prepend(4).prepend(3).prepend(2).prepend(1)
* = new Cons(1, new Cons(2, new Cons(3, new Cons(4, Nil.instance()))))
*
*
*
* @param Component type of the List.
* @param elements Zero or more elements.
* @return A list containing the given elements in the same order.
* @throws NullPointerException if {@code elements} is null
*/
@SafeVarargs
static List of(T... elements) {
Objects.requireNonNull(elements, "elements is null");
List result = Nil.instance();
for (int i = elements.length - 1; i >= 0; i--) {
result = result.prepend(elements[i]);
}
return result;
}
/**
* Creates a List of the given elements.
*
* The resulting list has the same iteration order as the given iterable of elements
* if the iteration order of the elements is stable.
*
* @param Component type of the List.
* @param elements An Iterable of elements.
* @return A list containing the given elements in the same order.
* @throws NullPointerException if {@code elements} is null
*/
@SuppressWarnings("unchecked")
static List ofAll(Iterable extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
if (elements instanceof List) {
return (List) elements;
} else if (elements instanceof ListView
&& ((ListView) elements).getDelegate() instanceof List) {
return (List) ((ListView) elements).getDelegate();
} else if (elements instanceof java.util.List) {
List result = Nil.instance();
final java.util.List list = (java.util.List) elements;
final ListIterator iterator = list.listIterator(list.size());
while (iterator.hasPrevious()) {
result = result.prepend(iterator.previous());
}
return result;
} else if (elements instanceof NavigableSet) {
List result = Nil.instance();
final java.util.Iterator iterator = ((NavigableSet) elements).descendingIterator();
while (iterator.hasNext()) {
result = result.prepend(iterator.next());
}
return result;
} else {
List result = Nil.instance();
for (T element : elements) {
result = result.prepend(element);
}
return result.reverse();
}
}
/**
* Creates a List that contains the elements of the given {@link java.util.stream.Stream}.
*
* @param javaStream A {@link java.util.stream.Stream}
* @param Component type of the Stream.
* @return A List containing the given elements in the same order.
*/
static List ofAll(java.util.stream.Stream extends T> javaStream) {
Objects.requireNonNull(javaStream, "javaStream is null");
final java.util.Iterator extends T> iterator = javaStream.iterator();
List list = List.empty();
while (iterator.hasNext()) {
list = list.prepend(iterator.next());
}
return list.reverse();
}
/**
* Creates a List from boolean values.
*
* @param elements boolean values
* @return A new List of Boolean values
* @throws NullPointerException if elements is null
*/
static List ofAll(boolean... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from byte values.
*
* @param elements byte values
* @return A new List of Byte values
* @throws NullPointerException if elements is null
*/
static List ofAll(byte... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from char values.
*
* @param elements char values
* @return A new List of Character values
* @throws NullPointerException if elements is null
*/
static List ofAll(char... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from double values.
*
* @param elements double values
* @return A new List of Double values
* @throws NullPointerException if elements is null
*/
static List ofAll(double... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from float values.
*
* @param elements a float values
* @return A new List of Float values
* @throws NullPointerException if elements is null
*/
static List ofAll(float... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from int values.
*
* @param elements int values
* @return A new List of Integer values
* @throws NullPointerException if elements is null
*/
static List ofAll(int... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from long values.
*
* @param elements long values
* @return A new List of Long values
* @throws NullPointerException if elements is null
*/
static List ofAll(long... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates a List from short values.
*
* @param elements short values
* @return A new List of Short values
* @throws NullPointerException if elements is null
*/
static List ofAll(short... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* 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 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 List tabulate(int n, Function super Integer, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return Collections.tabulate(n, f, empty(), List::of);
}
/**
* Returns a List containing {@code n} values supplied by a given Supplier {@code s}.
*
* @param Component type of the List
* @param n The number of elements in the List
* @param s The Supplier computing element values
* @return A List of size {@code n}, where each element contains the result supplied by {@code s}.
* @throws NullPointerException if {@code s} is null
*/
static List fill(int n, Supplier extends T> s) {
Objects.requireNonNull(s, "s is null");
return Collections.fill(n, s, empty(), List::of);
}
/**
* Returns a List containing {@code n} times the given {@code element}
*
* @param 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 List fill(int n, T element) {
return Collections.fillObject(n, element, empty(), List::of);
}
static List range(char from, char toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
static List rangeBy(char from, char toExclusive, int step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
@GwtIncompatible
static List rangeBy(double from, double toExclusive, double step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
/**
* Creates a List of int numbers starting from {@code from}, extending to {@code toExclusive - 1}.
*
* Examples:
*
*
* List.range(0, 0) // = List()
* List.range(2, 0) // = List()
* List.range(-2, 2) // = List(-2, -1, 0, 1)
*
*
*
* @param from the first number
* @param toExclusive the last number + 1
* @return a range of int values as specified or the empty range if {@code from >= toExclusive}
*/
static List range(int from, int toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
/**
* Creates a List of int numbers starting from {@code from}, extending to {@code toExclusive - 1},
* with {@code step}.
*
* Examples:
*
*
* List.rangeBy(1, 3, 1) // = List(1, 2)
* List.rangeBy(1, 4, 2) // = List(1, 3)
* List.rangeBy(4, 1, -2) // = List(4, 2)
* List.rangeBy(4, 1, 2) // = List()
*
*
*
* @param from the first number
* @param toExclusive the last number + 1
* @param step the step
* @return a range of long values as specified or the empty range if
* {@code from >= toInclusive} and {@code step > 0} or
* {@code from <= toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
static List rangeBy(int from, int toExclusive, int step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
/**
* Creates a List of long numbers starting from {@code from}, extending to {@code toExclusive - 1}.
*
* Examples:
*
*
* List.range(0L, 0L) // = List()
* List.range(2L, 0L) // = List()
* List.range(-2L, 2L) // = List(-2L, -1L, 0L, 1L)
*
*
*
* @param from the first number
* @param toExclusive the last number + 1
* @return a range of long values as specified or the empty range if {@code from >= toExclusive}
*/
static List range(long from, long toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
/**
* Creates a List of long numbers starting from {@code from}, extending to {@code toExclusive - 1},
* with {@code step}.
*
* Examples:
*
*
* List.rangeBy(1L, 3L, 1L) // = List(1L, 2L)
* List.rangeBy(1L, 4L, 2L) // = List(1L, 3L)
* List.rangeBy(4L, 1L, -2L) // = List(4L, 2L)
* List.rangeBy(4L, 1L, 2L) // = List()
*
*
*
* @param from the first number
* @param toExclusive the last number + 1
* @param step the step
* @return a range of long values as specified or the empty range if
* {@code from >= toInclusive} and {@code step > 0} or
* {@code from <= toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
static List rangeBy(long from, long toExclusive, long step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
static List rangeClosed(char from, char toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
static List rangeClosedBy(char from, char toInclusive, int step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
@GwtIncompatible
static List rangeClosedBy(double from, double toInclusive, double step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Creates a List of int numbers starting from {@code from}, extending to {@code toInclusive}.
*
* Examples:
*
*
* List.rangeClosed(0, 0) // = List(0)
* List.rangeClosed(2, 0) // = List()
* List.rangeClosed(-2, 2) // = List(-2, -1, 0, 1, 2)
*
*
*
* @param from the first number
* @param toInclusive the last number
* @return a range of int values as specified or the empty range if {@code from > toInclusive}
*/
static List rangeClosed(int from, int toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
/**
* Creates a List of int numbers starting from {@code from}, extending to {@code toInclusive},
* with {@code step}.
*
* Examples:
*
*
* List.rangeClosedBy(1, 3, 1) // = List(1, 2, 3)
* List.rangeClosedBy(1, 4, 2) // = List(1, 3)
* List.rangeClosedBy(4, 1, -2) // = List(4, 2)
* List.rangeClosedBy(4, 1, 2) // = List()
*
*
*
* @param from the first number
* @param toInclusive the last number
* @param step the step
* @return a range of int values as specified or the empty range if
* {@code from > toInclusive} and {@code step > 0} or
* {@code from < toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
static List rangeClosedBy(int from, int toInclusive, int step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Creates a List of long numbers starting from {@code from}, extending to {@code toInclusive}.
*
* Examples:
*
*
* List.rangeClosed(0L, 0L) // = List(0L)
* List.rangeClosed(2L, 0L) // = List()
* List.rangeClosed(-2L, 2L) // = List(-2L, -1L, 0L, 1L, 2L)
*
*
*
* @param from the first number
* @param toInclusive the last number
* @return a range of long values as specified or the empty range if {@code from > toInclusive}
*/
static List rangeClosed(long from, long toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
/**
* Creates a List of long numbers starting from {@code from}, extending to {@code toInclusive},
* with {@code step}.
*
* Examples:
*
*
* List.rangeClosedBy(1L, 3L, 1L) // = List(1L, 2L, 3L)
* List.rangeClosedBy(1L, 4L, 2L) // = List(1L, 3L)
* List.rangeClosedBy(4L, 1L, -2L) // = List(4L, 2L)
* List.rangeClosedBy(4L, 1L, 2L) // = List()
*
*
*
* @param from the first number
* @param toInclusive the last number
* @param step the step
* @return a range of int values as specified or the empty range if
* {@code from > toInclusive} and {@code step > 0} or
* {@code from < toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
static List rangeClosedBy(long from, long toInclusive, long step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Transposes the rows and columns of a {@link List} matrix.
*
* @param matrix element type
* @param matrix to be transposed.
* @return a transposed {@link List} matrix.
* @throws IllegalArgumentException if the row lengths of {@code matrix} differ.
*
* ex: {@code
* List.transpose(List(List(1,2,3), List(4,5,6))) → List(List(1,4), List(2,5), List(3,6))
* }
*/
static List> transpose(List> matrix) {
return Collections.transpose(matrix, List::ofAll, List::of);
}
/**
* Creates a list from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the list, otherwise {@code Some} {@code Tuple}
* of the element for the next call and the value to add to the
* resulting list.
*
* Example:
*
*
* List.unfoldRight(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<>(x, x-1)));
* // List(10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
*
*
*
* @param type of seeds
* @param type of unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return a list with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
static List unfoldRight(T seed, Function super T, Option>> f) {
return Iterator.unfoldRight(seed, f).toList();
}
/**
* Creates a list from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the list, otherwise {@code Some} {@code Tuple}
* of the value to add to the resulting list and
* the element for the next call.
*
* Example:
*
*
* List.unfoldLeft(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<>(x-1, x)));
* // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
*
*
*
* @param type of seeds
* @param type of unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return a list with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
static List unfoldLeft(T seed, Function super T, Option>> f) {
return Iterator.unfoldLeft(seed, f).toList();
}
/**
* Creates a list from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the list, otherwise {@code Some} {@code Tuple}
* of the value to add to the resulting list and
* the element for the next call.
*
* Example:
*
*
* List.unfold(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<>(x-1, x)));
* // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
*
*
*
* @param type of seeds and unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return a list with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
static List unfold(T seed, Function super T, Option>> f) {
return Iterator.unfold(seed, f).toList();
}
@Override
default List append(T element) {
return foldRight(of(element), (x, xs) -> xs.prepend(x));
}
@Override
default List appendAll(Iterable extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
return List. ofAll(elements).prependAll(this);
}
@GwtIncompatible
@Override
default java.util.List asJava() {
return JavaConverters.asJava(this, IMMUTABLE);
}
@GwtIncompatible
@Override
default List asJava(Consumer super java.util.List> action) {
return Collections.asJava(this, action, IMMUTABLE);
}
@GwtIncompatible
@Override
default java.util.List asJavaMutable() {
return JavaConverters.asJava(this, MUTABLE);
}
@GwtIncompatible
@Override
default List asJavaMutable(Consumer super java.util.List> action) {
return Collections.asJava(this, action, MUTABLE);
}
@Override
default List collect(PartialFunction super T, ? extends R> partialFunction) {
return ofAll(iterator(). collect(partialFunction));
}
@Override
default List> combinations() {
return rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity());
}
@Override
default List> combinations(int k) {
return Combinations.apply(this, Math.max(k, 0));
}
@Override
default Iterator> crossProduct(int power) {
return Collections.crossProduct(empty(), this, power);
}
@Override
default List distinct() {
return distinctBy(Function.identity());
}
@Override
default List distinctBy(Comparator super T> comparator) {
Objects.requireNonNull(comparator, "comparator is null");
final java.util.Set seen = new java.util.TreeSet<>(comparator);
return filter(seen::add);
}
@Override
default List distinctBy(Function super T, ? extends U> keyExtractor) {
Objects.requireNonNull(keyExtractor, "keyExtractor is null");
final java.util.Set seen = new java.util.HashSet<>();
return filter(t -> seen.add(keyExtractor.apply(t)));
}
@Override
default List drop(int n) {
if (n <= 0) {
return this;
}
if (n >= size()) {
return empty();
}
List list = this;
for (long i = n; i > 0 && !list.isEmpty(); i--) {
list = list.tail();
}
return list;
}
@Override
default List dropUntil(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return dropWhile(predicate.negate());
}
@Override
default List dropWhile(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
List list = this;
while (!list.isEmpty() && predicate.test(list.head())) {
list = list.tail();
}
return list;
}
@Override
default List dropRight(int n) {
if (n <= 0) {
return this;
}
if (n >= length()) {
return empty();
}
return ofAll(iterator().dropRight(n));
}
@Override
default List dropRightUntil(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return reverse().dropUntil(predicate).reverse();
}
@Override
default List dropRightWhile(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return dropRightUntil(predicate.negate());
}
@Override
default List filter(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
if (isEmpty()) {
return this;
} else {
final List filtered = foldLeft(empty(), (xs, x) -> predicate.test(x) ? xs.prepend(x) : xs);
if (filtered.isEmpty()) {
return empty();
} else if (filtered.length() == length()) {
return this;
} else {
return filtered.reverse();
}
}
}
@Override
default List reject(Predicate super T> predicate){
Objects.requireNonNull(predicate, "predicate is null");
return Collections.reject(this, predicate);
}
@Override
default List flatMap(Function super T, ? extends Iterable extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
List list = empty();
for (T t : this) {
for (U u : mapper.apply(t)) {
list = list.prepend(u);
}
}
return list.reverse();
}
@Override
default T get(int index) {
if (isEmpty()) {
throw new IndexOutOfBoundsException("get(" + index + ") on Nil");
}
if (index < 0) {
throw new IndexOutOfBoundsException("get(" + index + ")");
}
List list = this;
for (int i = index - 1; i >= 0; i--) {
list = list.tail();
if (list.isEmpty()) {
throw new IndexOutOfBoundsException("get(" + index + ") on List of length " + (index - i));
}
}
return list.head();
}
@Override
default Map> groupBy(Function super T, ? extends C> classifier) {
return Collections.groupBy(this, classifier, List::ofAll);
}
@Override
default Iterator> grouped(int size) {
return sliding(size, size);
}
@Override
default boolean hasDefiniteSize() {
return true;
}
@Override
default int indexOf(T element, int from) {
int index = 0;
for (List list = this; !list.isEmpty(); list = list.tail(), index++) {
if (index >= from && Objects.equals(list.head(), element)) {
return index;
}
}
return -1;
}
@Override
default List init() {
if (isEmpty()) {
throw new UnsupportedOperationException("init of empty list");
} else {
return dropRight(1);
}
}
@Override
default Option> initOption() {
return isEmpty() ? Option.none() : Option.some(init());
}
@Override
int length();
@Override
default List insert(int index, T element) {
if (index < 0) {
throw new IndexOutOfBoundsException("insert(" + index + ", e)");
}
List preceding = Nil.instance();
List tail = this;
for (int i = index; i > 0; i--, tail = tail.tail()) {
if (tail.isEmpty()) {
throw new IndexOutOfBoundsException("insert(" + index + ", e) on List of length " + length());
}
preceding = preceding.prepend(tail.head());
}
List result = tail.prepend(element);
for (T next : preceding) {
result = result.prepend(next);
}
return result;
}
@Override
default List insertAll(int index, Iterable extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
if (index < 0) {
throw new IndexOutOfBoundsException("insertAll(" + index + ", elements)");
}
List preceding = Nil.instance();
List 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 result = tail.prependAll(elements);
for (T next : preceding) {
result = result.prepend(next);
}
return result;
}
@Override
default List intersperse(T element) {
return ofAll(iterator().intersperse(element));
}
@Override
default boolean isTraversableAgain() {
return true;
}
@Override
default T last() {
return Collections.last(this);
}
@Override
default int lastIndexOf(T element, int end) {
int result = -1, index = 0;
for (List list = this; index <= end && !list.isEmpty(); list = list.tail(), index++) {
if (Objects.equals(list.head(), element)) {
result = index;
}
}
return result;
}
@Override
default List map(Function super T, ? extends U> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
List list = empty();
for (T t : this) {
list = list.prepend(mapper.apply(t));
}
return list.reverse();
}
@Override
default List orElse(Iterable extends T> other) {
return isEmpty() ? ofAll(other) : this;
}
@Override
default List orElse(Supplier extends Iterable extends T>> supplier) {
return isEmpty() ? ofAll(supplier.get()) : this;
}
@Override
default List padTo(int length, T element) {
final int actualLength = length();
if (length <= actualLength) {
return this;
} else {
return appendAll(Iterator.continually(element).take(length - actualLength));
}
}
@Override
default List 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 patch(int from, Iterable extends T> that, int replaced) {
from = from < 0 ? 0 : from;
replaced = replaced < 0 ? 0 : replaced;
List result = take(from).appendAll(that);
from += replaced;
result = result.appendAll(drop(from));
return result;
}
@Override
default Tuple2, List> partition(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
List left = empty(), right = empty();
for (T t : this) {
if (predicate.test(t)) {
left = left.prepend(t);
} else {
right = right.prepend(t);
}
}
return Tuple.of(left.reverse(), right.reverse());
}
/**
* 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();
}
/**
* 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 peekOption() {
return isEmpty() ? Option.none() : Option.some(head());
}
/**
* Performs an action on the head element of this {@code List}.
*
* @param action A {@code Consumer}
* @return this {@code List}
*/
@Override
default List peek(Consumer super T> action) {
Objects.requireNonNull(action, "action is null");
if (!isEmpty()) {
action.accept(head());
}
return this;
}
@Override
default List> permutations() {
if (isEmpty()) {
return Nil.instance();
} else {
final List tail = tail();
if (tail.isEmpty()) {
return of(this);
} else {
final List> zero = Nil.instance();
return distinct().foldLeft(zero, (xs, x) -> {
final Function, List> prepend = l -> l.prepend(x);
return xs.appendAll(remove(x).permutations().map(prepend));
});
}
}
}
/**
* Removes the head element from this List.
*
* @return the elements of this List without the head element
* @throws java.util.NoSuchElementException if this List is empty
*/
default List pop() {
if (isEmpty()) {
throw new NoSuchElementException("pop of empty list");
}
return tail();
}
/**
* Removes the head element from this List.
*
* @return {@code None} if this List is empty, otherwise a {@code Some} containing the elements of this List without the head element
*/
default Option> popOption() {
return isEmpty() ? Option.none() : Option.some(pop());
}
/**
* 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> pop2() {
if (isEmpty()) {
throw new NoSuchElementException("pop2 of empty list");
}
return Tuple.of(head(), tail());
}
/**
* 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>> pop2Option() {
return isEmpty() ? Option.none() : Option.some(Tuple.of(head(), pop()));
}
@Override
default List prepend(T element) {
return new Cons<>(element, this);
}
@Override
default List prependAll(Iterable extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
return isEmpty() ? ofAll(elements) : ofAll(elements).reverse().foldLeft(this, List::prepend);
}
/**
* Pushes a new element on top of this List.
*
* @param element The new element
* @return a new {@code List} instance, containing the new element on top of this List
*/
default List push(T element) {
return new Cons<>(element, this);
}
/**
* Pushes the given elements on top of this List. A List has LIFO order, i.e. the last of the given elements is
* the first which will be retrieved.
*
* @param elements Elements, may be empty
* @return a new {@code List} instance, containing the new elements on top of this List
* @throws NullPointerException if elements is null
*/
@SuppressWarnings("unchecked")
default List push(T... elements) {
Objects.requireNonNull(elements, "elements is null");
List result = this;
for (T element : elements) {
result = result.prepend(element);
}
return result;
}
/**
* Pushes the given elements on top of this List. A List has LIFO order, i.e. the last of the given elements is
* the first which will be retrieved.
*
* @param elements An Iterable of elements, may be empty
* @return a new {@code List} instance, containing the new elements on top of this List
* @throws NullPointerException if elements is null
*/
default List pushAll(Iterable elements) {
Objects.requireNonNull(elements, "elements is null");
List result = this;
for (T element : elements) {
result = result.prepend(element);
}
return result;
}
@Override
default List remove(T element) {
final Deque preceding = new ArrayDeque<>(size());
List result = this;
boolean found = false;
while (!found && !result.isEmpty()) {
final T head = result.head();
if (Objects.equals(head, element)) {
found = true;
} else {
preceding.addFirst(head);
}
result = result.tail();
}
if (!found) {
return this;
}
for (T next : preceding) {
result = result.prepend(next);
}
return result;
}
@Override
default List removeFirst(Predicate predicate) {
Objects.requireNonNull(predicate, "predicate is null");
List init = empty();
List 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);
}
}
@Override
default List removeLast(Predicate predicate) {
Objects.requireNonNull(predicate, "predicate is null");
final List removedAndReversed = reverse().removeFirst(predicate);
return removedAndReversed.length() == length() ? this : removedAndReversed.reverse();
}
@Override
default List removeAt(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("removeAt(" + index + ")");
}
if (isEmpty()) {
throw new IndexOutOfBoundsException("removeAt(" + index + ") on Nil");
}
List init = Nil.instance();
List tail = this;
while (index > 0 && !tail.isEmpty()) {
init = init.prepend(tail.head());
tail = tail.tail();
index--;
}
if (index > 0) {
throw new IndexOutOfBoundsException("removeAt() on Nil");
}
return init.reverse().appendAll(tail.tail());
}
@Override
default List removeAll(T element) {
return Collections.removeAll(this, element);
}
@Override
default List removeAll(Iterable extends T> elements) {
return Collections.removeAll(this, elements);
}
@Override
@Deprecated
default List removeAll(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}
@Override
default List replace(T currentElement, T newElement) {
List preceding = Nil.instance();
List tail = this;
while (!tail.isEmpty() && !Objects.equals(tail.head(), currentElement)) {
preceding = preceding.prepend(tail.head());
tail = tail.tail();
}
if (tail.isEmpty()) {
return this;
}
// skip the current head element because it is replaced
List result = tail.tail().prepend(newElement);
for (T next : preceding) {
result = result.prepend(next);
}
return result;
}
@Override
default List replaceAll(T currentElement, T newElement) {
List result = Nil.instance();
boolean changed = false;
for (List list = this; !list.isEmpty(); list = list.tail()) {
final T head = list.head();
if (Objects.equals(head, currentElement)) {
result = result.prepend(newElement);
changed = true;
} else {
result = result.prepend(head);
}
}
return changed ? result.reverse() : this;
}
@Override
default List retainAll(Iterable extends T> elements) {
return Collections.retainAll(this, elements);
}
@Override
default List reverse() {
return (length() <= 1) ? this : foldLeft(empty(), List::prepend);
}
@Override
default List rotateLeft(int n) {
return Collections.rotateLeft(this, n);
}
@Override
default List rotateRight(int n) {
return Collections.rotateRight(this, n);
}
@Override
default List scan(T zero, BiFunction super T, ? super T, ? extends T> operation) {
return scanLeft(zero, operation);
}
@Override
default List scanLeft(U zero, BiFunction super U, ? super T, ? extends U> operation) {
return Collections.scanLeft(this, zero, operation, Iterator::toList);
}
@Override
default List scanRight(U zero, BiFunction super T, ? super U, ? extends U> operation) {
return Collections.scanRight(this, zero, operation, Iterator::toList);
}
@Override
default List shuffle() {
return Collections.shuffle(this, List::ofAll);
}
@Override
default List slice(int beginIndex, int endIndex) {
if (beginIndex >= endIndex || beginIndex >= length() || isEmpty()) {
return empty();
} else {
List result = Nil.instance();
List list = this;
final long lowerBound = Math.max(beginIndex, 0);
final long upperBound = Math.min(endIndex, length());
for (int i = 0; i < upperBound; i++) {
if (i >= lowerBound) {
result = result.prepend(list.head());
}
list = list.tail();
}
return result.reverse();
}
}
@Override
default Iterator> slideBy(Function super T, ?> classifier) {
return iterator().slideBy(classifier).map(List::ofAll);
}
@Override
default Iterator> sliding(int size) {
return sliding(size, 1);
}
@Override
default Iterator> sliding(int size, int step) {
return iterator().sliding(size, step).map(List::ofAll);
}
@Override
default List sorted() {
return isEmpty() ? this : toJavaStream().sorted().collect(collector());
}
@Override
default List sorted(Comparator super T> comparator) {
Objects.requireNonNull(comparator, "comparator is null");
return isEmpty() ? this : toJavaStream().sorted(comparator).collect(collector());
}
@Override
default > List sortBy(Function super T, ? extends U> mapper) {
return sortBy(U::compareTo, mapper);
}
@Override
default List sortBy(Comparator super U> comparator, Function super T, ? extends U> mapper) {
return Collections.sortBy(this, comparator, mapper, collector());
}
@Override
default Tuple2, List> span(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
final Tuple2, Iterator> itt = iterator().span(predicate);
return Tuple.of(ofAll(itt._1), ofAll(itt._2));
}
@Override
default Tuple2, List> splitAt(int n) {
if (isEmpty()) {
return Tuple.of(empty(), empty());
} else {
List init = Nil.instance();
List tail = this;
while (n > 0 && !tail.isEmpty()) {
init = init.prepend(tail.head());
tail = tail.tail();
n--;
}
return Tuple.of(init.reverse(), tail);
}
}
@Override
default Tuple2, List> splitAt(Predicate super T> predicate) {
if (isEmpty()) {
return Tuple.of(empty(), empty());
} else {
final Tuple2, List> t = SplitAt.splitByPredicateReversed(this, predicate);
if (t._2.isEmpty()) {
return Tuple.of(this, empty());
} else {
return Tuple.of(t._1.reverse(), t._2);
}
}
}
@Override
default Tuple2, List> splitAtInclusive(Predicate super T> predicate) {
if (isEmpty()) {
return Tuple.of(empty(), empty());
} else {
final Tuple2, List> 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());
}
}
}
@Override
default String stringPrefix() {
return "List";
}
@Override
default List subSequence(int beginIndex) {
if (beginIndex < 0 || beginIndex > length()) {
throw new IndexOutOfBoundsException("subSequence(" + beginIndex + ")");
} else {
return drop(beginIndex);
}
}
@Override
default List subSequence(int beginIndex, int endIndex) {
Collections.subSequenceRangeCheck(beginIndex, endIndex, length());
if (beginIndex == endIndex) {
return empty();
} else if (beginIndex == 0 && endIndex == length()) {
return this;
} else {
List result = Nil.instance();
List list = this;
for (int i = 0; i < endIndex; i++, list = list.tail()) {
if (i >= beginIndex) {
result = result.prepend(list.head());
}
}
return result.reverse();
}
}
@Override
List tail();
@Override
default Option> tailOption() {
return isEmpty() ? Option.none() : Option.some(tail());
}
@Override
default List take(int n) {
if (n <= 0) {
return empty();
}
if (n >= length()) {
return this;
}
List