Package fj.data
Class Seq<A>
- java.lang.Object
-
- fj.data.Seq<A>
-
- All Implemented Interfaces:
java.lang.Iterable<A>
public final class Seq<A> extends java.lang.Object implements java.lang.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 Summary
Fields Modifier and Type Field Description private static Measured<java.lang.Integer,java.lang.Object>
ELEM_MEASURED
private static Seq<java.lang.Object>
EMPTY
private FingerTree<java.lang.Integer,A>
ftree
private static MakeTree<java.lang.Integer,java.lang.Object>
MK_TREE
-
Constructor Summary
Constructors Modifier Constructor Description private
Seq(FingerTree<java.lang.Integer,A> ftree)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Seq<A>
append(Seq<A> as)
Appends the given sequence to this sequence.static <A> Seq<A>
arraySeq(A... as)
Constructs a sequence from the array.<B> Seq<B>
bind(F<A,Seq<B>> f)
Bind the given function across this seq.private void
checkBounds(int i)
Seq<A>
cons(A a)
Inserts the given element at the front of this sequence.Seq<A>
delete(int i)
Delete the element at the given index.Seq<A>
drop(int n)
Drops the given number of elements from the head of this sequence if they are available.private static <A> Measured<java.lang.Integer,A>
elemMeasured()
static <A> Seq<A>
empty()
The empty sequence.boolean
equals(java.lang.Object other)
Seq<A>
filter(F<A,java.lang.Boolean> f)
<B> B
foldLeft(F2<B,A,B> f, B z)
<B> B
foldRight(F2<A,B,B> f, B z)
static <A> Seq<A>
fromJavaList(java.util.List<A> list)
Constructs a sequence from the given list.int
hashCode()
A
head()
The first element of this sequence.Option<A>
headOption()
A
index(int i)
Returns the element at the given index.Seq<A>
init()
The sequence without the last element.Seq<A>
insert(int index, A a)
Inserts the element at the given index.boolean
isEmpty()
Checks if this is the empty sequence.boolean
isNotEmpty()
Checks if this sequence is not empty.static <A> Seq<A>
iterableSeq(java.lang.Iterable<A> i)
Constructs a sequence from the iterable.java.util.Iterator<A>
iterator()
Returns an iterator for this seq.static <A> Seq<A>
iteratorSeq(java.util.Iterator<A> i)
Constructs a sequence from the iterator.A
last()
The last element of this sequence.int
length()
Returns the number of elements in this sequence.static <A> Seq<A>
listSeq(List<A> list)
Constructs a sequence from the given list.<B> Seq<B>
map(F<A,B> f)
private static <A> MakeTree<java.lang.Integer,A>
mkTree()
static <A> Seq<A>
seq(A... as)
Constructs a sequence from the given elements.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.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.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.static <C,B>
F<C,Seq<B>>sequenceF(Seq<F<C,B>> seq)
Sequence the given seq and collect the output as a function.static <B> IO<Seq<B>>
sequenceIO(Seq<IO<B>> seq)
Sequence the given seq and collect the output as an IO.static <B> List<Seq<B>>
sequenceList(Seq<List<B>> seq)
Sequence the given seq and collect the output as a list.static <B> Option<Seq<B>>
sequenceOption(Seq<Option<B>> seq)
Sequence the given seq and collect the output as an seq.static <B> P1<Seq<B>>
sequenceP1(Seq<P1<B>> seq)
Sequence the given seq and collect the output as a P1.static <B> Seq<Seq<B>>
sequenceSeq(Seq<Seq<B>> seq)
Sequence the given seq and collect the output as a seq.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.static <B> Stream<Seq<B>>
sequenceStream(Seq<Stream<B>> seq)
Sequence the given seq and collect the output as a stream.static <B> Trampoline<Seq<B>>
sequenceTrampoline(Seq<Trampoline<B>> seq)
Sequence the given seq and collect the output as a trampoline.static <E,B>
Validation<E,Seq<B>>sequenceValidation(Seq<Validation<E,B>> seq)
Sequence the given seq and collect the output as a validation.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.static <A> Seq<A>
single(A a)
A singleton sequence.Seq<A>
snoc(A a)
Inserts the given element at the end of this sequence.P2<Seq<A>,Seq<A>>
split(int i)
Splits this sequence into a pair of sequences at the given position.Seq<A>
tail()
The sequence without the first element.Seq<A>
take(int n)
Takes the given number of elements from the head of this sequence if they are available.java.util.List<A>
toJavaList()
Converts the sequence to a java.util.ListList<A>
toList()
Converts this sequence to a ListStream<A>
toStream()
Converts this sequence to a Streamjava.lang.String
toString()
<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.<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.<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.<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.<B> IO<Seq<B>>
traverseIO(F<A,IO<B>> f)
Traverse this seq with the given function and collect the output as an IO.<B> List<Seq<B>>
traverseList(F<A,List<B>> f)
Traverse this seq with the given function and collect the output as a list.<B> Option<Seq<B>>
traverseOption(F<A,Option<B>> f)
Traverses through the Seq with the given function<B> P1<Seq<B>>
traverseP1(F<A,P1<B>> f)
Traverse this seq with the given function and collect the output as a p1.<B> Seq<Seq<B>>
traverseSeq(F<A,Seq<B>> f)
Traverse this seq with the given function and collect the output as a seq.<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.<B> Stream<Seq<B>>
traverseStream(F<A,Stream<B>> f)
Traverse this seq with the given function and collect the output as a stream.<B> Trampoline<Seq<B>>
traverseTrampoline(F<A,Trampoline<B>> f)
Traverse this seq with the given function and collect the output as a trampoline.<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.<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.Seq<A>
update(int i, A a)
Replace the element at the given index with the supplied value.
-
-
-
Field Detail
-
ELEM_MEASURED
private static final Measured<java.lang.Integer,java.lang.Object> ELEM_MEASURED
-
MK_TREE
private static final MakeTree<java.lang.Integer,java.lang.Object> MK_TREE
-
EMPTY
private static final Seq<java.lang.Object> EMPTY
-
ftree
private final FingerTree<java.lang.Integer,A> ftree
-
-
Constructor Detail
-
Seq
private Seq(FingerTree<java.lang.Integer,A> ftree)
-
-
Method Detail
-
mkTree
private static <A> MakeTree<java.lang.Integer,A> mkTree()
-
elemMeasured
private static <A> Measured<java.lang.Integer,A> elemMeasured()
-
empty
public static <A> Seq<A> empty()
The empty sequence.- Returns:
- A sequence with no elements.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.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(java.lang.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(java.util.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(java.util.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.
-
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.
-
toJavaList
public java.util.List<A> toJavaList()
Converts the sequence to a java.util.List
-
iterator
public java.util.Iterator<A> iterator()
Returns an iterator for this seq. This method exists to permit the use in afor
-each loop.- Specified by:
iterator
in interfacejava.lang.Iterable<A>
- Returns:
- A iterator for this seq.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.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 toi
.
-
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)
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
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:
B
- the type of the right valueL
- the type of the left 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 valueB
- 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:
B
- the type of the right valueL
- the type of the left 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 valueB
- 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 ordseq
- 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 valueB
- 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 valueB
- the type of the success value- Parameters:
semigroup
- the given semigroupseq
- 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:
L
- the type of the left valueB
- the type of the right 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 valueB
- 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 valueB
- 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 valueB
- 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 ordf
- 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 valueB
- 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 valueB
- the type of the success value- Parameters:
semigroup
- the given semigroupf
- the given function- Returns:
- the validation
-
-