Interface ImmutableVector<A>
-
- Type Parameters:
A
- the element type
- All Superinterfaces:
software.kes.enhancediterables.EnhancedIterable<A>
,software.kes.enhancediterables.FiniteIterable<A>
,com.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>
,Immutable
,software.kes.enhancediterables.ImmutableFiniteIterable<A>
,software.kes.enhancediterables.ImmutableIterable<A>
,java.lang.Iterable<A>
,java.util.RandomAccess
,Vector<A>
- All Known Subinterfaces:
ImmutableNonEmptyVector<A>
public interface ImmutableVector<A> extends Vector<A>, software.kes.enhancediterables.ImmutableFiniteIterable<A>, Immutable
AVector
that is guaranteed at compile-time to be safe from mutation anywhere. In other words, it owns the sole reference to the underlying collection.In addition to guarantees of
Vector
, provides the following benefits :fmap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends B>)
always returns aImmutableVector
.take(int)
always returns aImmutableVector
.drop(int)
always returns aImmutableVector
.slice(int, int)
always returns aImmutableVector
.reverse()
always returns aImmutableVector
.zipWithIndex()
always returns aImmutableVector
.toImmutable()
always returns itself.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <A> VectorBuilder<A>
builder()
Creates a newVectorBuilder
.static <A> VectorBuilder<A>
builder(int initialCapacity)
Creates a newVectorBuilder
with an initial capacity hint.default <B> ImmutableVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>
cross(ImmutableVector<B> other)
Returns the cartesian product of thisImmutableVector
with anotherImmutableVector
.default ImmutableVector<A>
drop(int count)
Returns a newImmutableVector
that drops the firstcount
elements of thisImmutableVector
.default ImmutableVector<A>
dropRight(int count)
Returns a newImmutableVector
that drops all except the lastcount
elements of thisImmutableVector
.default ImmutableVector<A>
dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableVector
that drops longest prefix of elements of thisImmutableVector
that satisfy a predicate.default <B> ImmutableVector<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisImmutableVector
.default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends ImmutableVector<A>>
inits()
Returns aNonEmptyIterable
containing the inits of thisImmutableVector
.default software.kes.enhancediterables.ImmutableFiniteIterable<? extends ImmutableNonEmptyVector<A>>
magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anImmutableFiniteIterable
of contiguous groups of elements in thisImmutableVector
that match a predicate pairwise.default ImmutableVector<A>
reverse()
Creates anImmutableVector
with thisImmutableVector
's elements in reversed order.default ImmutableVector<A>
slice(int startIndex, int endIndexExclusive)
Creates a slice of thisImmutableVector
.default software.kes.enhancediterables.ImmutableFiniteIterable<? extends ImmutableNonEmptyVector<A>>
slide(int k)
"Slides" a window ofk
elements across theImmutableVector
by one element at a time.default com.jnape.palatable.lambda.adt.hlist.Tuple2<ImmutableVector<A>,ImmutableVector<A>>
span(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Splits thisImmutableVector
into a prefix/suffix pair according to a predicate.default com.jnape.palatable.lambda.adt.hlist.Tuple2<ImmutableVector<A>,ImmutableVector<A>>
splitAt(int index)
Splits thisImmutableVector
into two at a given position.default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<ImmutableVector<A>>
tails()
Returns aNonEmptyIterable
containing the tails of thisImmutableVector
.default ImmutableVector<A>
take(int count)
Returns a newImmutableVector
containing at most the firstcount
elements of thisImmutableVector
.default ImmutableVector<A>
takeRight(int count)
Returns a newImmutableVector
containing at most the lastcount
elements of thisImmutableVector
.default ImmutableVector<A>
takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableVector
containing the longest prefix of elements thisImmutableVector
that satisfy a predicate.default ImmutableVector<A>
toImmutable()
Returns anImmutableVector
containing the same elements as this one.default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableNonEmptyVector<A>>
toNonEmpty()
Attempts to convert thisImmutableVector
to anImmutableNonEmptyVector
.default ImmutableNonEmptyVector<A>
toNonEmptyOrThrow()
Attempts to convert thisImmutableVector
to anImmutableNonEmptyVector
.default <B,C>
ImmutableVector<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableVector<B> other)
Zips together thisImmutableVector
with anotherImmutableVector
by applying a zipping function.default ImmutableVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>>
zipWithIndex()
Zips thisImmutableVector
with its indices.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, find, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, concat, concat, cross, cross, filter, foldLeft, foldRight, intersperse, partition, prepend, prependAll, toFinite, zipWith
-
Methods inherited from interface software.kes.enhancediterables.ImmutableFiniteIterable
append, concat, concat, cross, cycle, distinct, filter, intersperse, partition, prepend, prependAll, toFinite, zipWith
-
-
-
-
Method Detail
-
cross
default <B> ImmutableVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(ImmutableVector<B> other)
Returns the cartesian product of thisImmutableVector
with anotherImmutableVector
.Does not make copies of any underlying collections.
The returned
ImmutableVector
will have a size ofsize()
×other.size()
, but will allocate no extra memory (aside from a few bytes for housekeeping).- Type Parameters:
B
- the type of the otherImmutableVector
- Parameters:
other
- anImmutableVector
of any type- Returns:
- a
ImmutableVector<Tuple2<A, B>>
-
drop
default ImmutableVector<A> drop(int count)
Returns a newImmutableVector
that drops the firstcount
elements of thisImmutableVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
ImmutableVector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed.- Specified by:
drop
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
drop
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
drop
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
drop
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
drop
in interfaceVector<A>
- Parameters:
count
- the number of elements to drop from thisImmutableVector
. Must be >= 0. May exceed size of thisImmutableVector
, in which case, the result will be an emptyImmutableVector
.- Returns:
- an
ImmutableVector<A>
-
dropRight
default ImmutableVector<A> dropRight(int count)
Returns a newImmutableVector
that drops all except the lastcount
elements of thisImmutableVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed.
-
dropWhile
default ImmutableVector<A> dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableVector
that drops longest prefix of elements of thisImmutableVector
that satisfy a predicate.Does not make copies of any underlying collections.
- Specified by:
dropWhile
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
dropWhile
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
dropWhile
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
dropWhile
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Parameters:
predicate
- a predicate; not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableVector<A>
-
fmap
default <B> ImmutableVector<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisImmutableVector
.Returns a new
ImmutableVector
of the same size (but possibly a different type).Does not make any copies of underlying collections.
This method is stack-safe, so a
ImmutableVector
can be mapped as many times as the heap permits.- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>
- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
fmap
in interfaceVector<A>
- Type Parameters:
B
- The type of the elements contained in the output Vector.- Parameters:
f
- a function fromA
toB
. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableVector<B>
of the same size
-
inits
default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends ImmutableVector<A>> inits()
Returns aNonEmptyIterable
containing the inits of thisImmutableVector
.The first value will be this
ImmutableVector
and the final one will be an emptyVector
, with the intervening values the results of successive applications ofinit
.
-
magnetizeBy
default software.kes.enhancediterables.ImmutableFiniteIterable<? extends ImmutableNonEmptyVector<A>> magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anImmutableFiniteIterable
of contiguous groups of elements in thisImmutableVector
that match a predicate pairwise.- Specified by:
magnetizeBy
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
magnetizeBy
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
magnetizeBy
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
magnetizeBy
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Parameters:
predicate
- the predicate function. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableFiniteIterable<ImmutableNonEmptyVector<A>>
containing the contiguous groups
-
reverse
default ImmutableVector<A> reverse()
Creates anImmutableVector
with thisImmutableVector
's elements in reversed order.Does not make copies of any underlying collections.
-
slice
default ImmutableVector<A> slice(int startIndex, int endIndexExclusive)
Creates a slice of thisImmutableVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
ImmutableVector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, useVector.copySliceFrom(int, int, java.lang.Iterable<A>)
instead.- Specified by:
slice
in interfaceVector<A>
- Parameters:
startIndex
- the index of the element to begin the slice. Must be >= 0. May exceed the size of thisImmutableVector
, in which case an emptyImmutableVector
will be returned.endIndexExclusive
- the end index (exclusive) of the slice. Must be >=startIndex
. May exceed the size of thisImmutableVector
, in which case the slice will contain as many elements as available.- Returns:
- an
ImmutableVector<A>
-
slide
default software.kes.enhancediterables.ImmutableFiniteIterable<? extends ImmutableNonEmptyVector<A>> slide(int k)
"Slides" a window ofk
elements across theImmutableVector
by one element at a time.Example:
Vector.of(1, 2, 3, 4, 5).slide(2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
- Specified by:
slide
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
slide
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
slide
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
slide
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
slide
in interfaceVector<A>
- Parameters:
k
- the number of elements in the sliding window. Must be >= 1.- Returns:
- an
FiniteIterable<ImmutableNonEmptyVector<A>>
-
span
default com.jnape.palatable.lambda.adt.hlist.Tuple2<ImmutableVector<A>,ImmutableVector<A>> span(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Splits thisImmutableVector
into a prefix/suffix pair according to a predicate.Does not make copies of any underlying collections.
Note that
vector.span(p)
is equivalent to, but possibly more efficient thantuple(vector.takeWhile(p), vector.dropWhile(p))
- Specified by:
span
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
span
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
span
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
span
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Parameters:
predicate
- a predicate; not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- a
Tuple2
contains ofImmutableVector
s, one of which containing the firstindex
elements that satisfied the predicate, the second containing the other elements.
-
splitAt
default com.jnape.palatable.lambda.adt.hlist.Tuple2<ImmutableVector<A>,ImmutableVector<A>> splitAt(int index)
Splits thisImmutableVector
into two at a given position.Does not make copies of any underlying collections.
Note that
vector.splitAt(n)
is equivalent to, but possibly more efficient thantuple(vector.take(n), vector.drop(n))
-
tails
default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<ImmutableVector<A>> tails()
Returns aNonEmptyIterable
containing the tails of thisImmutableVector
.The first value will be this
ImmutableVector
and the final one will be an emptyImmutableVector
, with the intervening values the results of successive applications oftail
.- Specified by:
tails
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
tails
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
tails
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
tails
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
tails
in interfaceVector<A>
- Returns:
- a
NonEmptyIterable
over all the tails of thisImmutableVector
-
take
default ImmutableVector<A> take(int count)
Returns a newImmutableVector
containing at most the firstcount
elements of thisImmutableVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
ImmutableVector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, useVector.copyFrom(int, Iterable)
instead.- Specified by:
take
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
take
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
take
in interfaceVector<A>
- Parameters:
count
- the maximum number of elements to take from thisImmutableVector
. Must be >= 0. May exceed size of thisImmutableVector
.- Returns:
- an
ImmutableVector<A>
-
takeRight
default ImmutableVector<A> takeRight(int count)
Returns a newImmutableVector
containing at most the lastcount
elements of thisImmutableVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
ImmutableVector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, useVector.copyFrom(int, Iterable)
instead.
-
takeWhile
default ImmutableVector<A> takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableVector
containing the longest prefix of elements thisImmutableVector
that satisfy a predicate.Does not make copies of any underlying collections.
- Specified by:
takeWhile
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
takeWhile
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
takeWhile
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
takeWhile
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Parameters:
predicate
- a predicate; not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableVector<A>
-
toImmutable
default ImmutableVector<A> toImmutable()
Returns anImmutableVector
containing the same elements as this one.Since this is an
ImmutableVector
already, this method simply returns itself.- Specified by:
toImmutable
in interfaceVector<A>
- Returns:
- itself
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableNonEmptyVector<A>> toNonEmpty()
Attempts to convert thisImmutableVector
to anImmutableNonEmptyVector
.If successful, returns a
ImmutableNonEmptyVector
containing the same elements as this one, wrapped in aMaybe.just(A)
.If this
ImmutableVector
is empty, returnsMaybe.nothing()
.Does not make copies of any underlying collections.
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.ImmutableFiniteIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.ImmutableIterable<A>
- Specified by:
toNonEmpty
in interfaceVector<A>
- Returns:
- a
Maybe<ImmutableNonEmptyVector<A>>
-
toNonEmptyOrThrow
default ImmutableNonEmptyVector<A> toNonEmptyOrThrow()
Attempts to convert thisImmutableVector
to anImmutableNonEmptyVector
.If successful, returns a
ImmutableNonEmptyVector
containing the same elements as this one. Use this if you are confident that thisImmutableVector
is not empty.If this
ImmutableVector
is empty, throws anIllegalArgumentException
.Does not make copies of any underlying collections.
- Specified by:
toNonEmptyOrThrow
in interfaceVector<A>
- Returns:
- an
ImmutableNonEmptyVector<A>
- Throws:
java.lang.IllegalArgumentException
- if thisImmutableVector
is empty
-
zipWith
default <B,C> ImmutableVector<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableVector<B> other)
Zips together thisImmutableVector
with anotherImmutableVector
by applying a zipping function.Applies the function to the successive elements of of each
ImmutableVector
until one of them runs out of elements.Does not make copies of any underlying collections.
- Type Parameters:
B
- The element type of the otherImmutableVector
C
- The element type of the result- Parameters:
fn
- The zipping function. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.other
- The otherImmutableVector
- Returns:
- An
ImmutableVector<C>
-
zipWithIndex
default ImmutableVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>> zipWithIndex()
Zips thisImmutableVector
with its indices.Does not make copies of any underlying collections.
- Specified by:
zipWithIndex
in interfaceVector<A>
- Returns:
- a new
ImmutableVector
containing pairs consisting of all elements of thisImmutableVector
paired with their index. Indices start at 0.
-
builder
static <A> VectorBuilder<A> builder()
Creates a newVectorBuilder
.- Type Parameters:
A
- the element type- Returns:
- an empty
VectorBuilder
-
builder
static <A> VectorBuilder<A> builder(int initialCapacity)
Creates a newVectorBuilder
with an initial capacity hint.- Type Parameters:
A
- the element type- Parameters:
initialCapacity
- an initial capacity hint. Must be >= 0.- Returns:
- an empty
VectorBuilder
-
-