Interface Seq<T>

  • Type Parameters:
    T - the element type
    All Superinterfaces:
    Foldable<T>, java.util.function.Function<java.lang.Integer,​T>, Function1<java.lang.Integer,​T>, java.lang.Iterable<T>, PartialFunction<java.lang.Integer,​T>, java.io.Serializable, Traversable<T>, Value<T>
    All Known Subinterfaces:
    IndexedSeq<T>, LinearSeq<T>, List<T>, Stream<T>
    All Known Implementing Classes:
    Array, CharSeq, List.Cons, List.Nil, Queue, Stream.Cons, Stream.Empty, StreamModule.AppendElements, StreamModule.ConsImpl, Vector

    public interface Seq<T>
    extends Traversable<T>, PartialFunction<java.lang.Integer,​T>, java.io.Serializable
    Base interface for immutable, sequential collections. Implementations preserve element order and return new instances on each structural modification, ensuring that all operations are side-effect free.

    Sequences built on this interface support a broad set of operations, including element insertion and removal, indexed access, slicing, filtering, sorting, zipping, and various combinatorial transformations such as permutations, combinations, and cross-products. Most operations return a new sequence while retaining the original ordering semantics.

    The interface also provides mechanisms for traversal, conversion to Java collection views (both read-only and mutable), and treating the sequence as an index-based partial function.

    Views:
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long serialVersionUID
      The serial version UID for serialization.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      Seq<T> append​(T element)
      Returns a new sequence with the given element appended at the end.
      Seq<T> appendAll​(@NonNull java.lang.Iterable<? extends T> elements)
      Returns a new sequence with all elements from the given Iterable appended at the end of this sequence.
      default T apply​(java.lang.Integer index)
      Deprecated.
      Will be removed
      java.util.List<T> asJava()
      Returns an immutable List view of this Seq.
      Seq<T> asJava​(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
      Creates an immutable List view of this Seq and passes it to the given action.
      java.util.List<T> asJavaMutable()
      Returns a mutable List view of this Seq.
      Seq<T> asJavaMutable​(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
      Creates a mutable List view of this Seq and passes it to the given action.
      PartialFunction<java.lang.Integer,​T> asPartialFunction()
      Returns a PartialFunction view of this Seq, where the function is defined at an index if this sequence contains at least index + 1 elements.
      <R> Seq<R> collect​(@NonNull PartialFunction<? super T,​? extends R> partialFunction)
      Applies a PartialFunction to all elements that are defined for it and collects the results.
      Seq<? extends Seq<T>> combinations()
      Returns a sequence containing all combinations of elements from this sequence, for all sizes from 0 to length().
      Seq<? extends Seq<T>> combinations​(int k)
      Returns all subsets of this sequence containing exactly k distinct elements, i.e., the k-combinations of this sequence.
      default boolean containsSlice​(@NonNull java.lang.Iterable<? extends T> that)
      Checks whether this sequence contains the given sequence as a contiguous slice.
      default Iterator<Tuple2<T,​T>> crossProduct()
      Computes the Cartesian product of this sequence with itself, producing all pairs of elements (this × this).
      Iterator<? extends Seq<T>> crossProduct​(int power)
      Returns the n-ary Cartesian power (cross product) of this sequence.
      default <U> Iterator<Tuple2<T,​U>> crossProduct​(@NonNull java.lang.Iterable<? extends U> that)
      Computes the Cartesian product of this sequence with another iterable, producing pairs of elements (this × that).
      Seq<T> distinct()
      Returns a new Traversable containing the elements of this instance with all duplicates removed.
      Seq<T> distinctBy​(@NonNull java.util.Comparator<? super T> comparator)
      Returns a new Traversable containing the elements of this instance without duplicates, as determined by the given comparator.
      <U> Seq<T> distinctBy​(@NonNull java.util.function.Function<? super T,​? extends U> keyExtractor)
      Returns a new Traversable containing the elements of this instance without duplicates, based on keys extracted from elements using keyExtractor.
      Seq<T> distinctByKeepLast​(@NonNull java.util.Comparator<? super T> comparator)
      Returns a sequence with duplicate elements removed, as determined by the provided comparator.
      <U> Seq<T> distinctByKeepLast​(@NonNull java.util.function.Function<? super T,​? extends U> keyExtractor)
      Returns a sequence with duplicates removed based on a key extracted from each element.
      Seq<T> drop​(int n)
      Returns a new Traversable without the first n elements, or an empty instance if this contains fewer than n elements.
      Seq<T> dropRight​(int n)
      Returns a new Traversable without the last n elements, or an empty instance if this contains fewer than n elements.
      Seq<T> dropRightUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
      Drops elements from the end of the sequence until an element satisfies the given predicate.
      Seq<T> dropRightWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
      Drops elements from the end of the sequence while the given predicate holds.
      Seq<T> dropUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns a new Traversable starting from the first element that satisfies the given predicate, dropping all preceding elements.
      Seq<T> dropWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns a new Traversable starting from the first element that does not satisfy the given predicate, dropping all preceding elements.
      default boolean endsWith​(@NonNull Seq<? extends T> that)
      Checks whether this sequence ends with the given sequence.
      Seq<T> filter​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns a new traversable containing only the elements that satisfy the given predicate.
      <U> Seq<U> flatMap​(@NonNull java.util.function.Function<? super T,​? extends java.lang.Iterable<? extends U>> mapper)
      Transforms each element of this Traversable into an Iterable of elements and flattens the resulting iterables into a single Traversable.
      default <U> U foldRight​(U zero, @NonNull java.util.function.BiFunction<? super T,​? super U,​? extends U> f)
      Folds the elements of this structure from the right, starting with the given zero value and successively applying the combine function to each element.
      T get​(int index)
      Returns the element at the specified index.
      <C> Map<C,​? extends Seq<T>> groupBy​(@NonNull java.util.function.Function<? super T,​? extends C> classifier)
      Groups elements of this Traversable based on a classifier function.
      Iterator<? extends Seq<T>> grouped​(int size)
      Splits this Traversable into consecutive blocks of the given size.
      default int indexOf​(T element)
      Returns the index of the first occurrence of the given element, or -1 if this sequence does not contain the element.
      int indexOf​(T element, int from)
      Returns the index of the first occurrence of the given element, starting at the specified index, or -1 if this sequence does not contain the element.
      default Option<java.lang.Integer> indexOfOption​(T element)
      Returns the index of the first occurrence of the given element as an Option.
      default Option<java.lang.Integer> indexOfOption​(T element, int from)
      Returns the index of the first occurrence of the given element at or after the specified start index, as an Option.
      default int indexOfSlice​(@NonNull java.lang.Iterable<? extends T> that)
      Returns the first index at which this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.
      int indexOfSlice​(@NonNull java.lang.Iterable<? extends T> that, int from)
      Returns the first index at or after the specified start index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.
      default Option<java.lang.Integer> indexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that)
      Returns the first index at which this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
      default Option<java.lang.Integer> indexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that, int from)
      Returns the first index at or after the specified start index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
      default int indexWhere​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns the index of the first element in this sequence that satisfies the given predicate, or -1 if no such element exists.
      int indexWhere​(@NonNull java.util.function.Predicate<? super T> predicate, int from)
      Returns the index of the first element at or after the specified start index that satisfies the given predicate, or -1 if no such element exists.
      default Option<java.lang.Integer> indexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns the index of the first element satisfying the given predicate as an Option.
      default Option<java.lang.Integer> indexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate, int from)
      Returns the index of the first element at or after the specified start index that satisfies the given predicate as an Option.
      Seq<T> init()
      Returns all elements of this Traversable except the last one.
      Option<? extends Seq<T>> initOption()
      Returns all elements of this Traversable except the last one, wrapped in an Option.
      Seq<T> insert​(int index, T element)
      Returns a new sequence with the given element inserted at the specified index.
      Seq<T> insertAll​(int index, @NonNull java.lang.Iterable<? extends T> elements)
      Returns a new sequence with the given elements inserted at the specified index.
      Seq<T> intersperse​(T element)
      Returns a new sequence where the given element is inserted between all elements of this sequence.
      default boolean isSequential()
      Indicates whether the elements of this Traversable appear in encounter (insertion) order.
      default Iterator<T> iterator​(int index)
      Returns an iterator over the elements of this sequence starting at the specified index.
      default int lastIndexOf​(T element)
      Returns the index of the last occurrence of the given element, or -1 if this sequence does not contain the element.
      int lastIndexOf​(T element, int end)
      Returns the index of the last occurrence of the given element at or before the specified end index, or -1 if this sequence does not contain the element.
      default Option<java.lang.Integer> lastIndexOfOption​(T element)
      Returns the index of the last occurrence of the given element as an Option.
      default Option<java.lang.Integer> lastIndexOfOption​(T element, int end)
      Returns the index of the last occurrence of the given element at or before the specified end index as an Option.
      default int lastIndexOfSlice​(@NonNull java.lang.Iterable<? extends T> that)
      Returns the last index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.
      int lastIndexOfSlice​(@NonNull java.lang.Iterable<? extends T> that, int end)
      Returns the last index at or before the specified end index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.
      default Option<java.lang.Integer> lastIndexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that)
      Returns the last index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
      default Option<java.lang.Integer> lastIndexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that, int end)
      Returns the last index at or before the specified end index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
      default int lastIndexWhere​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns the index of the last element in this sequence that satisfies the given predicate, or -1 if no such element exists.
      int lastIndexWhere​(@NonNull java.util.function.Predicate<? super T> predicate, int end)
      Returns the index of the last element at or before the specified end index that satisfies the given predicate, or -1 if no such element exists.
      default Option<java.lang.Integer> lastIndexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns the index of the last element satisfying the given predicate as an Option.
      default Option<java.lang.Integer> lastIndexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate, int end)
      Returns the index of the last element at or before the specified end index that satisfies the given predicate, wrapped in an Option.
      Seq<T> leftPadTo​(int length, T element)
      Returns a new sequence with this sequence padded on the left with the given element until the specified target length is reached.
      default Function1<java.lang.Integer,​Option<T>> lift()
      Deprecated.
      Will be removed
      <U> Seq<U> map​(@NonNull java.util.function.Function<? super T,​? extends U> mapper)
      Transforms the elements of this Traversable to a new type, preserving order if defined.
      default <U> Seq<U> mapTo​(U value)
      Maps the underlying value to another fixed value.
      default Seq<java.lang.Void> mapToVoid()
      Maps the underlying value to Void
      static <T> Seq<T> narrow​(Seq<? extends T> seq)
      Narrows a Seq<? extends T> to Seq<T> via a safe unchecked cast.
      Seq<T> orElse​(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
      Returns this Traversable if it is non-empty; otherwise, returns the result of evaluating the given supplier.
      Seq<T> orElse​(java.lang.Iterable<? extends T> other)
      Returns this Traversable if it is non-empty; otherwise, returns the given alternative.
      Seq<T> padTo​(int length, T element)
      Returns a new sequence with this sequence padded on the right with the given element until the specified target length is reached.
      Tuple2<? extends Seq<T>,​? extends Seq<T>> partition​(@NonNull java.util.function.Predicate<? super T> predicate)
      Splits this Traversable into two partitions according to a predicate.
      Seq<T> patch​(int from, @NonNull java.lang.Iterable<? extends T> that, int replaced)
      Returns a new sequence in which a slice of elements in this sequence is replaced by the elements of another sequence.
      Seq<T> peek​(@NonNull java.util.function.Consumer<? super T> action)
      Performs the given action on the first element if this is an eager implementation.
      Seq<? extends Seq<T>> permutations()
      Returns all unique permutations of this sequence.
      default int prefixLength​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns the length of the longest prefix of this sequence whose elements all satisfy the given predicate.
      Seq<T> prepend​(T element)
      Returns a new sequence with the given element prepended to this sequence.
      Seq<T> prependAll​(@NonNull java.lang.Iterable<? extends T> elements)
      Returns a new sequence with all given elements prepended to this sequence.
      Seq<T> reject​(@NonNull java.util.function.Predicate<? super T> predicate)
      Returns a new traversable containing only the elements that do not satisfy the given predicate.
      Seq<T> remove​(T element)
      Returns a new sequence with the first occurrence of the given element removed.
      Seq<T> removeAll​(@NonNull java.lang.Iterable<? extends T> elements)
      Returns a new sequence with all occurrences of the given elements removed.
      Seq<T> removeAll​(@NonNull java.util.function.Predicate<? super T> predicate)
      Deprecated.
      Seq<T> removeAll​(T element)
      Returns a new sequence with all occurrences of the given element removed.
      Seq<T> removeAt​(int index)
      Returns a new sequence with the element at the specified position removed.
      Seq<T> removeFirst​(@NonNull java.util.function.Predicate<T> predicate)
      Returns a new sequence with the first element that satisfies the given predicate removed.
      Seq<T> removeLast​(@NonNull java.util.function.Predicate<T> predicate)
      Returns a new sequence with the last element that satisfies the given predicate removed.
      Seq<T> replace​(T currentElement, T newElement)
      Replaces the first occurrence of currentElement with newElement, if it exists.
      Seq<T> replaceAll​(T currentElement, T newElement)
      Replaces all occurrences of currentElement with newElement.
      Seq<T> retainAll​(@NonNull java.lang.Iterable<? extends T> elements)
      Retains only the elements from this Traversable that are contained in the given elements.
      Seq<T> reverse()
      Returns a new sequence with the order of elements reversed.
      Iterator<T> reverseIterator()
      Returns an iterator that yields elements of this sequence in reversed order.
      Seq<T> rotateLeft​(int n)
      Returns a new sequence with the elements circularly rotated to the left by the specified distance.
      Seq<T> rotateRight​(int n)
      Returns a new sequence with the elements circularly rotated to the right by the specified distance.
      Seq<T> scan​(T zero, @NonNull java.util.function.BiFunction<? super T,​? super T,​? extends T> operation)
      Computes a prefix scan of the elements of this Traversable.
      <U> Seq<U> scanLeft​(U zero, @NonNull java.util.function.BiFunction<? super U,​? super T,​? extends U> operation)
      Produces a collection containing cumulative results of applying the operator from left to right.
      <U> Seq<U> scanRight​(U zero, @NonNull java.util.function.BiFunction<? super T,​? super U,​? extends U> operation)
      Produces a collection containing cumulative results of applying the operator from right to left.
      int search​(T element)
      Searches for a specified element in this sequence, which must be sorted in ascending natural order.
      int search​(T element, @NonNull java.util.Comparator<? super T> comparator)
      Searches for a specified element in this sequence, which must be sorted according to the given comparator.
      int segmentLength​(@NonNull java.util.function.Predicate<? super T> predicate, int from)
      Returns the length of the longest contiguous segment, starting from the specified index, in which all elements satisfy the given predicate.
      Seq<T> shuffle()
      Returns a new sequence with the elements randomly shuffled.
      Seq<T> slice​(int beginIndex, int endIndex)
      Returns a subsequence (slice) of this sequence, starting at beginIndex (inclusive) and ending at endIndex (exclusive).
      Iterator<? extends Seq<T>> slideBy​(@NonNull java.util.function.Function<? super T,​?> classifier)
      Partitions this Traversable into consecutive non-overlapping windows according to a classification function.
      Iterator<? extends Seq<T>> sliding​(int size)
      Slides a window of a given size over this Traversable with a step size of 1.
      Iterator<? extends Seq<T>> sliding​(int size, int step)
      Slides a window of a specific size with a given step over this Traversable.
      <U> Seq<T> sortBy​(@NonNull java.util.Comparator<? super U> comparator, java.util.function.Function<? super T,​? extends U> mapper)
      Returns a new sequence sorted by comparing elements in a different domain defined by the given mapper, using the provided comparator.
      <U extends java.lang.Comparable<? super U>>
      Seq<T>
      sortBy​(@NonNull java.util.function.Function<? super T,​? extends U> mapper)
      Returns a new sequence sorted by comparing elements in a different domain defined by the given mapper.
      Seq<T> sorted()
      Returns a new sequence with elements sorted according to their natural order.
      Seq<T> sorted​(@NonNull java.util.Comparator<? super T> comparator)
      Returns a new sequence with elements sorted according to the given Comparator.
      Tuple2<? extends Seq<T>,​? extends Seq<T>> span​(@NonNull java.util.function.Predicate<? super T> predicate)
      Splits this Traversable into a prefix and remainder according to the given predicate.
      Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAt​(int n)
      Splits this sequence at the specified index.
      Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAt​(@NonNull java.util.function.Predicate<? super T> predicate)
      Splits this sequence at the first element satisfying the given predicate.
      Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAtInclusive​(@NonNull java.util.function.Predicate<? super T> predicate)
      Splits this sequence at the first element satisfying the given predicate, including the element in the first part.
      default boolean startsWith​(@NonNull java.lang.Iterable<? extends T> that)
      Tests whether this sequence starts with the given sequence.
      default boolean startsWith​(@NonNull java.lang.Iterable<? extends T> that, int offset)
      Tests whether this sequence contains the given sequence starting at the specified index.
      Seq<T> subSequence​(int beginIndex)
      Returns a Seq that is a subsequence of this sequence, starting from the specified beginIndex and extending to the end of this sequence.
      Seq<T> subSequence​(int beginIndex, int endIndex)
      Returns a Seq that is a subsequence of this sequence, starting from the specified beginIndex (inclusive) and ending at endIndex (exclusive).
      Seq<T> tail()
      Returns a new Traversable without its first element.
      Option<? extends Seq<T>> tailOption()
      Returns a new Traversable without its first element as an Option.
      Seq<T> take​(int n)
      Returns the first n elements of this Traversable, or all elements if n exceeds the length.
      Seq<T> takeRight​(int n)
      Returns the last n elements of this Traversable, or all elements if n exceeds the length.
      Seq<T> takeRightUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
      Takes elements from the end of the sequence until an element satisfies the given predicate.
      Seq<T> takeRightWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
      Takes elements from the end of the sequence while the given predicate holds.
      Seq<T> takeUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
      Takes elements from this Traversable until the given predicate holds for an element.
      Seq<T> takeWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
      Takes elements from this Traversable while the given predicate holds.
      <T1,​T2>
      Tuple2<? extends Seq<T1>,​? extends Seq<T2>>
      unzip​(@NonNull java.util.function.Function<? super T,​Tuple2<? extends T1,​? extends T2>> unzipper)
      Unzips the elements of this Traversable by mapping each element to a pair and splitting them into two separate Traversable collections.
      <T1,​T2,​T3>
      Tuple3<? extends Seq<T1>,​? extends Seq<T2>,​? extends Seq<T3>>
      unzip3​(@NonNull java.util.function.Function<? super T,​Tuple3<? extends T1,​? extends T2,​? extends T3>> unzipper)
      Unzips the elements of this Traversable by mapping each element to a triple and splitting them into three separate Traversable collections.
      Seq<T> update​(int index, @NonNull java.util.function.Function<? super T,​? extends T> updater)
      Returns a new Seq with the element at the specified index updated using the given function.
      Seq<T> update​(int index, T element)
      Returns a new Seq with the element at the specified index replaced by the given value.
      default Function1<java.lang.Integer,​T> withDefault​(@NonNull java.util.function.Function<? super java.lang.Integer,​? extends T> defaultFunction)
      Deprecated.
      Will be removed
      default Function1<java.lang.Integer,​T> withDefaultValue​(T defaultValue)
      Deprecated.
      Will be removed
      <U> Seq<Tuple2<T,​U>> zip​(@NonNull java.lang.Iterable<? extends U> that)
      Returns a Traversable formed by pairing elements of this Traversable with elements of another Iterable.
      <U> Seq<Tuple2<T,​U>> zipAll​(@NonNull java.lang.Iterable<? extends U> that, T thisElem, U thatElem)
      Returns a Traversable formed by pairing elements of this Traversable with elements of another Iterable, filling in placeholder elements when one collection is shorter than the other.
      <U,​R>
      Seq<R>
      zipWith​(@NonNull java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,​? super U,​? extends R> mapper)
      Returns a Traversable by combining elements of this Traversable with elements of another Iterable using a mapping function.
      Seq<Tuple2<T,​java.lang.Integer>> zipWithIndex()
      Zips this Traversable with its indices, starting at 0.
      <U> Seq<U> zipWithIndex​(@NonNull java.util.function.BiFunction<? super T,​? super java.lang.Integer,​? extends U> mapper)
      Zips this Traversable with its indices and maps the resulting pairs using the provided mapper.
    • Field Detail

      • serialVersionUID

        static final long serialVersionUID
        The serial version UID for serialization.
        See Also:
        Constant Field Values
    • Method Detail

      • narrow

        static <T> Seq<T> narrow​(Seq<? extends T> seq)
        Narrows a Seq<? extends T> to Seq<T> via a safe unchecked cast. This is valid because Seq is immutable and thus covariant in its element type.
        Type Parameters:
        T - the element type of the resulting sequence
        Parameters:
        seq - the sequence to narrow
        Returns:
        the given sequence viewed as Seq<T>
      • append

        Seq<T> append​(T element)
        Returns a new sequence with the given element appended at the end.
        Parameters:
        element - the element to append
        Returns:
        a new Seq containing all elements of this sequence followed by the given element
      • appendAll

        Seq<T> appendAll​(@NonNull java.lang.Iterable<? extends T> elements)
        Returns a new sequence with all elements from the given Iterable appended at the end of this sequence.
        Parameters:
        elements - the elements to append; must not be null
        Returns:
        a new Seq containing all elements of this sequence followed by the given elements
        Throws:
        java.lang.NullPointerException - if elements is null
      • apply

        @Deprecated
        default T apply​(java.lang.Integer index)
        Deprecated.
        Will be removed
        A Seq is a partial function which returns the element at the specified index by calling get(int).
        Specified by:
        apply in interface java.util.function.Function<java.lang.Integer,​T>
        Specified by:
        apply in interface Function1<java.lang.Integer,​T>
        Specified by:
        apply in interface PartialFunction<java.lang.Integer,​T>
        Parameters:
        index - an index
        Returns:
        the element at the given index
        Throws:
        java.lang.IndexOutOfBoundsException - if this is empty, index < 0 or index >= length()
      • asJava

        @GwtIncompatible
        java.util.List<T> asJava()
        Returns an immutable List view of this Seq. Any attempt to modify the view (e.g., via mutator methods) will throw UnsupportedOperationException at runtime.

        This is a view, not a copy. Compared to conversion methods like toJava*():

        • Creating the view is O(1) (constant time), whereas conversions take O(n), with n = collection size.
        • Operations on the view have the same performance characteristics as the underlying persistent Vavr collection, while converted collections behave like standard Java collections.

        Note: the immutable Java list view throws UnsupportedOperationException before checking method arguments, which may differ from standard Java behavior.

        Returns:
        an immutable List view of this sequence
      • asJava

        @GwtIncompatible
        Seq<T> asJava​(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
        Creates an immutable List view of this Seq and passes it to the given action.

        The view is immutable: any attempt to modify it will throw UnsupportedOperationException at runtime.

        Parameters:
        action - a side-effecting operation that receives an immutable java.util.List view
        Returns:
        this sequence
        See Also:
        asJava()
      • asJavaMutable

        @GwtIncompatible
        java.util.List<T> asJavaMutable()
        Returns a mutable List view of this Seq. All standard mutator methods of the List interface are supported and modify the underlying view.

        Unlike asJava(), this view allows modifications, but the performance characteristics correspond to the underlying persistent Vavr collection.

        Returns:
        a mutable List view of this sequence
        See Also:
        asJava()
      • asJavaMutable

        @GwtIncompatible
        Seq<T> asJavaMutable​(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
        Creates a mutable List view of this Seq and passes it to the given action.

        The view supports all standard mutator methods. The result of the action determines what is returned:

        • If only read operations are performed, this instance is returned.
        • If any write operations are performed, a new Seq reflecting those changes is returned.
        Parameters:
        action - a side-effecting operation that receives a mutable java.util.List view
        Returns:
        this sequence or a new sequence reflecting modifications made through the view
        See Also:
        asJavaMutable()
      • asPartialFunction

        PartialFunction<java.lang.Integer,​T> asPartialFunction()
                                                              throws java.lang.IndexOutOfBoundsException
        Returns a PartialFunction view of this Seq, where the function is defined at an index if this sequence contains at least index + 1 elements. Applying the partial function to a defined index returns the element at that index.
        Returns:
        a PartialFunction mapping indices to elements
        Throws:
        java.lang.IndexOutOfBoundsException - if the sequence is empty, or if index < 0 or index >= length()
      • collect

        <R> Seq<R> collect​(@NonNull PartialFunction<? super T,​? extends R> partialFunction)
        Description copied from interface: Traversable
        Applies a PartialFunction to all elements that are defined for it and collects the results.

        For each element in iteration order, the function is first tested:

        
         partialFunction.isDefinedAt(element)
         
        If true, the element is mapped to type R:
        
         R newElement = partialFunction.apply(element)
         

        Note: If this Traversable is ordered (i.e., extends Ordered), the caller must ensure that the resulting elements are comparable (i.e., implement Comparable).

        Specified by:
        collect in interface Traversable<T>
        Type Parameters:
        R - the type of elements in the resulting Traversable
        Parameters:
        partialFunction - a function that may not be defined for all elements of this traversable
        Returns:
        a new Traversable containing the results of applying the partial function
      • combinations

        Seq<? extends Seq<T>> combinations()
        Returns a sequence containing all combinations of elements from this sequence, for all sizes from 0 to length().

        Examples:

         
         [].combinations() = [[]]
        
         [1,2,3].combinations() = [
           [],                  // k = 0
           [1], [2], [3],       // k = 1
           [1,2], [1,3], [2,3], // k = 2
           [1,2,3]              // k = 3
         ]
         
         
        Returns:
        a sequence of sequences representing all combinations of this sequence's elements
      • combinations

        Seq<? extends Seq<T>> combinations​(int k)
        Returns all subsets of this sequence containing exactly k distinct elements, i.e., the k-combinations of this sequence.
        Parameters:
        k - the size of each subset
        Returns:
        a sequence of sequences representing all k-element combinations
        See Also:
        Combination
      • containsSlice

        default boolean containsSlice​(@NonNull java.lang.Iterable<? extends T> that)
        Checks whether this sequence contains the given sequence as a contiguous slice.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        Returns:
        true if this sequence contains a slice equal to that, false otherwise
        Throws:
        java.lang.NullPointerException - if that is null
      • crossProduct

        default Iterator<Tuple2<T,​T>> crossProduct()
        Computes the Cartesian product of this sequence with itself, producing all pairs of elements (this × this).

        Example:

        
         // Result: [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
         List.of(1, 2, 3).crossProduct();
         
        Returns:
        an Iterator over all pairs in the Cartesian square of this sequence
      • crossProduct

        Iterator<? extends Seq<T>> crossProduct​(int power)
        Returns the n-ary Cartesian power (cross product) of this sequence. Each element of the resulting iterator is a sequence of length power, containing all possible combinations of elements from this sequence.

        Example for power = 2:

        
         // Result: [(A,A), (A,B), (A,C), ..., (B,A), (B,B), ..., (Z,Y), (Z,Z)]
         CharSeq.rangeClosed('A', 'Z').crossProduct(2);
         

        If power is negative, the result is an empty iterator:

        
         // Result: ()
         CharSeq.rangeClosed('A', 'Z').crossProduct(-1);
         
        Parameters:
        power - the number of Cartesian multiplications
        Returns:
        an Iterator over sequences representing the Cartesian power of this sequence
      • crossProduct

        default <U> Iterator<Tuple2<T,​U>> crossProduct​(@NonNull java.lang.Iterable<? extends U> that)
        Computes the Cartesian product of this sequence with another iterable, producing pairs of elements (this × that).

        Example:

        
         // Result: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b')]
         List.of(1, 2, 3).crossProduct(List.of('a', 'b'));
         
        Type Parameters:
        U - the element type of the other iterable
        Parameters:
        that - another iterable; must not be null
        Returns:
        an Iterator over all pairs from this sequence and that
        Throws:
        java.lang.NullPointerException - if that is null
      • endsWith

        default boolean endsWith​(@NonNull Seq<? extends T> that)
        Checks whether this sequence ends with the given sequence.

        Note: If both this sequence and that are infinite, this method may not terminate.

        Parameters:
        that - the sequence to check as a suffix; must not be null
        Returns:
        true if this sequence ends with that, false otherwise
        Throws:
        java.lang.NullPointerException - if that is null
      • get

        T get​(int index)
        Returns the element at the specified index.
        Parameters:
        index - the position of the element to retrieve
        Returns:
        the element at the given index
        Throws:
        java.lang.IndexOutOfBoundsException - if the sequence is empty, or if index < 0 or index >= length()
      • indexOf

        default int indexOf​(T element)
        Returns the index of the first occurrence of the given element, or -1 if this sequence does not contain the element.
        Parameters:
        element - the element to search for
        Returns:
        the index of the first occurrence, or -1 if not found
      • indexOfOption

        default Option<java.lang.Integer> indexOfOption​(T element)
        Returns the index of the first occurrence of the given element as an Option.
        Parameters:
        element - the element to search for
        Returns:
        Some(index) if the element is found, or None if not found
      • indexOf

        int indexOf​(T element,
                    int from)
        Returns the index of the first occurrence of the given element, starting at the specified index, or -1 if this sequence does not contain the element.
        Parameters:
        element - the element to search for
        from - the starting index for the search
        Returns:
        the index of the first occurrence at or after from, or -1 if not found
      • indexOfOption

        default Option<java.lang.Integer> indexOfOption​(T element,
                                                        int from)
        Returns the index of the first occurrence of the given element at or after the specified start index, as an Option.
        Parameters:
        element - the element to search for
        from - the starting index for the search
        Returns:
        Some(index) if the element is found, or None if not found
      • indexOfSlice

        default int indexOfSlice​(@NonNull java.lang.Iterable<? extends T> that)
        Returns the first index at which this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        Returns:
        the starting index of the first matching slice, or -1 if not found
        Throws:
        java.lang.NullPointerException - if that is null
      • indexOfSliceOption

        default Option<java.lang.Integer> indexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that)
        Returns the first index at which this sequence contains the given sequence as a contiguous slice, wrapped in an Option.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        Returns:
        Some(index) if a matching slice is found, or None if not found
      • indexOfSlice

        int indexOfSlice​(@NonNull java.lang.Iterable<? extends T> that,
                         int from)
        Returns the first index at or after the specified start index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        from - the starting index for the search
        Returns:
        the starting index of the first matching slice at or after from, or -1 if not found
        Throws:
        java.lang.NullPointerException - if that is null
      • indexOfSliceOption

        default Option<java.lang.Integer> indexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that,
                                                             int from)
        Returns the first index at or after the specified start index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        from - the starting index for the search
        Returns:
        Some(index) if a matching slice is found, or None if not found
      • indexWhere

        default int indexWhere​(@NonNull java.util.function.Predicate<? super T> predicate)
        Returns the index of the first element in this sequence that satisfies the given predicate, or -1 if no such element exists.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        Returns:
        the index of the first matching element, or -1 if none exists
      • indexWhereOption

        default Option<java.lang.Integer> indexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate)
        Returns the index of the first element satisfying the given predicate as an Option.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        Returns:
        Some(index) if a matching element exists, or None if not found
      • indexWhere

        int indexWhere​(@NonNull java.util.function.Predicate<? super T> predicate,
                       int from)
        Returns the index of the first element at or after the specified start index that satisfies the given predicate, or -1 if no such element exists.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        from - the starting index for the search
        Returns:
        the index >= from of the first matching element, or -1 if none exists
      • indexWhereOption

        default Option<java.lang.Integer> indexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate,
                                                           int from)
        Returns the index of the first element at or after the specified start index that satisfies the given predicate as an Option.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        from - the starting index for the search
        Returns:
        Some(index) if a matching element exists, or None if not found
      • insert

        Seq<T> insert​(int index,
                      T element)
        Returns a new sequence with the given element inserted at the specified index.
        Parameters:
        index - the position at which to insert the element
        element - the element to insert
        Returns:
        a new Seq with the element inserted
        Throws:
        java.lang.IndexOutOfBoundsException - if the sequence is empty, or if index < 0 or index >= length()
      • insertAll

        Seq<T> insertAll​(int index,
                         @NonNull java.lang.Iterable<? extends T> elements)
        Returns a new sequence with the given elements inserted at the specified index.
        Parameters:
        index - the position at which to insert the elements
        elements - the elements to insert; must not be null
        Returns:
        a new Seq with the elements inserted
        Throws:
        java.lang.IndexOutOfBoundsException - if the sequence is empty, or if index < 0 or index >= length()
      • intersperse

        Seq<T> intersperse​(T element)
        Returns a new sequence where the given element is inserted between all elements of this sequence.
        Parameters:
        element - the element to intersperse
        Returns:
        a new Seq with the element interspersed
      • iterator

        default Iterator<T> iterator​(int index)
        Returns an iterator over the elements of this sequence starting at the specified index. Equivalent to this.subSequence(index).iterator().
        Parameters:
        index - the starting index
        Returns:
        an Iterator beginning at the given index, or empty if index == length()
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 or index > length()
      • lastIndexOf

        default int lastIndexOf​(T element)
        Returns the index of the last occurrence of the given element, or -1 if this sequence does not contain the element.
        Parameters:
        element - the element to search for
        Returns:
        the index of the last occurrence, or -1 if not found
      • lastIndexOfOption

        default Option<java.lang.Integer> lastIndexOfOption​(T element)
        Returns the index of the last occurrence of the given element as an Option.
        Parameters:
        element - the element to search for
        Returns:
        Some(index) if found, or None if not found
      • lastIndexWhere

        default int lastIndexWhere​(@NonNull java.util.function.Predicate<? super T> predicate)
        Returns the index of the last element in this sequence that satisfies the given predicate, or -1 if no such element exists.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        Returns:
        the index of the last matching element, or -1 if none exists
      • lastIndexWhereOption

        default Option<java.lang.Integer> lastIndexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate)
        Returns the index of the last element satisfying the given predicate as an Option.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        Returns:
        Some(index) if a matching element exists, or None if not found
      • lastIndexWhere

        int lastIndexWhere​(@NonNull java.util.function.Predicate<? super T> predicate,
                           int end)
        Returns the index of the last element at or before the specified end index that satisfies the given predicate, or -1 if no such element exists.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        end - the maximum index to consider
        Returns:
        the index <= end of the last matching element, or -1 if none exists
      • lastIndexWhereOption

        default Option<java.lang.Integer> lastIndexWhereOption​(@NonNull java.util.function.Predicate<? super T> predicate,
                                                               int end)
        Returns the index of the last element at or before the specified end index that satisfies the given predicate, wrapped in an Option.
        Parameters:
        predicate - the predicate used to test elements; must not be null
        end - the maximum index to consider
        Returns:
        Some(index) if a matching element exists, or None if not found
      • lift

        @Deprecated
        default Function1<java.lang.Integer,​Option<T>> lift()
        Deprecated.
        Will be removed
        Turns this sequence into a plain function returning an Option result.
        Specified by:
        lift in interface PartialFunction<java.lang.Integer,​T>
        Returns:
        a function that takes an index i and returns the value of this sequence in a Some if the index is within bounds, otherwise a None.
      • lastIndexOf

        int lastIndexOf​(T element,
                        int end)
        Returns the index of the last occurrence of the given element at or before the specified end index, or -1 if this sequence does not contain the element.
        Parameters:
        element - the element to search for
        end - the maximum index to consider
        Returns:
        the index of the last occurrence at or before end, or -1 if not found
      • lastIndexOfOption

        default Option<java.lang.Integer> lastIndexOfOption​(T element,
                                                            int end)
        Returns the index of the last occurrence of the given element at or before the specified end index as an Option.
        Parameters:
        element - the element to search for
        end - the maximum index to consider
        Returns:
        Some(index) if found, or None if not found
      • lastIndexOfSlice

        default int lastIndexOfSlice​(@NonNull java.lang.Iterable<? extends T> that)
        Returns the last index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.

        Note: This method will not terminate for infinite sequences.

        Parameters:
        that - the sequence to search for; must not be null
        Returns:
        the starting index of the last matching slice, or -1 if not found
        Throws:
        java.lang.NullPointerException - if that is null
      • lastIndexOfSliceOption

        default Option<java.lang.Integer> lastIndexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that)
        Returns the last index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
        Parameters:
        that - the sequence to search for; must not be null
        Returns:
        Some(index) if a matching slice exists, or None if not found
      • lastIndexOfSlice

        int lastIndexOfSlice​(@NonNull java.lang.Iterable<? extends T> that,
                             int end)
        Returns the last index at or before the specified end index where this sequence contains the given sequence as a contiguous slice, or -1 if no such slice exists.
        Parameters:
        that - the sequence to search for; must not be null
        end - the maximum index to consider
        Returns:
        the last index <= end where the slice starts, or -1 if not found
        Throws:
        java.lang.NullPointerException - if that is null
      • lastIndexOfSliceOption

        default Option<java.lang.Integer> lastIndexOfSliceOption​(@NonNull java.lang.Iterable<? extends T> that,
                                                                 int end)
        Returns the last index at or before the specified end index where this sequence contains the given sequence as a contiguous slice, wrapped in an Option.
        Parameters:
        that - the sequence to search for; must not be null
        end - the maximum index to consider
        Returns:
        Some(index) if a matching slice exists, or None if not found
      • padTo

        Seq<T> padTo​(int length,
                     T element)
        Returns a new sequence with this sequence padded on the right with the given element until the specified target length is reached.

        Note: Lazily-evaluated sequences may need to process all elements to determine the overall length.

        Parameters:
        length - the target length of the resulting sequence
        element - the element to append as padding
        Returns:
        a new Seq consisting of this sequence followed by the minimal number of occurrences of element to reach at least length
      • leftPadTo

        Seq<T> leftPadTo​(int length,
                         T element)
        Returns a new sequence with this sequence padded on the left with the given element until the specified target length is reached.

        Note: Lazily-evaluated sequences may need to process all elements to determine the overall length.

        Parameters:
        length - the target length of the resulting sequence
        element - the element to prepend as padding
        Returns:
        a new Seq consisting of this sequence prepended by the minimal number of occurrences of element to reach at least length
      • patch

        Seq<T> patch​(int from,
                     @NonNull java.lang.Iterable<? extends T> that,
                     int replaced)
        Returns a new sequence in which a slice of elements in this sequence is replaced by the elements of another sequence.
        Parameters:
        from - the starting index of the slice to be replaced
        that - the sequence of elements to insert; must not be null
        replaced - the number of elements to remove from this sequence starting at from
        Returns:
        a new Seq with the specified slice replaced
      • permutations

        Seq<? extends Seq<T>> permutations()
        Returns all unique permutations of this sequence.

        Example:

        
         [].permutations() = []
        
         [1, 2, 3].permutations() = [
           [1, 2, 3],
           [1, 3, 2],
           [2, 1, 3],
           [2, 3, 1],
           [3, 1, 2],
           [3, 2, 1]
         ]
         
        Returns:
        a sequence of all unique permutations of this sequence
      • prefixLength

        default int prefixLength​(@NonNull java.util.function.Predicate<? super T> predicate)
        Returns the length of the longest prefix of this sequence whose elements all satisfy the given predicate.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        predicate - the predicate to test elements; must not be null
        Returns:
        the length of the longest prefix in which every element satisfies predicate
      • prepend

        Seq<T> prepend​(T element)
        Returns a new sequence with the given element prepended to this sequence.
        Parameters:
        element - the element to prepend
        Returns:
        a new Seq with the element added at the front
      • prependAll

        Seq<T> prependAll​(@NonNull java.lang.Iterable<? extends T> elements)
        Returns a new sequence with all given elements prepended to this sequence.
        Parameters:
        elements - the elements to prepend; must not be null
        Returns:
        a new Seq with the elements added at the front
      • remove

        Seq<T> remove​(T element)
        Returns a new sequence with the first occurrence of the given element removed.
        Parameters:
        element - the element to remove
        Returns:
        a new Seq without the first occurrence of the element
      • removeAll

        Seq<T> removeAll​(T element)
        Returns a new sequence with all occurrences of the given element removed.
        Parameters:
        element - the element to remove
        Returns:
        a new Seq without any occurrences of the element
      • removeAll

        Seq<T> removeAll​(@NonNull java.lang.Iterable<? extends T> elements)
        Returns a new sequence with all occurrences of the given elements removed.
        Parameters:
        elements - the elements to remove; must not be null
        Returns:
        a new Seq without any of the given elements
        Throws:
        java.lang.NullPointerException - if elements is null
      • removeAll

        @Deprecated
        Seq<T> removeAll​(@NonNull java.util.function.Predicate<? super T> predicate)
        Deprecated.
        Returns a new Seq consisting of all elements which do not satisfy the given predicate.
        Parameters:
        predicate - the predicate used to test elements
        Returns:
        a new Seq
        Throws:
        java.lang.NullPointerException - if predicate is null
      • removeAt

        Seq<T> removeAt​(int index)
        Returns a new sequence with the element at the specified position removed. Subsequent elements are shifted to the left (indices decreased by one).
        Parameters:
        index - the position of the element to remove
        Returns:
        a new Seq without the element at the specified index
        Throws:
        java.lang.IndexOutOfBoundsException - if the sequence is empty, or if index < 0 or index >= length()
      • removeFirst

        Seq<T> removeFirst​(@NonNull java.util.function.Predicate<T> predicate)
        Returns a new sequence with the first element that satisfies the given predicate removed.
        Parameters:
        predicate - the predicate used to identify the element to remove; must not be null
        Returns:
        a new Seq without the first matching element
      • removeLast

        Seq<T> removeLast​(@NonNull java.util.function.Predicate<T> predicate)
        Returns a new sequence with the last element that satisfies the given predicate removed.
        Parameters:
        predicate - the predicate used to identify the element to remove; must not be null
        Returns:
        a new Seq without the last matching element
      • reverse

        Seq<T> reverse()
        Returns a new sequence with the order of elements reversed.
        Returns:
        a new Seq with elements in reversed order
      • reverseIterator

        Iterator<T> reverseIterator()
        Returns an iterator that yields elements of this sequence in reversed order.

        Note: xs.reverseIterator() is equivalent to xs.reverse().iterator() but may be more efficient.

        Returns:
        an Iterator over the elements in reversed order
      • rotateLeft

        Seq<T> rotateLeft​(int n)
        Returns a new sequence with the elements circularly rotated to the left by the specified distance.

        Example:

        
         // Result: List(3, 4, 5, 1, 2)
         List.of(1, 2, 3, 4, 5).rotateLeft(2);
         
        Parameters:
        n - the number of positions to rotate left
        Returns:
        a new Seq with elements rotated left
      • rotateRight

        Seq<T> rotateRight​(int n)
        Returns a new sequence with the elements circularly rotated to the right by the specified distance.

        Example:

        
         // Result: List(4, 5, 1, 2, 3)
         List.of(1, 2, 3, 4, 5).rotateRight(2);
         
        Parameters:
        n - the number of positions to rotate right
        Returns:
        a new Seq with elements rotated right
      • segmentLength

        int segmentLength​(@NonNull java.util.function.Predicate<? super T> predicate,
                          int from)
        Returns the length of the longest contiguous segment, starting from the specified index, in which all elements satisfy the given predicate.

        Note: This method may not terminate for infinite sequences.

        Parameters:
        predicate - the predicate used to test elements; must not be null
        from - the index at which to start the search
        Returns:
        the length of the longest segment starting at from where every element satisfies predicate
      • shuffle

        Seq<T> shuffle()
        Returns a new sequence with the elements randomly shuffled.
        Returns:
        a new Seq containing the same elements in a random order
      • slice

        Seq<T> slice​(int beginIndex,
                     int endIndex)
        Returns a subsequence (slice) of this sequence, starting at beginIndex (inclusive) and ending at endIndex (exclusive).

        Examples:

        
         List.of(1, 2, 3, 4).slice(1, 3); // = (2, 3)
         List.of(1, 2, 3, 4).slice(0, 4); // = (1, 2, 3, 4)
         List.of(1, 2, 3, 4).slice(2, 2); // = ()
         List.of(1, 2).slice(1, 0);       // = ()
         List.of(1, 2).slice(-10, 10);    // = (1, 2)
         

        See also subSequence(int, int), which may throw an exception instead of returning a sequence in some cases.

        Parameters:
        beginIndex - the starting index (inclusive)
        endIndex - the ending index (exclusive)
        Returns:
        a new Seq representing the specified slice
      • sorted

        Seq<T> sorted()
        Returns a new sequence with elements sorted according to their natural order.
        Returns:
        a new Seq with elements in natural order
        Throws:
        java.lang.ClassCastException - if elements are not Comparable
      • sorted

        Seq<T> sorted​(@NonNull java.util.Comparator<? super T> comparator)
        Returns a new sequence with elements sorted according to the given Comparator.
        Parameters:
        comparator - the comparator used to order elements; must not be null
        Returns:
        a new Seq with elements sorted according to the comparator
      • sortBy

        <U extends java.lang.Comparable<? super U>> Seq<T> sortBy​(@NonNull java.util.function.Function<? super T,​? extends U> mapper)
        Returns a new sequence sorted by comparing elements in a different domain defined by the given mapper.
        Type Parameters:
        U - the type used for comparison
        Parameters:
        mapper - a function mapping elements to a Comparable domain; must not be null
        Returns:
        a new Seq sorted according to the mapped values
        Throws:
        java.lang.NullPointerException - if mapper is null
      • sortBy

        <U> Seq<T> sortBy​(@NonNull java.util.Comparator<? super U> comparator,
                          java.util.function.Function<? super T,​? extends U> mapper)
        Returns a new sequence sorted by comparing elements in a different domain defined by the given mapper, using the provided comparator.
        Type Parameters:
        U - the type used for comparison
        Parameters:
        comparator - the comparator used to compare mapped values; must not be null
        mapper - a function mapping elements to the domain for comparison; must not be null
        Returns:
        a new Seq sorted according to the mapped values and comparator
        Throws:
        java.lang.NullPointerException - if comparator or mapper is null
      • splitAt

        Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAt​(int n)
        Splits this sequence at the specified index.

        The result of splitAt(n) is equivalent to Tuple.of(take(n), drop(n)).

        Parameters:
        n - the index at which to split
        Returns:
        a Tuple2 containing the first n elements and the remaining elements
      • splitAt

        Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAt​(@NonNull java.util.function.Predicate<? super T> predicate)
        Splits this sequence at the first element satisfying the given predicate.
        Parameters:
        predicate - the predicate used to determine the split point; must not be null
        Returns:
        a Tuple2 containing the sequence before the first matching element and the remaining sequence
      • splitAtInclusive

        Tuple2<? extends Seq<T>,​? extends Seq<T>> splitAtInclusive​(@NonNull java.util.function.Predicate<? super T> predicate)
        Splits this sequence at the first element satisfying the given predicate, including the element in the first part.
        Parameters:
        predicate - the predicate used to determine the split point; must not be null
        Returns:
        a Tuple2 containing the sequence up to and including the first matching element and the remaining sequence
      • startsWith

        default boolean startsWith​(@NonNull java.lang.Iterable<? extends T> that)
        Tests whether this sequence starts with the given sequence.
        Parameters:
        that - the sequence to test; must not be null
        Returns:
        true if that is empty or is a prefix of this sequence, false otherwise
      • startsWith

        default boolean startsWith​(@NonNull java.lang.Iterable<? extends T> that,
                                   int offset)
        Tests whether this sequence contains the given sequence starting at the specified index.

        Note: If both this sequence and the argument sequence are infinite, this method may not terminate.

        Parameters:
        that - the sequence to test; must not be null
        offset - the index at which to start checking for the prefix
        Returns:
        true if that is empty or matches a subsequence of this sequence starting at offset, false otherwise
      • subSequence

        Seq<T> subSequence​(int beginIndex)
        Returns a Seq that is a subsequence of this sequence, starting from the specified beginIndex and extending to the end of this sequence.

        Examples:

        
         List.of(1, 2).subSequence(0);   // = (1, 2)
         List.of(1, 2).subSequence(1);   // = (2)
         List.of(1, 2).subSequence(2);   // = ()
         List.of(1, 2).subSequence(10);  // throws IndexOutOfBoundsException
         List.of(1, 2).subSequence(-10); // throws IndexOutOfBoundsException
         

        See also drop(int), which provides similar functionality but does not throw an exception for out-of-bounds indices.

        Parameters:
        beginIndex - the starting index (inclusive) of the subsequence
        Returns:
        a new Seq representing the subsequence from beginIndex to the end
        Throws:
        java.lang.IndexOutOfBoundsException - if beginIndex is negative or greater than the length of this sequence
      • subSequence

        Seq<T> subSequence​(int beginIndex,
                           int endIndex)
        Returns a Seq that is a subsequence of this sequence, starting from the specified beginIndex (inclusive) and ending at endIndex (exclusive).

        Examples:

        
         List.of(1, 2, 3, 4).subSequence(1, 3); // = (2, 3)
         List.of(1, 2, 3, 4).subSequence(0, 4); // = (1, 2, 3, 4)
         List.of(1, 2, 3, 4).subSequence(2, 2); // = ()
         List.of(1, 2).subSequence(1, 0);       // throws IndexOutOfBoundsException
         List.of(1, 2).subSequence(-10, 1);     // throws IndexOutOfBoundsException
         List.of(1, 2).subSequence(0, 10);      // throws IndexOutOfBoundsException
         

        See also slice(int, int), which returns an empty sequence instead of throwing exceptions when indices are out of range.

        Parameters:
        beginIndex - the starting index (inclusive) of the subsequence
        endIndex - the ending index (exclusive) of the subsequence
        Returns:
        a new Seq representing the subsequence from beginIndex to endIndex - 1
        Throws:
        java.lang.IndexOutOfBoundsException - if beginIndex or endIndex is negative, or if endIndex is greater than length()
        java.lang.IllegalArgumentException - if beginIndex is greater than endIndex
      • update

        Seq<T> update​(int index,
                      T element)
        Returns a new Seq with the element at the specified index replaced by the given value.
        Parameters:
        index - the index of the element to update
        element - the new element to set at the specified index
        Returns:
        a new Seq with the updated element
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length()
      • update

        Seq<T> update​(int index,
                      @NonNull java.util.function.Function<? super T,​? extends T> updater)
        Returns a new Seq with the element at the specified index updated using the given function.
        Parameters:
        index - the index of the element to update
        updater - a function that computes the new element from the existing element
        Returns:
        a new Seq with the element at index transformed by updater
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length()
        java.lang.NullPointerException - if updater is null
      • search

        int search​(T element)
        Searches for a specified element in this sequence, which must be sorted in ascending natural order.

        If the sequence is an IndexedSeq, a binary search is used; otherwise, a linear search is performed.

        Parameters:
        element - the element to search for
        Returns:
        the index of the element if found; otherwise, -(insertion point) - 1, where the insertion point is the index at which the element would be inserted. A non-negative return value indicates the element is present.
        Throws:
        java.lang.ClassCastException - if the element cannot be compared using natural ordering
      • search

        int search​(T element,
                   @NonNull java.util.Comparator<? super T> comparator)
        Searches for a specified element in this sequence, which must be sorted according to the given comparator.

        If the sequence is an IndexedSeq, a binary search is used; otherwise, a linear search is performed.

        Parameters:
        element - the element to search for
        comparator - the comparator defining the order of the sequence
        Returns:
        the index of the element if found; otherwise, -(insertion point) - 1, where the insertion point is the index at which the element would be inserted. A non-negative return value indicates the element is present.
        Throws:
        java.lang.NullPointerException - if comparator is null
      • distinct

        Seq<T> distinct()
        Description copied from interface: Traversable
        Returns a new Traversable containing the elements of this instance with all duplicates removed. Element equality is determined using equals.
        Specified by:
        distinct in interface Traversable<T>
        Returns:
        a new Traversable without duplicate elements
      • distinctBy

        Seq<T> distinctBy​(@NonNull java.util.Comparator<? super T> comparator)
        Description copied from interface: Traversable
        Returns a new Traversable containing the elements of this instance without duplicates, as determined by the given comparator.
        Specified by:
        distinctBy in interface Traversable<T>
        Parameters:
        comparator - a comparator used to determine equality of elements
        Returns:
        a new Traversable with duplicates removed
      • distinctBy

        <U> Seq<T> distinctBy​(@NonNull java.util.function.Function<? super T,​? extends U> keyExtractor)
        Description copied from interface: Traversable
        Returns a new Traversable containing the elements of this instance without duplicates, based on keys extracted from elements using keyExtractor.

        The first occurrence of each key is retained in the resulting sequence.

        Specified by:
        distinctBy in interface Traversable<T>
        Type Parameters:
        U - the type of key
        Parameters:
        keyExtractor - a function to extract keys for determining uniqueness
        Returns:
        a new Traversable with duplicates removed based on keys
      • distinctByKeepLast

        Seq<T> distinctByKeepLast​(@NonNull java.util.Comparator<? super T> comparator)
        Returns a sequence with duplicate elements removed, as determined by the provided comparator. When duplicates are found, the **last occurrence** of each element is retained.
        Parameters:
        comparator - a comparator defining equality between elements
        Returns:
        a new sequence with duplicates removed, keeping the last occurrence of each element
      • distinctByKeepLast

        <U> Seq<T> distinctByKeepLast​(@NonNull java.util.function.Function<? super T,​? extends U> keyExtractor)
        Returns a sequence with duplicates removed based on a key extracted from each element. The key is obtained via the provided keyExtractor function. When duplicates are found, the **last occurrence** of each element for a given key is retained.
        Type Parameters:
        U - the type of the key used for determining uniqueness
        Parameters:
        keyExtractor - a function extracting a key from each element for uniqueness comparison
        Returns:
        a new sequence of elements distinct by the extracted key, keeping the last occurrence
      • drop

        Seq<T> drop​(int n)
        Description copied from interface: Traversable
        Returns a new Traversable without the first n elements, or an empty instance if this contains fewer than n elements.
        Specified by:
        drop in interface Traversable<T>
        Parameters:
        n - the number of elements to drop
        Returns:
        a new instance excluding the first n elements
      • dropUntil

        Seq<T> dropUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Returns a new Traversable starting from the first element that satisfies the given predicate, dropping all preceding elements.
        Specified by:
        dropUntil in interface Traversable<T>
        Parameters:
        predicate - a condition tested on each element
        Returns:
        a new instance starting from the first element matching the predicate
      • dropWhile

        Seq<T> dropWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Returns a new Traversable starting from the first element that does not satisfy the given predicate, dropping all preceding elements.

        This is equivalent to dropUntil(predicate.negate()), which is useful for method references that cannot be negated directly.

        Specified by:
        dropWhile in interface Traversable<T>
        Parameters:
        predicate - a condition tested on each element
        Returns:
        a new instance starting from the first element not matching the predicate
      • dropRight

        Seq<T> dropRight​(int n)
        Description copied from interface: Traversable
        Returns a new Traversable without the last n elements, or an empty instance if this contains fewer than n elements.
        Specified by:
        dropRight in interface Traversable<T>
        Parameters:
        n - the number of elements to drop from the end
        Returns:
        a new instance excluding the last n elements
      • dropRightUntil

        Seq<T> dropRightUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
        Drops elements from the end of the sequence until an element satisfies the given predicate. The returned sequence includes the last element that satisfies the predicate.
        Parameters:
        predicate - a condition to test elements, starting from the end
        Returns:
        a new sequence containing all elements up to and including the last element that satisfies the predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • dropRightWhile

        Seq<T> dropRightWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
        Drops elements from the end of the sequence while the given predicate holds.

        This is equivalent to dropRightUntil(predicate.negate()). Useful when using method references that cannot be negated directly.

        Parameters:
        predicate - a condition to test elements, starting from the end
        Returns:
        a new sequence containing all elements up to and including the last element that does not satisfy the predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • filter

        Seq<T> filter​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Returns a new traversable containing only the elements that satisfy the given predicate.
        Specified by:
        filter in interface Traversable<T>
        Parameters:
        predicate - the condition to test elements
        Returns:
        a traversable with elements matching the predicate
      • reject

        Seq<T> reject​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Returns a new traversable containing only the elements that do not satisfy the given predicate.

        This is equivalent to filter(predicate.negate()).

        Specified by:
        reject in interface Traversable<T>
        Parameters:
        predicate - the condition to test elements
        Returns:
        a traversable with elements not matching the predicate
      • flatMap

        <U> Seq<U> flatMap​(@NonNull java.util.function.Function<? super T,​? extends java.lang.Iterable<? extends U>> mapper)
        Description copied from interface: Traversable
        Transforms each element of this Traversable into an Iterable of elements and flattens the resulting iterables into a single Traversable.
        Specified by:
        flatMap in interface Traversable<T>
        Type Parameters:
        U - the type of elements in the resulting Traversable
        Parameters:
        mapper - a function mapping elements to iterables
        Returns:
        a new Traversable containing all elements produced by applying mapper and flattening
      • foldRight

        default <U> U foldRight​(U zero,
                                @NonNull java.util.function.BiFunction<? super T,​? super U,​? extends U> f)
        Description copied from interface: Foldable
        Folds the elements of this structure from the right, starting with the given zero value and successively applying the combine function to each element.

        Folding from the right means that elements are combined starting from the last element and associating each step with the accumulated result so far.

        Example:

        
         // Result: "!cba"
         List.of("a", "b", "c").foldRight("!", (x, acc) -> acc + x);
         
        Specified by:
        foldRight in interface Foldable<T>
        Specified by:
        foldRight in interface Traversable<T>
        Type Parameters:
        U - the type of the accumulated result
        Parameters:
        zero - the initial value to start folding with
        f - a function that combines the next element and the accumulated value
        Returns:
        the folded result
      • groupBy

        <C> Map<C,​? extends Seq<T>> groupBy​(@NonNull java.util.function.Function<? super T,​? extends C> classifier)
        Description copied from interface: Traversable
        Groups elements of this Traversable based on a classifier function.
        Specified by:
        groupBy in interface Traversable<T>
        Type Parameters:
        C - The type of the group keys
        Parameters:
        classifier - A function that assigns each element to a group
        Returns:
        A map where each key corresponds to a group of elements
        See Also:
        Traversable.arrangeBy(Function)
      • grouped

        Iterator<? extends Seq<T>> grouped​(int size)
        Description copied from interface: Traversable
        Splits this Traversable into consecutive blocks of the given size.

        Let length be the number of elements in this Traversable:

        • If empty, the resulting Iterator is empty.
        • If size <= length, the resulting Iterator contains length / size blocks of size size and possibly a final smaller block of size length % size.
        • If size > length, the resulting Iterator contains a single block of size length.

        Examples:

         
         [].grouped(1) = []
         [].grouped(0) throws
         [].grouped(-1) throws
         [1,2,3,4].grouped(2) = [[1,2],[3,4]]
         [1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]]
         [1,2,3,4].grouped(5) = [[1,2,3,4]]
         
         

        Note: grouped(size) is equivalent to sliding(size, size).

        Specified by:
        grouped in interface Traversable<T>
        Parameters:
        size - the block size; must be positive
        Returns:
        an Iterator over blocks of elements
      • init

        Seq<T> init()
        Description copied from interface: Traversable
        Returns all elements of this Traversable except the last one.

        This is the dual of Traversable.tail().

        Specified by:
        init in interface Traversable<T>
        Returns:
        a new instance containing all elements except the last
      • initOption

        Option<? extends Seq<T>> initOption()
        Description copied from interface: Traversable
        Returns all elements of this Traversable except the last one, wrapped in an Option.

        This is the dual of Traversable.tailOption().

        Specified by:
        initOption in interface Traversable<T>
        Returns:
        Some(traversable) if non-empty, or None if this Traversable is empty
      • map

        <U> Seq<U> map​(@NonNull java.util.function.Function<? super T,​? extends U> mapper)
        Description copied from interface: Traversable
        Transforms the elements of this Traversable to a new type, preserving order if defined.
        Specified by:
        map in interface Traversable<T>
        Specified by:
        map in interface Value<T>
        Type Parameters:
        U - the target element type
        Parameters:
        mapper - a mapping function
        Returns:
        a new Traversable containing the mapped elements
      • mapTo

        default <U> Seq<U> mapTo​(U value)
        Description copied from interface: Value
        Maps the underlying value to another fixed value.
        Specified by:
        mapTo in interface Traversable<T>
        Specified by:
        mapTo in interface Value<T>
        Type Parameters:
        U - The new component type
        Parameters:
        value - value to replace the contents with
        Returns:
        A new value
      • mapToVoid

        default Seq<java.lang.Void> mapToVoid()
        Description copied from interface: Value
        Maps the underlying value to Void
        Specified by:
        mapToVoid in interface Traversable<T>
        Specified by:
        mapToVoid in interface Value<T>
        Returns:
        A new value of type Void
      • orElse

        Seq<T> orElse​(java.lang.Iterable<? extends T> other)
        Description copied from interface: Traversable
        Returns this Traversable if it is non-empty; otherwise, returns the given alternative.
        Specified by:
        orElse in interface Traversable<T>
        Parameters:
        other - an alternative Traversable to return if this is empty
        Returns:
        this Traversable if non-empty, otherwise other
      • orElse

        Seq<T> orElse​(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
        Description copied from interface: Traversable
        Returns this Traversable if it is non-empty; otherwise, returns the result of evaluating the given supplier.
        Specified by:
        orElse in interface Traversable<T>
        Parameters:
        supplier - a supplier of an alternative Traversable if this is empty
        Returns:
        this Traversable if non-empty, otherwise the result of supplier.get()
      • partition

        Tuple2<? extends Seq<T>,​? extends Seq<T>> partition​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Splits this Traversable into two partitions according to a predicate.

        The first partition contains all elements that satisfy the predicate, and the second contains all elements that do not. The original iteration order is preserved.

        Specified by:
        partition in interface Traversable<T>
        Parameters:
        predicate - a predicate used to classify elements
        Returns:
        a Tuple2 containing the two resulting Traversable instances
      • peek

        Seq<T> peek​(@NonNull java.util.function.Consumer<? super T> action)
        Description copied from interface: Value
        Performs the given action on the first element if this is an eager implementation. Performs the given action on all elements (the first immediately, successive deferred), if this is a lazy implementation.
        Specified by:
        peek in interface Traversable<T>
        Specified by:
        peek in interface Value<T>
        Parameters:
        action - The action that will be performed on the element(s).
        Returns:
        this instance
      • replace

        Seq<T> replace​(T currentElement,
                       T newElement)
        Description copied from interface: Traversable
        Replaces the first occurrence of currentElement with newElement, if it exists.
        Specified by:
        replace in interface Traversable<T>
        Parameters:
        currentElement - the element to be replaced
        newElement - the replacement element
        Returns:
        a new Traversable with the first occurrence of currentElement replaced by newElement
      • replaceAll

        Seq<T> replaceAll​(T currentElement,
                          T newElement)
        Description copied from interface: Traversable
        Replaces all occurrences of currentElement with newElement.
        Specified by:
        replaceAll in interface Traversable<T>
        Parameters:
        currentElement - the element to be replaced
        newElement - the replacement element
        Returns:
        a new Traversable with all occurrences of currentElement replaced by newElement
      • retainAll

        Seq<T> retainAll​(@NonNull java.lang.Iterable<? extends T> elements)
        Description copied from interface: Traversable
        Retains only the elements from this Traversable that are contained in the given elements.
        Specified by:
        retainAll in interface Traversable<T>
        Parameters:
        elements - the elements to keep
        Returns:
        a new Traversable containing only the elements present in elements, in their original order
      • scan

        Seq<T> scan​(T zero,
                    @NonNull java.util.function.BiFunction<? super T,​? super T,​? extends T> operation)
        Description copied from interface: Traversable
        Computes a prefix scan of the elements of this Traversable.

        The neutral element zero may be applied more than once.

        Specified by:
        scan in interface Traversable<T>
        Parameters:
        zero - the neutral element for the operator
        operation - an associative binary operator
        Returns:
        a new Traversable containing the prefix scan of the elements
      • scanLeft

        <U> Seq<U> scanLeft​(U zero,
                            @NonNull java.util.function.BiFunction<? super U,​? super T,​? extends U> operation)
        Description copied from interface: Traversable
        Produces a collection containing cumulative results of applying the operator from left to right.

        Will not terminate for infinite collections. The results may vary across runs unless the collection is ordered.

        Specified by:
        scanLeft in interface Traversable<T>
        Type Parameters:
        U - the type of the resulting elements
        Parameters:
        zero - the initial value
        operation - a binary operator applied to the intermediate result and each element
        Returns:
        a new Traversable containing the cumulative results
      • scanRight

        <U> Seq<U> scanRight​(U zero,
                             @NonNull java.util.function.BiFunction<? super T,​? super U,​? extends U> operation)
        Description copied from interface: Traversable
        Produces a collection containing cumulative results of applying the operator from right to left.

        The head of the resulting collection is the last cumulative result. Will not terminate for infinite collections. Results may vary across runs unless the collection is ordered.

        Specified by:
        scanRight in interface Traversable<T>
        Type Parameters:
        U - the type of the resulting elements
        Parameters:
        zero - the initial value
        operation - a binary operator applied to each element and the intermediate result
        Returns:
        a new Traversable containing the cumulative results
      • slideBy

        Iterator<? extends Seq<T>> slideBy​(@NonNull java.util.function.Function<? super T,​?> classifier)
        Description copied from interface: Traversable
        Partitions this Traversable into consecutive non-overlapping windows according to a classification function.

        Each window contains elements with the same class, as determined by classifier. Two consecutive elements belong to the same window only if classifier returns equal values for both. Otherwise, the current window ends and a new window begins with the next element.

        Examples:

        
         [].slideBy(Function.identity()) = []
         [1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]]
         [1,2,3,10,12,5,7,20,29].slideBy(x -> x / 10) = [[1,2,3],[10,12],[5,7],[20,29]]
         
        Specified by:
        slideBy in interface Traversable<T>
        Parameters:
        classifier - A function classifying elements into groups
        Returns:
        An Iterator of windows (grouped elements)
      • sliding

        Iterator<? extends Seq<T>> sliding​(int size)
        Description copied from interface: Traversable
        Slides a window of a given size over this Traversable with a step size of 1.

        This is equivalent to calling Traversable.sliding(int, int) with a step size of 1.

        Specified by:
        sliding in interface Traversable<T>
        Parameters:
        size - a positive window size
        Returns:
        An Iterator of windows, each containing up to size elements
      • sliding

        Iterator<? extends Seq<T>> sliding​(int size,
                                           int step)
        Description copied from interface: Traversable
        Slides a window of a specific size with a given step over this Traversable.

        Examples:

        
         [].sliding(1, 1) = []
         [1,2,3,4,5].sliding(2, 3) = [[1,2],[4,5]]
         [1,2,3,4,5].sliding(2, 4) = [[1,2],[5]]
         [1,2,3,4,5].sliding(2, 5) = [[1,2]]
         [1,2,3,4].sliding(5, 3) = [[1,2,3,4],[4]]
         
        Specified by:
        sliding in interface Traversable<T>
        Parameters:
        size - a positive window size
        step - a positive step size
        Returns:
        an Iterator of windows with the given size and step
      • span

        Tuple2<? extends Seq<T>,​? extends Seq<T>> span​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Splits this Traversable into a prefix and remainder according to the given predicate.

        The first element of the returned Tuple is the longest prefix of elements satisfying predicate, and the second element is the remaining elements.

        Specified by:
        span in interface Traversable<T>
        Parameters:
        predicate - a predicate used to determine the prefix
        Returns:
        a Tuple containing the prefix and remainder
      • tail

        Seq<T> tail()
        Description copied from interface: Traversable
        Returns a new Traversable without its first element.
        Specified by:
        tail in interface Traversable<T>
        Returns:
        a new Traversable containing all elements except the first
      • tailOption

        Option<? extends Seq<T>> tailOption()
        Description copied from interface: Traversable
        Returns a new Traversable without its first element as an Option.
        Specified by:
        tailOption in interface Traversable<T>
        Returns:
        Some(traversable) if non-empty, otherwise None
      • take

        Seq<T> take​(int n)
        Description copied from interface: Traversable
        Returns the first n elements of this Traversable, or all elements if n exceeds the length.

        Equivalent to sublist(0, max(0, min(length(), n))), but safe for n < 0 or n > length().

        If n < 0, an empty instance is returned. If n > length(), the full instance is returned.

        Specified by:
        take in interface Traversable<T>
        Parameters:
        n - the number of elements to take
        Returns:
        a new Traversable containing the first n elements
      • takeUntil

        Seq<T> takeUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Takes elements from this Traversable until the given predicate holds for an element.

        Equivalent to takeWhile(predicate.negate()), but useful when using method references that cannot be negated directly.

        Specified by:
        takeUntil in interface Traversable<T>
        Parameters:
        predicate - a condition tested sequentially on the elements
        Returns:
        a new Traversable containing all elements before the first one that satisfies the predicate
      • takeWhile

        Seq<T> takeWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
        Description copied from interface: Traversable
        Takes elements from this Traversable while the given predicate holds.
        Specified by:
        takeWhile in interface Traversable<T>
        Parameters:
        predicate - a condition tested sequentially on the elements
        Returns:
        a new Traversable containing all elements up to (but not including) the first one that does not satisfy the predicate
      • takeRight

        Seq<T> takeRight​(int n)
        Description copied from interface: Traversable
        Returns the last n elements of this Traversable, or all elements if n exceeds the length.

        Equivalent to sublist(max(0, length() - n), length()), but safe for n < 0 or n > length().

        If n < 0, an empty instance is returned. If n > length(), the full instance is returned.

        Specified by:
        takeRight in interface Traversable<T>
        Parameters:
        n - the number of elements to take from the end
        Returns:
        a new Traversable containing the last n elements
      • takeRightUntil

        Seq<T> takeRightUntil​(@NonNull java.util.function.Predicate<? super T> predicate)
        Takes elements from the end of the sequence until an element satisfies the given predicate. The returned sequence starts after the last element that satisfies the predicate.
        Parameters:
        predicate - a condition to test elements, starting from the end
        Returns:
        a new sequence containing all elements after the last element that satisfies the predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • takeRightWhile

        Seq<T> takeRightWhile​(@NonNull java.util.function.Predicate<? super T> predicate)
        Takes elements from the end of the sequence while the given predicate holds.

        This is an equivalent to takeRightUntil(predicate.negate()). Useful when using method references that cannot be negated directly.

        Parameters:
        predicate - a condition to test elements, starting from the end
        Returns:
        a new sequence containing all elements after the last element that does not satisfy the predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • unzip

        <T1,​T2> Tuple2<? extends Seq<T1>,​? extends Seq<T2>> unzip​(@NonNull java.util.function.Function<? super T,​Tuple2<? extends T1,​? extends T2>> unzipper)
        Description copied from interface: Traversable
        Unzips the elements of this Traversable by mapping each element to a pair and splitting them into two separate Traversable collections.
        Specified by:
        unzip in interface Traversable<T>
        Type Parameters:
        T1 - type of the first element in the resulting pairs
        T2 - type of the second element in the resulting pairs
        Parameters:
        unzipper - a function that maps elements of this Traversable to pairs
        Returns:
        a Tuple2 containing two Traversable collections with the split elements
      • unzip3

        <T1,​T2,​T3> Tuple3<? extends Seq<T1>,​? extends Seq<T2>,​? extends Seq<T3>> unzip3​(@NonNull java.util.function.Function<? super T,​Tuple3<? extends T1,​? extends T2,​? extends T3>> unzipper)
        Description copied from interface: Traversable
        Unzips the elements of this Traversable by mapping each element to a triple and splitting them into three separate Traversable collections.
        Specified by:
        unzip3 in interface Traversable<T>
        Type Parameters:
        T1 - type of the first element in the resulting triples
        T2 - type of the second element in the resulting triples
        T3 - type of the third element in the resulting triples
        Parameters:
        unzipper - a function that maps elements of this Traversable to triples
        Returns:
        a Tuple3 containing three Traversable collections with the split elements
      • zip

        <U> Seq<Tuple2<T,​U>> zip​(@NonNull java.lang.Iterable<? extends U> that)
        Description copied from interface: Traversable
        Returns a Traversable formed by pairing elements of this Traversable with elements of another Iterable. Pairing stops when either collection runs out of elements; any remaining elements in the longer collection are ignored.

        The length of the resulting Traversable is the minimum of the lengths of this Traversable and that.

        Specified by:
        zip in interface Traversable<T>
        Type Parameters:
        U - the type of elements in the second half of each pair
        Parameters:
        that - an Iterable providing the second element of each pair
        Returns:
        a new Traversable containing pairs of corresponding elements
      • zipWith

        <U,​R> Seq<R> zipWith​(@NonNull java.lang.Iterable<? extends U> that,
                                   java.util.function.BiFunction<? super T,​? super U,​? extends R> mapper)
        Description copied from interface: Traversable
        Returns a Traversable by combining elements of this Traversable with elements of another Iterable using a mapping function. Pairing stops when either collection runs out of elements.

        The length of the resulting Traversable is the minimum of the lengths of this Traversable and that.

        Specified by:
        zipWith in interface Traversable<T>
        Type Parameters:
        U - the type of elements in the second parameter of the mapper
        R - the type of elements in the resulting Traversable
        Parameters:
        that - an Iterable providing the second parameter of the mapper
        mapper - a function that combines elements from this and that into a new element
        Returns:
        a new Traversable containing mapped elements
      • zipAll

        <U> Seq<Tuple2<T,​U>> zipAll​(@NonNull java.lang.Iterable<? extends U> that,
                                          T thisElem,
                                          U thatElem)
        Description copied from interface: Traversable
        Returns a Traversable formed by pairing elements of this Traversable with elements of another Iterable, filling in placeholder elements when one collection is shorter than the other.

        The length of the resulting Traversable is the maximum of the lengths of this Traversable and that.

        If this Traversable is shorter than that, thisElem is used as a filler. Conversely, if that is shorter, thatElem is used.

        Specified by:
        zipAll in interface Traversable<T>
        Type Parameters:
        U - the type of elements in the second half of each pair
        Parameters:
        that - an Iterable providing the second element of each pair
        thisElem - the element used to fill missing values if this Traversable is shorter than that
        thatElem - the element used to fill missing values if that is shorter than this Traversable
        Returns:
        a new Traversable containing pairs of elements, including fillers as needed
      • zipWithIndex

        Seq<Tuple2<T,​java.lang.Integer>> zipWithIndex()
        Description copied from interface: Traversable
        Zips this Traversable with its indices, starting at 0.
        Specified by:
        zipWithIndex in interface Traversable<T>
        Returns:
        a new Traversable containing each element paired with its index
      • zipWithIndex

        <U> Seq<U> zipWithIndex​(@NonNull java.util.function.BiFunction<? super T,​? super java.lang.Integer,​? extends U> mapper)
        Description copied from interface: Traversable
        Zips this Traversable with its indices and maps the resulting pairs using the provided mapper.
        Specified by:
        zipWithIndex in interface Traversable<T>
        Type Parameters:
        U - the type of elements in the resulting Traversable
        Parameters:
        mapper - a function mapping an element and its index to a new element
        Returns:
        a new Traversable containing the mapped elements
      • withDefaultValue

        @Deprecated
        default Function1<java.lang.Integer,​T> withDefaultValue​(T defaultValue)
        Deprecated.
        Will be removed
        Turns this sequence from a partial function into a total function that returns defaultValue for all indexes that are out of bounds.
        Parameters:
        defaultValue - default value to return for out of bound indexes
        Returns:
        a total function from index to T
      • withDefault

        @Deprecated
        default Function1<java.lang.Integer,​T> withDefault​(@NonNull java.util.function.Function<? super java.lang.Integer,​? extends T> defaultFunction)
        Deprecated.
        Will be removed
        Turns this sequence from a partial function into a total function that returns a value computed by defaultFunction for all indexes that are out of bounds.
        Parameters:
        defaultFunction - function to evaluate for all out-of-bounds indexes.
        Returns:
        a total function from index to T
      • isSequential

        default boolean isSequential()
        Description copied from interface: Traversable
        Indicates whether the elements of this Traversable appear in encounter (insertion) order.
        Specified by:
        isSequential in interface Traversable<T>
        Returns:
        true if insertion order is preserved, false otherwise