Package fj.data
Class Seq<A>
java.lang.Object
fj.data.Seq<A>
- All Implemented Interfaces:
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppends the given sequence to this sequence.static <A> Seq
<A> arraySeq
(A... as) Constructs a sequence from the array.<B> Seq
<B> Bind the given function across this seq.private void
checkBounds
(int i) Inserts the given element at the front of this sequence.delete
(int i) Delete the element at the given index.drop
(int n) Drops the given number of elements from the head of this sequence if they are available.static <A> Seq
<A> empty()
The empty sequence.boolean
<B> B
<B> B
static <A> Seq
<A> fromJavaList
(List<A> list) Constructs a sequence from the given list.int
hashCode()
head()
The first element of this sequence.index
(int i) Returns the element at the given index.init()
The sequence without the last element.Inserts the element at the given index.boolean
isEmpty()
Checks if this is the empty sequence.boolean
Checks if this sequence is not empty.static <A> Seq
<A> iterableSeq
(Iterable<A> i) Constructs a sequence from the iterable.iterator()
Returns an iterator for this seq.static <A> Seq
<A> iteratorSeq
(Iterator<A> i) Constructs a sequence from the iterator.last()
The last element of this sequence.int
length()
Returns the number of elements in this sequence.static <A> Seq
<A> Constructs a sequence from the given list.<B> Seq
<B> mkTree()
static <A> Seq
<A> seq
(A... as) Constructs a sequence from the given elements.sequenceEither
(Seq<Either<L, B>> seq) Sequence the given seq and collect the output on the right side of an either.sequenceEitherLeft
(Seq<Either<B, R>> seq) Sequence the given seq and collect the output on the left side of an either.sequenceEitherRight
(Seq<Either<L, B>> seq) Sequence the given seq and collect the output on the right side of an either.Sequence the given seq and collect the output as a function.sequenceIO
(Seq<IO<B>> seq) Sequence the given seq and collect the output as an IO.sequenceList
(Seq<List<B>> seq) Sequence the given seq and collect the output as a list.sequenceOption
(Seq<Option<B>> seq) Sequence the given seq and collect the output as an seq.sequenceP1
(Seq<P1<B>> seq) Sequence the given seq and collect the output as a P1.sequenceSeq
(Seq<Seq<B>> seq) Sequence the given seq and collect the output as a seq.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.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.Inserts the given element at the end of this sequence.split
(int i) Splits this sequence into a pair of sequences at the given position.tail()
The sequence without the first element.take
(int n) Takes the given number of elements from the head of this sequence if they are available.Converts the sequence to a java.util.ListtoList()
Converts this sequence to a ListtoStream()
Converts this sequence to a StreamtoString()
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.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.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.Traverse this seq with the given function and collect the output as a function.traverseIO
(F<A, IO<B>> f) Traverse this seq with the given function and collect the output as an IO.traverseList
(F<A, List<B>> f) Traverse this seq with the given function and collect the output as a list.traverseOption
(F<A, Option<B>> f) Traverses through the Seq with the given functiontraverseP1
(F<A, P1<B>> f) Traverse this seq with the given function and collect the output as a p1.traverseSeq
(F<A, Seq<B>> f) Traverse this seq with the given function and collect the output as a seq.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.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.Replace the element at the given index with the supplied value.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
ELEM_MEASURED
-
MK_TREE
-
EMPTY
-
ftree
-
-
Constructor Details
-
Seq
-
-
Method Details
-
mkTree
-
elemMeasured
-
empty
The empty sequence.- Returns:
- A sequence with no elements.
-
equals
-
single
A singleton sequence.- Parameters:
a
- The single element in the sequence.- Returns:
- A new sequence with the given element in it.
-
seq
Constructs a sequence from the given elements.- Parameters:
as
- The elements to create the sequence from.- Returns:
- A sequence with the given elements.
-
listSeq
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
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
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
Constructs a sequence from the array. -
fromJavaList
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
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
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
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
-
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
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
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
Converts this sequence to a Stream -
toList
Converts this sequence to a List -
toJavaList
Converts the sequence to a java.util.List -
iterator
Returns an iterator for this seq. This method exists to permit the use in afor
-each loop. -
toString
-
append
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
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
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
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
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
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
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
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
-
foldRight
-
filter
-
hashCode
public int hashCode() -
map
-
bind
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
Sequence the given seq 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:
seq
- the given seq- Returns:
- the either
-
sequenceEitherLeft
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
Sequence the given seq 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:
seq
- the given seq- Returns:
- the either
-
sequenceF
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
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
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
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
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
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
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
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
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
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
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 valueL
- the type of the left value- Parameters:
f
- the given function- Returns:
- the either
-
traverseEitherLeft
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
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
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
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
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
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
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
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
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
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
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
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
-