Interface NonEmptyVector<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<?>>
,java.lang.Iterable<A>
,software.kes.enhancediterables.NonEmptyFiniteIterable<A>
,software.kes.enhancediterables.NonEmptyIterable<A>
,java.util.RandomAccess
,Vector<A>
- All Known Subinterfaces:
ImmutableNonEmptyVector<A>
public interface NonEmptyVector<A> extends software.kes.enhancediterables.NonEmptyFiniteIterable<A>, Vector<A>
AVector
that is guaranteed at compile-time to contain at least one element.In addition to guarantees of
Vector
, provides the following benefits:head()
method that returns the first element.- Implements
NonEmptyIterable
. fmap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends B>)
always returns aNonEmptyVector
.reverse()
always returns aNonEmptyVector
.zipWithIndex()
always returns aNonEmptyVector
.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <A> NonEmptyVectorBuilder<A>
builder(int initialCapacity, A first)
Creates a newNonEmptyVectorBuilder
with an initial capacity hint.static <A> NonEmptyVectorBuilder<A>
builder(A first)
Creates a newNonEmptyVectorBuilder
.static <A> ImmutableNonEmptyVector<A>
copyFromOrThrow(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array, but with a maximum number of elements.static <A> ImmutableNonEmptyVector<A>
copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
from anyIterable
, but consuming a maximum number of elements.static <A> ImmutableNonEmptyVector<A>
copyFromOrThrow(A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array.static <A> ImmutableNonEmptyVector<A>
copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
from anyIterable
.default <B> NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>
cross(NonEmptyVector<B> other)
Returns the cartesian product of thisNonEmptyVector
with anotherNonEmptyVector
.static <A> ImmutableNonEmptyVector<A>
fill(int size, A value)
Creates anImmutableNonEmptyVector
that repeats the same elementsize
times.default <B> NonEmptyVector<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisNonEmptyVector
.default A
head()
Returns the first element.default ImmutableNonEmptyVector<java.lang.Integer>
indices()
Returns anImmutableNonEmptyVector<Integer>
that contains all the indices of thisNonEmptyVector
.default Vector<A>
init()
Returns the init of thisNonEmptyVector
.default boolean
isEmpty()
Tests whether thisNonEmptyVector
is empty.default java.util.Iterator<A>
iterator()
Returns an iterator over thisNonEmptyVector
's elements.default A
last()
Returns the last element.static <A> ImmutableNonEmptyVector<A>
lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)
Creates anImmutableNonEmptyVector
where elements are lazily evaluated.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>
maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array, but with a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>
maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
that is copied from anyIterable
, but consuming a maximum number of elements.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>
maybeCopyFrom(A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array.static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>>
maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
that is copied from anyIterable
.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>>
maybeWrap(A[] underlying)
Attempts to create aNonEmptyVector
that wraps an array.static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>>
maybeWrap(java.util.List<A> underlying)
Attempts to create aNonEmptyVector
that wraps ajava.util.List
.static <A> ImmutableNonEmptyVector<A>
nonEmptyCopyFrom(int maxCount, software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVector
that is copied from anyNonEmptyIterable
, but consuming a maximum number of elements.static <A> ImmutableNonEmptyVector<A>
nonEmptyCopyFrom(software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVector
that is copied from anyNonEmptyIterable
.static <A> ImmutableNonEmptyVector<A>
of(A first, A... more)
Creates aImmutableNonEmptyVector
with the given elements.static ImmutableNonEmptyVector<java.lang.Integer>
range(int size)
Creates anImmutableNonEmptyVector<Integer>
containing elements 0..size - 1
.default NonEmptyVector<A>
reverse()
Creates aNonEmptyVector
with thisNonEmptyVector
's elements in reversed order.default Vector<A>
tail()
Returns the tail of thisNonEmptyVector
.default ImmutableNonEmptyVector<A>
toImmutable()
Converts thisNonEmptyVector
to anImmutableNonEmptyVector
.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>>
toNonEmpty()
Attempts to convert thisVector
to aNonEmptyVector
.default NonEmptyVector<A>
toNonEmptyOrThrow()
Attempts to convert thisVector
to aNonEmptyVector
.static <A> NonEmptyVector<A>
wrapOrThrow(A[] underlying)
Attempts to create aNonEmptyVector
that wraps an array.static <A> NonEmptyVector<A>
wrapOrThrow(java.util.List<A> underlying)
Attempts to create aNonEmptyVector
that wraps ajava.util.List
.default <B,C>
NonEmptyVector<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, NonEmptyVector<B> other)
Zips together thisNonEmptyVector
with anotherNonEmptyVector
by applying a zipping function.default NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>>
zipWithIndex()
Zips thisNonEmptyVector
with its indices.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, find, toArray, toCollection, zipWith, zipWith
-
Methods inherited from interface software.kes.enhancediterables.FiniteIterable
append, concat, cross, cross, dropWhile, filter, foldLeft, foldRight, partition, prepend, span, takeWhile, zipWith
-
-
-
-
Method Detail
-
cross
default <B> NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(NonEmptyVector<B> other)
Returns the cartesian product of thisNonEmptyVector
with anotherNonEmptyVector
.Does not make copies of any underlying collections.
The returned
NonEmptyVector
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 otherNonEmptyVector
- Parameters:
other
- aNonEmptyVector
of any type- Returns:
- a
NonEmptyVector<Tuple2<A, B>>
-
fmap
default <B> NonEmptyVector<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisNonEmptyVector
.Returns a new
NonEmptyVector
of the same size (but possibly a different type).Does not make any copies of underlying collections.
This method is stack-safe, so a
NonEmptyVector
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.NonEmptyFiniteIterable<A>
- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.NonEmptyIterable<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:
- a
NonEmptyVector<B>
of the same size
-
head
default A head()
Returns the first element.- Specified by:
head
in interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>
- Returns:
- an element of type
A
-
indices
default ImmutableNonEmptyVector<java.lang.Integer> indices()
Returns anImmutableNonEmptyVector<Integer>
that contains all the indices of thisNonEmptyVector
.
-
init
default Vector<A> init()
Returns the init of thisNonEmptyVector
.The init of a
NonEmptyVector
is the sameVector
with the last element dropped. May be empty.Does not make copies of any underlying collections.
- Specified by:
init
in interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>
- Returns:
- a
Vector<A>
-
isEmpty
default boolean isEmpty()
Tests whether thisNonEmptyVector
is empty.Always returns false for
NonEmptyVector
s.
-
iterator
default java.util.Iterator<A> iterator()
Returns an iterator over thisNonEmptyVector
's elements.
-
last
default A last()
Returns the last element.- Specified by:
last
in interfacesoftware.kes.enhancediterables.NonEmptyFiniteIterable<A>
- Returns:
- an element of type
A
-
reverse
default NonEmptyVector<A> reverse()
Creates aNonEmptyVector
with thisNonEmptyVector
's elements in reversed order.Does not make copies of any underlying collections.
-
tail
default Vector<A> tail()
Returns the tail of thisNonEmptyVector
.The tail of a
NonEmptyVector
is the sameVector
with the first element dropped. May be empty.Does not make copies of any underlying collections.
-
toImmutable
default ImmutableNonEmptyVector<A> toImmutable()
Converts thisNonEmptyVector
to anImmutableNonEmptyVector
.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
NonEmptyVector
is already anImmutableNonEmptyVector
, no copies are made and this method is a no-op.- Specified by:
toImmutable
in interfaceVector<A>
- Returns:
- an
ImmutableNonEmptyVector
of the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>> toNonEmpty()
Attempts to convert thisVector
to aNonEmptyVector
.Since this will always be successful for
NonEmptyVector
s, this method always returns itself wrapped in aMaybe.just(A)
.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.NonEmptyFiniteIterable<A>
- Specified by:
toNonEmpty
in interfacesoftware.kes.enhancediterables.NonEmptyIterable<A>
- Specified by:
toNonEmpty
in interfaceVector<A>
- Returns:
- this
NonEmptyVector
wrapped in aMaybe.just(A)
-
toNonEmptyOrThrow
default NonEmptyVector<A> toNonEmptyOrThrow()
Attempts to convert thisVector
to aNonEmptyVector
.Since this will always be successful for
NonEmptyVector
s, this method always returns itself.Does not make copies of any underlying collections.
- Specified by:
toNonEmptyOrThrow
in interfaceVector<A>
- Returns:
- this
NonEmptyVector
-
zipWith
default <B,C> NonEmptyVector<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, NonEmptyVector<B> other)
Zips together thisNonEmptyVector
with anotherNonEmptyVector
by applying a zipping function.Applies the function to the successive elements of of each
NonEmptyVector
until one of them runs out of elements.Does not make copies of any underlying collections.
- Type Parameters:
B
- The element type of the otherNonEmptyVector
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 otherNonEmptyVector
- Returns:
- A
NonEmptyVector<C>
-
zipWithIndex
default NonEmptyVector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>> zipWithIndex()
Zips thisNonEmptyVector
with its indices.Does not make copies of any underlying collections.
- Specified by:
zipWithIndex
in interfaceVector<A>
- Returns:
- a new
NonEmptyVector
containing pairs consisting of all elements of thisNonEmptyVector
paired with their index. Indices start at 0.
-
of
@SafeVarargs static <A> ImmutableNonEmptyVector<A> of(A first, A... more)
Creates aImmutableNonEmptyVector
with the given elements.- Type Parameters:
A
- the element type- Parameters:
first
- the first elementmore
- the remaining elements- Returns:
- an
ImmutableNonEmptyVector<A>
-
builder
static <A> NonEmptyVectorBuilder<A> builder(A first)
Creates a newNonEmptyVectorBuilder
.- Type Parameters:
A
- the element type- Parameters:
first
- the first element- Returns:
- an empty
VectorBuilder
-
builder
static <A> NonEmptyVectorBuilder<A> builder(int initialCapacity, A first)
Creates a newNonEmptyVectorBuilder
with an initial capacity hint.- Type Parameters:
A
- the element type- Parameters:
initialCapacity
- an initial capacity hint. Must be >= 0.first
- the first element- Returns:
- an empty
VectorBuilder
-
maybeWrap
static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>> maybeWrap(A[] underlying)
Attempts to create aNonEmptyVector
that wraps an array.Does not make any copies of the given array. The created
NonEmptyVector
will hold on to a reference to the array, but will never alter it in any way.Bearers of the created
NonEmptyVector
will be unable to gain access to the underlying array, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the array can still mutate it. Use
Vector.copyFrom(java.lang.Iterable<A>)
instead if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
- the array to wrap; not null- Returns:
- a
NonEmptyVector<A>
wrapped in aMaybe.just(A)
ifunderlying
is non-empty;Maybe.nothing()
otherwise.
-
maybeWrap
static <A> com.jnape.palatable.lambda.adt.Maybe<NonEmptyVector<A>> maybeWrap(java.util.List<A> underlying)
Attempts to create aNonEmptyVector
that wraps ajava.util.List
.Does not make any copies of the given
List
. The createdNonEmptyVector
will hold a reference to the givenList
, but will not alter it in any way.Bearers of the
NonEmptyVector
will be unable to gain access to the underlyingList
, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the
List
can still mutate it. Mutating theList
is not advised. Operations that change the size of the underlyingList
will result in unpredictable behavior. UseVector.copyFrom(java.lang.Iterable<A>)
if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
-java.util.List
to wrap; not null- Returns:
- a
NonEmptyVector<A>
wrapped in aMaybe.just(A)
ifunderlying
is non-empty;Maybe.nothing()
otherwise.
-
wrapOrThrow
static <A> NonEmptyVector<A> wrapOrThrow(A[] underlying)
Attempts to create aNonEmptyVector
that wraps an array. If it is not possible, throws anIllegalArgumentException
.Does not make any copies of the given array. The created
NonEmptyVector
will hold on to a reference to the array, but will never alter it in any way.Bearers of the created
NonEmptyVector
will be unable to gain access to the underlying array, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the array can still mutate it. Use
copyFromOrThrow(Object[])
instead if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
- array to wrap; not null- Returns:
- a
NonEmptyVector<A>
ifunderlying
is non-empty; throws anIllegalArgumentException
otherwise
-
wrapOrThrow
static <A> NonEmptyVector<A> wrapOrThrow(java.util.List<A> underlying)
Attempts to create aNonEmptyVector
that wraps ajava.util.List
. If it is not possible, throws anIllegalArgumentException
.Does not make any copies of the given
List
. The createdNonEmptyVector
will hold a reference to the givenjava.util.List
, but will not alter it in any way.Bearers of the created
NonEmptyVector
will be unable to gain access to the underlyingList
, it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the
List
can still mutate it. Mutating theList
is not advised. Operations that change the size of the underlyingList
will result in unpredictable behavior. UsecopyFromOrThrow(Iterable)
if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
-List
to wrap; not null- Returns:
- a
NonEmptyVector<A>
ifunderlying
is non-empty; throws anIllegalArgumentException
otherwise
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
that is copied from anyIterable
.The entire
Iterable
will be eagerly iterated. Be careful not to pass in an infiniteIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
already is anImmutableNonEmptyVector
, it will be returned directly.- Type Parameters:
A
- the element type- Parameters:
source
- anIterable<A>
that may be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptyVector<A>
wrapped in aMaybe.just(A)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array.- Type Parameters:
A
- the element type- Parameters:
source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptyVector<A>
wrapped in aMaybe.just(A)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
that is copied from anyIterable
, but consuming a maximum number of elements.The
Iterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is anImmutableVector
and its size is less than or equal tomaxCount
, in which case it will be returned directly.If
source
is anImmutableVector
that is greater thanmaxCount
in size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVector
that you no longer need.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to consume from the source. Must be >= 0. If 0, this method will always returnMaybe.nothing()
.source
- anIterable<A>
that will be iterated eagerly for up tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableNonEmptyVector<A>
wrapped in aMaybe.just(A)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
maybeCopyFrom
static <A> com.jnape.palatable.lambda.adt.Maybe<ImmutableNonEmptyVector<A>> maybeCopyFrom(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array, but with a maximum number of elements.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to copy from the array. Must be >= 0. If 0, this method will always returnMaybe.nothing()
.source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptyVector<A>
wrapped in aMaybe.just(A)
ifsource
is non-empty;Maybe.nothing()
otherwise.
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
from anyIterable
. If theIterable
is empty, throws anIllegalArgumentException
.The entire
Iterable
will be eagerly iterated. Be careful not to pass in an infiniteIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
already is anImmutableVector
, it will be returned directly.- Type Parameters:
A
- the element type- Parameters:
source
- anIterable<A>
that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array. If the array is empty, throws anIllegalArgumentException
.- Type Parameters:
A
- the element type- Parameters:
source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(int maxCount, java.lang.Iterable<A> source)
Attempts to create anImmutableNonEmptyVector
from anyIterable
, but consuming a maximum number of elements. If theIterable
is empty, throws anIllegalArgumentException
.The
Iterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is anImmutableVector
and its size is less than or equal tomaxCount
, in which case it will be returned directly.If
source
is anImmutableVector
that is greater thanmaxCount
in size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVector
that you no longer need.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to consume from the source. Must be >= 1.source
- anIterable<A>
that will be iterated eagerly for up tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableNonEmptyVector<A>
-
copyFromOrThrow
static <A> ImmutableNonEmptyVector<A> copyFromOrThrow(int maxCount, A[] source)
Attempts to create anImmutableNonEmptyVector
that is copied from an array, but with a maximum number of elements. If the array is empty, throws anIllegalArgumentException
.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to copy from the array. Must be >= 1.source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableNonEmptyVector<A>
-
fill
static <A> ImmutableNonEmptyVector<A> fill(int size, A value)
Creates anImmutableNonEmptyVector
that repeats the same elementsize
times.Uses O(1) memory.
- Type Parameters:
A
- the element type- Parameters:
size
- the number of elements. Must be >= 1.value
- the value that will be repeated all elements of theImmutableNonEmptyVector
- Returns:
- an
ImmutableVector<A>
ofsize
elements, with each element having the valuevalue
-
lazyFill
static <A> ImmutableNonEmptyVector<A> lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)
Creates anImmutableNonEmptyVector
where elements are lazily evaluated.Uses O(1) memory.
- Type Parameters:
A
- the element type- Parameters:
size
- the number of elements. Must be >= 1.valueSupplier
- a function that accepts an index and returns the computed value for that index. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableNonEmptyVector<A>
-
nonEmptyCopyFrom
static <A> ImmutableNonEmptyVector<A> nonEmptyCopyFrom(software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVector
that is copied from anyNonEmptyIterable
.The entire
NonEmptyIterable
will be eagerly iterated. Be careful not to pass in an infiniteNonEmptyIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
is an untransformedImmutableNonEmptyVector
, it will be returned directly.- Type Parameters:
A
- the element type- Parameters:
source
- aNonEmptyIterable<A>
that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableNonEmptyVector<A>
-
nonEmptyCopyFrom
static <A> ImmutableNonEmptyVector<A> nonEmptyCopyFrom(int maxCount, software.kes.enhancediterables.NonEmptyIterable<A> source)
Creates anImmutableNonEmptyVector
that is copied from anyNonEmptyIterable
, but consuming a maximum number of elements.The
NonEmptyIterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is an untransformedImmutableNonEmptyVector
and its size is less than or equal tomaxCount
, in which case it will be returned directly.If
source
is anImmutableNonEmptyVector
that is greater thanmaxCount
in size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVector
that you no longer need.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to consume from the source. Must be >= 1.source
- aNonEmptyIterable<A>
that will be iterated eagerly for up tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableNonEmptyVector
that contains at mostmaxCount
elements
-
range
static ImmutableNonEmptyVector<java.lang.Integer> range(int size)
Creates anImmutableNonEmptyVector<Integer>
containing elements 0..size - 1
. In other words, each element of the returnedVector
will contains its index.Uses O(1) memory.
- Parameters:
size
- the number of elements. Must be >= 1.- Returns:
- an
ImmutableNonEmptyVector<Integer>
-
-