Interface ImmutableIterable<A>
-
- Type Parameters:
A
- the element type
- All Superinterfaces:
EnhancedIterable<A>
,com.jnape.palatable.lambda.functor.Functor<A,EnhancedIterable<?>>
,java.lang.Iterable<A>
- All Known Subinterfaces:
ImmutableFiniteIterable<A>
,ImmutableNonEmptyFiniteIterable<A>
,ImmutableNonEmptyIterable<A>
public interface ImmutableIterable<A> extends EnhancedIterable<A>
AnEnhancedIterable
that is safe from mutation.May be infinite, finite, or empty.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default ImmutableNonEmptyIterable<A>
append(A element)
Lazily appends an element to the end of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.default ImmutableIterable<A>
concat(ImmutableIterable<A> other)
Lazily concatenates anotherImmutableIterable
to the end of thisImmutableIterable
, yielding a newImmutableIterable
.default ImmutableNonEmptyIterable<A>
concat(ImmutableNonEmptyIterable<A> other)
Lazily concatenates anImmutableNonEmptyIterable
to the end of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.static <A> ImmutableFiniteIterable<A>
copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableFiniteIterable
by copying elements from anIterable
.static <A> ImmutableFiniteIterable<A>
copyFrom(java.util.Collection<A> source)
Creates anImmutableFiniteIterable
by copying elements from aCollection
.static <A> ImmutableFiniteIterable<A>
copyFrom(FiniteIterable<A> source)
Creates anImmutableFiniteIterable
by copying elements from aFiniteIterable
.default ImmutableIterable<A>
drop(int count)
Returns a newImmutableIterable
that drops the firstcount
elements of thisImmutableIterable
.default ImmutableIterable<A>
dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that skips the first contiguous group of elements of thisImmutableIterable
that satisfy a predicate.static <A> ImmutableFiniteIterable<A>
emptyImmutableIterable()
Creates an emptyImmutableIterable
.default ImmutableIterable<A>
filter(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that contains all elements of thisImmutableIterable
that satisfy a predicate.default <B> ImmutableIterable<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Returns a newImmutableIterable
by applying a function to all elements of thisImmutableIterable
.default ImmutableIterable<A>
intersperse(A separator)
Returns a newImmutableIterable
with the provided separator value injected between each value of thisImmutableIterable
.default ImmutableIterable<? extends ImmutableNonEmptyIterable<A>>
magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anIterable
of contiguous groups of elements in thisImmutableIterable
that match a predicate pairwise.static <A> ImmutableNonEmptyFiniteIterable<A>
of(A first, A... more)
Creates anImmutableIterable
containing the given elements.default <B,C>
com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends ImmutableIterable<B>,? extends ImmutableIterable<C>>partition(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.adt.coproduct.CoProduct2<B,C,?>> function)
Partitions thisImmutableIterable
given a disjoint mapping function.default ImmutableNonEmptyIterable<A>
prepend(A element)
Lazily prepends an element to the front of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.default ImmutableIterable<A>
prependAll(A separator)
Returns a newImmutableIterable
with the provided separator value injected before each value of thisImmutableIterable
.static <A> ImmutableNonEmptyIterable<A>
repeat(A element)
Returns an infiniteImmutableNonEmptyIterable
that repeatedly iterates a given element.default ImmutableIterable<? extends ImmutableNonEmptyFiniteIterable<A>>
slide(int k)
"Slides" a window ofk
elements across theImmutableIterable
by one element at a time.default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends ImmutableIterable<A>,? extends ImmutableIterable<A>>
span(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns aTuple2
where the first slot is the front contiguous elements of thisImmutableIterable
matching a predicate and the second slot is all the remaining elements.default ImmutableNonEmptyIterable<? extends ImmutableIterable<A>>
tails()
Returns anImmutableNonEmptyIterable
containing all of the subsequences of tail elements of thisImmutableIterable
, ordered by size, starting with the full list.default ImmutableFiniteIterable<A>
take(int count)
Returns a newImmutableFiniteIterable
that takes the firstcount
elements of thisImmutableIterable
.default ImmutableIterable<A>
takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that limits to the first contiguous group of elements of thisFiniteIterable
that satisfy a predicate.default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableFiniteIterable<A>>
toFinite()
Converts thisImmutableIterable
to anImmutableFiniteIterable
if there is enough information to do so without iterating it.default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableNonEmptyIterable<A>>
toNonEmpty()
Converts thisImmutableIterable
to aImmutableNonEmptyIterable
if it contains one or more elements.default <B,C>
ImmutableFiniteIterable<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableFiniteIterable<B> other)
Zips together thisImmutableIterable
with anImmutableFiniteIterable
by applying a zipping function.default <B,C>
ImmutableIterable<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableIterable<B> other)
Zips together thisImmutableIterable
with anotherImmutableIterable
by applying a zipping function.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, find, isEmpty, toArray, toCollection, zipWith, zipWith, zipWith
-
-
-
-
Method Detail
-
append
default ImmutableNonEmptyIterable<A> append(A element)
Lazily appends an element to the end of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.- Specified by:
append
in interfaceEnhancedIterable<A>
- Parameters:
element
- the element to append- Returns:
- a
ImmutableNonEmptyIterable<A>
-
concat
default ImmutableIterable<A> concat(ImmutableIterable<A> other)
Lazily concatenates anotherImmutableIterable
to the end of thisImmutableIterable
, yielding a newImmutableIterable
.- Parameters:
other
- the otherImmutableIterable
- Returns:
- an
ImmutableIterable<A>
-
concat
default ImmutableNonEmptyIterable<A> concat(ImmutableNonEmptyIterable<A> other)
Lazily concatenates anImmutableNonEmptyIterable
to the end of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.- Parameters:
other
- anImmutableNonEmptyIterable
- Returns:
- an
ImmutableNonEmptyIterable<A>
-
drop
default ImmutableIterable<A> drop(int count)
Returns a newImmutableIterable
that drops the firstcount
elements of thisImmutableIterable
.- Specified by:
drop
in interfaceEnhancedIterable<A>
- Parameters:
count
- the number of elements to drop from thisImmutableIterable
. Must be >= 0. May exceed size of thisImmutableIterable
, in which case, the result will be an emptyImmutableIterable
.- Returns:
- an
ImmutableIterable<A>
-
dropWhile
default ImmutableIterable<A> dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that skips the first contiguous group of elements of thisImmutableIterable
that satisfy a predicate.Iteration begins at the first element for which the predicate evaluates to false.
- Specified by:
dropWhile
in interfaceEnhancedIterable<A>
- Parameters:
predicate
- a predicate; should be referentially transparent and not have side-effects- Returns:
- a
ImmutableIterable<A>
-
filter
default ImmutableIterable<A> filter(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that contains all elements of thisImmutableIterable
that satisfy a predicate.- Specified by:
filter
in interfaceEnhancedIterable<A>
- Parameters:
predicate
- a predicate; should be referentially transparent and not have side-effects- Returns:
- a
ImmutableIterable<A>
-
fmap
default <B> ImmutableIterable<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Returns a newImmutableIterable
by applying a function to all elements of thisImmutableIterable
.- Specified by:
fmap
in interfaceEnhancedIterable<A>
- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.functor.Functor<A,EnhancedIterable<?>>
- Type Parameters:
B
- the type returned byf
- Parameters:
f
- a function fromA
toB
. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- a
ImmutableIterable<B>
-
intersperse
default ImmutableIterable<A> intersperse(A separator)
Returns a newImmutableIterable
with the provided separator value injected between each value of thisImmutableIterable
.If this
ImmutableIterable
contains fewer than two elements, it is left untouched.- Specified by:
intersperse
in interfaceEnhancedIterable<A>
- Parameters:
separator
- the separator value- Returns:
- a
ImmutableIterable<A>
-
magnetizeBy
default ImmutableIterable<? extends ImmutableNonEmptyIterable<A>> magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anIterable
of contiguous groups of elements in thisImmutableIterable
that match a predicate pairwise.- Specified by:
magnetizeBy
in interfaceEnhancedIterable<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
ImmutableIterable<ImmutableNonEmptyIterable<A>>
containing the contiguous groups
-
partition
default <B,C> com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends ImmutableIterable<B>,? extends ImmutableIterable<C>> partition(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.adt.coproduct.CoProduct2<B,C,?>> function)
Partitions thisImmutableIterable
given a disjoint mapping function.Note that while the returned tuple must be constructed eagerly, the left and right iterables contained therein are both lazy, so comprehension over infinite iterables is supported.
- Specified by:
partition
in interfaceEnhancedIterable<A>
- Type Parameters:
B
- the output left Iterable element type, as well as the CoProduct2 A typeC
- the output right Iterable element type, as well as the CoProduct2 B type- Parameters:
function
- the mapping function- Returns:
- a
Tuple2<ImmutableIterable<B>, ImmutableIterable<C>>
-
prepend
default ImmutableNonEmptyIterable<A> prepend(A element)
Lazily prepends an element to the front of thisImmutableIterable
, yielding a newImmutableNonEmptyIterable
.- Specified by:
prepend
in interfaceEnhancedIterable<A>
- Parameters:
element
- the element to prepend- Returns:
- an
ImmutableNonEmptyIterable<A>
-
prependAll
default ImmutableIterable<A> prependAll(A separator)
Returns a newImmutableIterable
with the provided separator value injected before each value of thisImmutableIterable
.If this
ImmutableIterable
is empty, it is left untouched.- Specified by:
prependAll
in interfaceEnhancedIterable<A>
- Parameters:
separator
- the separator value- Returns:
- a
ImmutableIterable<A>
-
slide
default ImmutableIterable<? extends ImmutableNonEmptyFiniteIterable<A>> slide(int k)
"Slides" a window ofk
elements across theImmutableIterable
by one element at a time.Example:
ImmutableIterable.of(1, 2, 3, 4, 5).slide(2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
- Specified by:
slide
in interfaceEnhancedIterable<A>
- Parameters:
k
- the number of elements in the sliding window. Must be >= 1.- Returns:
- an
ImmutableIterable<ImmutableNonEmptyFiniteIterable<A>>
-
span
default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends ImmutableIterable<A>,? extends ImmutableIterable<A>> span(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns aTuple2
where the first slot is the front contiguous elements of thisImmutableIterable
matching a predicate and the second slot is all the remaining elements.- Specified by:
span
in interfaceEnhancedIterable<A>
- Parameters:
predicate
- a predicate; should be referentially transparent and not have side-effects- Returns:
- a
Tuple2<ImmutableIterable<B>, ImmutableIterable<C>>
-
tails
default ImmutableNonEmptyIterable<? extends ImmutableIterable<A>> tails()
Returns anImmutableNonEmptyIterable
containing all of the subsequences of tail elements of thisImmutableIterable
, ordered by size, starting with the full list. Example:ImmutableIterable.of(1, 2, 3).tails(); // [[1, 2, 3], [2, 3], [3], []]
- Specified by:
tails
in interfaceEnhancedIterable<A>
- Returns:
- an
ImmutableNonEmptyIterable<ImmutableIterable<A>>
-
take
default ImmutableFiniteIterable<A> take(int count)
Returns a newImmutableFiniteIterable
that takes the firstcount
elements of thisImmutableIterable
.- Specified by:
take
in interfaceEnhancedIterable<A>
- Parameters:
count
- the number of elements to take from thisEnhancedIterable
. Must be >= 0. May exceed size of thisImmutableIterable
, in which case, the result will contain as many elements available.- Returns:
- an
ImmutableFiniteIterable<A>
-
takeWhile
default ImmutableIterable<A> takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newImmutableIterable
that limits to the first contiguous group of elements of thisFiniteIterable
that satisfy a predicate.Iteration ends at, but does not include, the first element for which the predicate evaluates to false.
- Specified by:
takeWhile
in interfaceEnhancedIterable<A>
- Parameters:
predicate
- a predicate; should be referentially transparent and not have side-effects- Returns:
- a
ImmutableIterable<A>
-
toFinite
default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableFiniteIterable<A>> toFinite()
Converts thisImmutableIterable
to anImmutableFiniteIterable
if there is enough information to do so without iterating it.Note that if this method returns
nothing()
, it does NOT necessarily mean thisImmutableIterable
is infinite.- Specified by:
toFinite
in interfaceEnhancedIterable<A>
- Returns:
- a
Maybe<ImmutableFiniteIterable<A>
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends ImmutableNonEmptyIterable<A>> toNonEmpty()
Converts thisImmutableIterable
to aImmutableNonEmptyIterable
if it contains one or more elements.- Specified by:
toNonEmpty
in interfaceEnhancedIterable<A>
- Returns:
- a
Maybe<ImmutableNonEmptyIterable<A>
-
zipWith
default <B,C> ImmutableIterable<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableIterable<B> other)
Zips together thisImmutableIterable
with anotherImmutableIterable
by applying a zipping function.Applies the function to the successive elements of each
Iterable
until one of them runs out of elements.- Type Parameters:
B
- the element type of the otherIterable
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 otherIterable
- Returns:
- an
ImmutableIterable<C>
-
zipWith
default <B,C> ImmutableFiniteIterable<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, ImmutableFiniteIterable<B> other)
Zips together thisImmutableIterable
with anImmutableFiniteIterable
by applying a zipping function.Applies the function to the successive elements of each
Iterable
until one of them runs out of elements.- Type Parameters:
B
- the element type of the otherIterable
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 otherIterable
- Returns:
- an
ImmutableFiniteIterable<C>
-
copyFrom
static <A> ImmutableFiniteIterable<A> copyFrom(FiniteIterable<A> source)
Creates anImmutableFiniteIterable
by copying elements from aFiniteIterable
.If
source
is already anImmutableFiniteIterable
, this method will return it without copying.- Type Parameters:
A
- the element type- Parameters:
source
- the source to copy from- Returns:
- an
ImmutableFiniteIterable<A>
-
copyFrom
static <A> ImmutableFiniteIterable<A> copyFrom(java.util.Collection<A> source)
Creates anImmutableFiniteIterable
by copying elements from aCollection
.- Type Parameters:
A
- the element type- Parameters:
source
- the source to copy from- Returns:
- an
ImmutableFiniteIterable<A>
-
copyFrom
static <A> ImmutableFiniteIterable<A> copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableFiniteIterable
by copying elements from anIterable
.If
source
is already anImmutableIterable
, no copying will be performed.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to take from the suppliedIterable
. Must be >= 0. May exceed size of theIterable
, in which case, the result will contain as many elements available.source
- the source to copy from- Returns:
- an
ImmutableFiniteIterable<A>
-
emptyImmutableIterable
static <A> ImmutableFiniteIterable<A> emptyImmutableIterable()
Creates an emptyImmutableIterable
.- Type Parameters:
A
- the element type- Returns:
- an
ImmutableFiniteIterable<A>
-
of
@SafeVarargs static <A> ImmutableNonEmptyFiniteIterable<A> of(A first, A... more)
Creates anImmutableIterable
containing the given elements.Note that this method actually returns an
ImmutableNonEmptyFiniteIterable
, which is also anImmutableIterable
.- Type Parameters:
A
- the element type- Parameters:
first
- the first elementmore
- the remaining elements- Returns:
- an
ImmutableNonEmptyFiniteIterable<A>
-
repeat
static <A> ImmutableNonEmptyIterable<A> repeat(A element)
Returns an infiniteImmutableNonEmptyIterable
that repeatedly iterates a given element.- Type Parameters:
A
- the element type- Parameters:
element
- the value to repeat- Returns:
- an
ImmutableNonEmptyIterable<A>
-
-