Package fj.data

Class Seq<A>

java.lang.Object
fj.data.Seq<A>
All Implemented Interfaces:
Iterable<A>

public final class Seq<A> extends Object implements Iterable<A>
Provides an immutable finite sequence, implemented as a finger tree. This structure gives O(1) access to the head and tail, as well as O(log n) random access and concatenation of sequences.
  • Field Details

  • Constructor Details

  • Method Details

    • mkTree

      private static <A> MakeTree<Integer,A> mkTree()
    • elemMeasured

      private static <A> Measured<Integer,A> elemMeasured()
    • empty

      public static <A> Seq<A> empty()
      The empty sequence.
      Returns:
      A sequence with no elements.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • single

      public static <A> Seq<A> single(A a)
      A singleton sequence.
      Parameters:
      a - The single element in the sequence.
      Returns:
      A new sequence with the given element in it.
    • seq

      @SafeVarargs public static <A> Seq<A> seq(A... as)
      Constructs a sequence from the given elements.
      Parameters:
      as - The elements to create the sequence from.
      Returns:
      A sequence with the given elements.
    • listSeq

      public static <A> Seq<A> listSeq(List<A> list)
      Constructs a sequence from the given list.
      Parameters:
      list - The list to create the sequence from.
      Returns:
      A sequence with the elements of the list.
    • iterableSeq

      public static <A> Seq<A> iterableSeq(Iterable<A> i)
      Constructs a sequence from the iterable.
      Parameters:
      i - The iterable to create the sequence from.
      Returns:
      A sequence with the elements of the iterable.
    • iteratorSeq

      public static <A> Seq<A> iteratorSeq(Iterator<A> i)
      Constructs a sequence from the iterator.
      Parameters:
      i - The iterator to create the sequence from.
      Returns:
      A sequence with the elements of the iterator.
    • arraySeq

      @SafeVarargs public static <A> Seq<A> arraySeq(A... as)
      Constructs a sequence from the array.
    • fromJavaList

      public static <A> Seq<A> fromJavaList(List<A> list)
      Constructs a sequence from the given list.
      Parameters:
      list - The list to create the sequence from.
      Returns:
      A sequence with the elements of the list.
    • cons

      public Seq<A> cons(A a)
      Inserts the given element at the front of this sequence.
      Parameters:
      a - An element to insert at the front of this sequence.
      Returns:
      A new sequence with the given element at the front.
    • snoc

      public Seq<A> snoc(A a)
      Inserts the given element at the end of this sequence.
      Parameters:
      a - An element to insert at the end of this sequence.
      Returns:
      A new sequence with the given element at the end.
    • head

      public A head()
      The first element of this sequence. This is an O(1) operation.
      Returns:
      The first element if this sequence is nonempty, otherwise throws an error.
    • headOption

      public Option<A> headOption()
    • last

      public A last()
      The last element of this sequence. This is an O(1) operation.
      Returns:
      The last element if this sequence is nonempty, otherwise throws an error.
    • tail

      public Seq<A> tail()
      The sequence without the first element. This is an O(1) operation.
      Returns:
      The sequence without the first element if this sequence is nonempty, otherwise throws an error.
    • init

      public Seq<A> init()
      The sequence without the last element. This is an O(1) operation.
      Returns:
      The sequence without the last element if this sequence is nonempty, otherwise throws an error.
    • toStream

      public Stream<A> toStream()
      Converts this sequence to a Stream
    • toList

      public List<A> toList()
      Converts this sequence to a List
    • toJavaList

      public List<A> toJavaList()
      Converts the sequence to a java.util.List
    • iterator

      public Iterator<A> iterator()
      Returns an iterator for this seq. This method exists to permit the use in a for-each loop.
      Specified by:
      iterator in interface Iterable<A>
      Returns:
      A iterator for this seq.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • append

      public Seq<A> append(Seq<A> as)
      Appends the given sequence to this sequence.
      Parameters:
      as - A sequence to append to this one.
      Returns:
      A new sequence with the given sequence appended to this one.
    • isEmpty

      public boolean isEmpty()
      Checks if this is the empty sequence.
      Returns:
      True if this sequence is empty, otherwise false.
    • insert

      public Seq<A> insert(int index, A a)
      Inserts the element at the given index. This is an O(log(n)) operation.
      Parameters:
      index - The index of the element to return.
      Returns:
      The sequence with the element inserted at the given index, or throws an error if the index is out of bounds.
    • isNotEmpty

      public boolean isNotEmpty()
      Checks if this sequence is not empty.
      Returns:
      True if this sequence is not empty, otherwise false.
    • length

      public int length()
      Returns the number of elements in this sequence.
      Returns:
      the number of elements in this sequence.
    • split

      public P2<Seq<A>,Seq<A>> split(int i)
      Splits this sequence into a pair of sequences at the given position. This is a O(log(n)) operation.
      Returns:
      Pair: the subsequence containing elements with indices less than i and the subsequence containing elements with indices greater than or equal to i.
    • index

      public A index(int i)
      Returns the element at the given index. This is an O(log(n)) operation.
      Parameters:
      i - The index of the element to return.
      Returns:
      The element at the given index, or throws an error if the index is out of bounds.
    • update

      public Seq<A> update(int i, A a)
      Replace the element at the given index with the supplied value. This is an O(log(n)) operation.
      Parameters:
      i - The index of the element to update.
      a - The new value.
      Returns:
      The updated sequence, or throws an error if the index is out of bounds.
    • delete

      public Seq<A> delete(int i)
      Delete the element at the given index. This is an O(log(n)) operation.
      Parameters:
      i - The index of the element to update.
      Returns:
      The updated sequence, or throws an error if the index is out of bounds.
    • take

      public Seq<A> take(int n)
      Takes the given number of elements from the head of this sequence if they are available.
      Parameters:
      n - The maximum number of elements to take from this sequence.
      Returns:
      A sequence consisting only of the first n elements of this sequence, or else the whole sequence, if it has less than n elements.
    • drop

      public Seq<A> drop(int n)
      Drops the given number of elements from the head of this sequence if they are available.
      Parameters:
      n - The number of elements to drop from this sequence.
      Returns:
      A sequence consisting of all elements of this sequence except the first n ones, or else the empty sequence, if this sequence has less than n elements.
    • checkBounds

      private void checkBounds(int i)
    • foldLeft

      public <B> B foldLeft(F2<B,A,B> f, B z)
    • foldRight

      public <B> B foldRight(F2<A,B,B> f, B z)
    • filter

      public Seq<A> filter(F<A,Boolean> f)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • map

      public <B> Seq<B> map(F<A,B> f)
    • bind

      public <B> Seq<B> bind(F<A,Seq<B>> f)
      Bind the given function across this seq.
      Type Parameters:
      B - the type of the seq value
      Parameters:
      f - the given function
      Returns:
      the seq
    • sequenceEither

      public static <L, B> Either<L,Seq<B>> sequenceEither(Seq<Either<L,B>> seq)
      Sequence the given seq and collect the output on the right side of an either.
      Type Parameters:
      L - the type of the left value
      B - the type of the right value
      Parameters:
      seq - the given seq
      Returns:
      the either
    • sequenceEitherLeft

      public static <R, B> Either<Seq<B>,R> sequenceEitherLeft(Seq<Either<B,R>> seq)
      Sequence the given seq and collect the output on the left side of an either.
      Type Parameters:
      R - the type of the right value
      B - the type of the left value
      Parameters:
      seq - the given seq
      Returns:
      the either
    • sequenceEitherRight

      public static <L, B> Either<L,Seq<B>> sequenceEitherRight(Seq<Either<L,B>> seq)
      Sequence the given seq and collect the output on the right side of an either.
      Type Parameters:
      L - the type of the left value
      B - the type of the right value
      Parameters:
      seq - the given seq
      Returns:
      the either
    • sequenceF

      public static <C, B> F<C,Seq<B>> sequenceF(Seq<F<C,B>> seq)
      Sequence the given seq and collect the output as a function.
      Type Parameters:
      C - the type of the input value
      B - the type of the output value
      Parameters:
      seq - the given seq
      Returns:
      the either
    • sequenceIO

      public static <B> IO<Seq<B>> sequenceIO(Seq<IO<B>> seq)
      Sequence the given seq and collect the output as an IO.
      Type Parameters:
      B - the type of the IO value
      Parameters:
      seq - the given seq
      Returns:
      the IO
    • sequenceList

      public static <B> List<Seq<B>> sequenceList(Seq<List<B>> seq)
      Sequence the given seq and collect the output as a list.
      Type Parameters:
      B - the type of the seq value
      Parameters:
      seq - the given seq
      Returns:
      the list
    • sequenceOption

      public static <B> Option<Seq<B>> sequenceOption(Seq<Option<B>> seq)
      Sequence the given seq and collect the output as an seq.
      Type Parameters:
      B - the type of the seq value
      Parameters:
      seq - the given seq
      Returns:
      the seq
    • sequenceP1

      public static <B> P1<Seq<B>> sequenceP1(Seq<P1<B>> seq)
      Sequence the given seq and collect the output as a P1.
      Type Parameters:
      B - the type of the P1 value
      Parameters:
      seq - the given seq
      Returns:
      the P1
    • sequenceSeq

      public static <B> Seq<Seq<B>> sequenceSeq(Seq<Seq<B>> seq)
      Sequence the given seq and collect the output as a seq.
      Type Parameters:
      B - the type of the seq value
      Parameters:
      seq - the given seq
      Returns:
      the seq
    • sequenceSet

      public static <B> Set<Seq<B>> sequenceSet(Ord<B> ord, Seq<Set<B>> seq)
      Sequence the given seq and collect the output as a set; use the given ord to order the set.
      Type Parameters:
      B - the type of the set value
      Parameters:
      ord - the given ord
      seq - the given seq
      Returns:
      the either
    • sequenceStream

      public static <B> Stream<Seq<B>> sequenceStream(Seq<Stream<B>> seq)
      Sequence the given seq and collect the output as a stream.
      Type Parameters:
      B - the type of the stream value
      Parameters:
      seq - the given seq
      Returns:
      the stream
    • sequenceTrampoline

      public static <B> Trampoline<Seq<B>> sequenceTrampoline(Seq<Trampoline<B>> seq)
      Sequence the given seq and collect the output as a trampoline.
      Type Parameters:
      B - the type of the stream value
      Parameters:
      seq - the given trampoline
      Returns:
      the stream
    • sequenceValidation

      public static <E, B> Validation<E,Seq<B>> sequenceValidation(Seq<Validation<E,B>> seq)
      Sequence the given seq and collect the output as a validation.
      Type Parameters:
      E - the type of the failure value
      B - the type of the success value
      Parameters:
      seq - the given seq
      Returns:
      the validation
    • sequenceValidation

      public static <E, B> Validation<E,Seq<B>> sequenceValidation(Semigroup<E> semigroup, Seq<Validation<E,B>> seq)
      Sequence the given seq and collect the output as a validation; use the given semigroup to reduce the errors.
      Type Parameters:
      E - the type of the failure value
      B - the type of the success value
      Parameters:
      semigroup - the given semigroup
      seq - the given seq
      Returns:
      the validation
    • traverseEither

      public <B, L> Either<L,Seq<B>> traverseEither(F<A,Either<L,B>> f)
      Traverse this seq with the given function and collect the output on the right side of an either.
      Type Parameters:
      B - the type of the right value
      L - the type of the left value
      Parameters:
      f - the given function
      Returns:
      the either
    • traverseEitherLeft

      public <R, B> Either<Seq<B>,R> traverseEitherLeft(F<A,Either<B,R>> f)
      Traverse this seq with the given function and collect the output on the left side of an either.
      Type Parameters:
      R - the type of the left value
      B - the type of the right value
      Parameters:
      f - the given function
      Returns:
      the either
    • traverseEitherRight

      public <L, B> Either<L,Seq<B>> traverseEitherRight(F<A,Either<L,B>> f)
      Traverse this seq with the given function and collect the output on the right side of an either.
      Type Parameters:
      L - the type of the left value
      B - the type of the right value
      Parameters:
      f - the given function
      Returns:
      the either
    • traverseF

      public <C, B> F<C,Seq<B>> traverseF(F<A,F<C,B>> f)
      Traverse this seq with the given function and collect the output as a function.
      Type Parameters:
      C - the type of the input value
      B - the type of the output value
      Parameters:
      f - the given function
      Returns:
      the function
    • traverseIO

      public <B> IO<Seq<B>> traverseIO(F<A,IO<B>> f)
      Traverse this seq with the given function and collect the output as an IO.
      Type Parameters:
      B - the type of the IO value
      Parameters:
      f - the given function
      Returns:
      the IO
    • traverseList

      public <B> List<Seq<B>> traverseList(F<A,List<B>> f)
      Traverse this seq with the given function and collect the output as a list.
      Type Parameters:
      B - the type of the list value
      Parameters:
      f - the given function
      Returns:
      the list
    • traverseOption

      public <B> Option<Seq<B>> traverseOption(F<A,Option<B>> f)
      Traverses through the Seq with the given function
      Parameters:
      f - The function that produces Option value
      Returns:
      none if applying f returns none to any element of the seq or f mapped seq in some .
    • traverseP1

      public <B> P1<Seq<B>> traverseP1(F<A,P1<B>> f)
      Traverse this seq with the given function and collect the output as a p1.
      Type Parameters:
      B - the type of the p1 value
      Parameters:
      f - the given function
      Returns:
      the p1
    • traverseSeq

      public <B> Seq<Seq<B>> traverseSeq(F<A,Seq<B>> f)
      Traverse this seq with the given function and collect the output as a seq.
      Type Parameters:
      B - the type of the seq value
      Parameters:
      f - the given function
      Returns:
      the seq
    • traverseSet

      public <B> Set<Seq<B>> traverseSet(Ord<B> ord, F<A,Set<B>> f)
      Traverse this seq with the given function and collect the output as a set; use the given ord to order the set.
      Type Parameters:
      B - the type of the set value
      Parameters:
      ord - the given ord
      f - the given function
      Returns:
      the set
    • traverseStream

      public <B> Stream<Seq<B>> traverseStream(F<A,Stream<B>> f)
      Traverse this seq with the given function and collect the output as a stream.
      Type Parameters:
      B - the type of the stream value
      Parameters:
      f - the given function
      Returns:
      the stream
    • traverseTrampoline

      public <B> Trampoline<Seq<B>> traverseTrampoline(F<A,Trampoline<B>> f)
      Traverse this seq with the given function and collect the output as a trampoline.
      Type Parameters:
      B - the type of the trampoline value
      Parameters:
      f - the given function
      Returns:
      the trampoline
    • traverseValidation

      public <E, B> Validation<E,Seq<B>> traverseValidation(F<A,Validation<E,B>> f)
      Traverse this seq with the given function and collect the output as a validation.
      Type Parameters:
      E - the type of the failure value
      B - the type of the success value
      Parameters:
      f - the given function
      Returns:
      the validation
    • traverseValidation

      public <E, B> Validation<E,Seq<B>> traverseValidation(Semigroup<E> semigroup, F<A,Validation<E,B>> f)
      Traverse this seq with the given function and collect the output as a validation; use the given semigroup to reduce the errors.
      Type Parameters:
      E - the type of the failure value
      B - the type of the success value
      Parameters:
      semigroup - the given semigroup
      f - the given function
      Returns:
      the validation