Interface FiniteIterable<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>
,NonEmptyFiniteIterable<A>
public interface FiniteIterable<A> extends EnhancedIterable<A>
AnEnhancedIterable
that is finite.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default NonEmptyFiniteIterable<A>
append(A element)
Lazily appends an element to the end of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.default FiniteIterable<A>
concat(java.util.Collection<A> other)
Lazily concatenates aCollection
to the end of thisFiniteIterable
, yielding a newFiniteIterable
.default FiniteIterable<A>
concat(FiniteIterable<A> other)
Lazily concatenates anotherFiniteIterable
to the end of thisFiniteIterable
, yielding a newFiniteIterable
.default NonEmptyFiniteIterable<A>
concat(NonEmptyFiniteIterable<A> other)
Lazily concatenates aNonEmptyFiniteIterable
to the end of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.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 <B> FiniteIterable<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>
cross(java.util.Collection<B> other)
Returns the lazily computed cartesian product of thisFiniteIterable
with aCollection
.default <B> FiniteIterable<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>
cross(FiniteIterable<B> other)
Returns the lazily computed cartesian product of thisFiniteIterable
with anotherFiniteIterable
.default EnhancedIterable<A>
cycle()
Returns an infiniteEnhancedIterable
that repeatedly cycles thisFiniteIterable
's elements, in order.default FiniteIterable<A>
distinct()
Returns aFiniteIterable
of the distinct values from thisFiniteIterable
.default FiniteIterable<A>
drop(int count)
Returns a newFiniteIterable
that drops the firstcount
elements of thisFiniteIterable
.default FiniteIterable<A>
dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
that skips the first contiguous group of elements of thisFiniteIterable
that satisfy a predicate.static <A> ImmutableFiniteIterable<A>
emptyFiniteIterable()
Creates an emptyFiniteIterable
.default FiniteIterable<A>
filter(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
that contains all elements of thisFiniteIterable
that satisfy a predicate.static <A> FiniteIterable<A>
finiteIterable(int maxCount, java.lang.Iterable<A> iterable)
Creates aFiniteIterable
by wrapping anIterable
.static <A> FiniteIterable<A>
finiteIterable(java.util.Collection<A> collection)
Creates aFiniteIterable
by wrapping aCollection
.default <B> FiniteIterable<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Returns a newFiniteIterable
by applying a function to all elements of thisFiniteIterable
.default <B> B
foldLeft(com.jnape.palatable.lambda.functions.Fn2<? super B,? super A,? extends B> op, B z)
Applies a binary operator to a start value and all elements of thisFiniteIterable
, going left to right.default <B> com.jnape.palatable.lambda.functor.builtin.Lazy<B>
foldRight(com.jnape.palatable.lambda.functions.Fn2<? super A,? super com.jnape.palatable.lambda.functor.builtin.Lazy<B>,? extends com.jnape.palatable.lambda.functor.builtin.Lazy<B>> op, com.jnape.palatable.lambda.functor.builtin.Lazy<B> z)
Applies a binary operator to a start value and all elements of thisFiniteIterable
, going right to left.default ImmutableNonEmptyFiniteIterable<? extends FiniteIterable<A>>
inits()
Returns aImmutableNonEmptyIterable
containing all of the subsequences of initial elements of thisFiniteIterable
, ordered by size, starting with the empty list.default FiniteIterable<A>
intersperse(A separator)
Returns a newFiniteIterable
with the provided separator value injected between each value of thisFiniteIterable
.default FiniteIterable<? extends NonEmptyFiniteIterable<A>>
magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anIterable
of contiguous groups of elements in thisFiniteIterable
that match a predicate pairwise.static <A> ImmutableNonEmptyFiniteIterable<A>
of(A first, A... more)
Creates aFiniteIterable
containing the given elements.default <B,C>
com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends FiniteIterable<B>,? extends FiniteIterable<C>>partition(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.adt.coproduct.CoProduct2<B,C,?>> function)
Partitions thisFiniteIterable
given a disjoint mapping function.default NonEmptyFiniteIterable<A>
prepend(A element)
Lazily prepends an element to the front of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.default FiniteIterable<A>
prependAll(A separator)
Returns a newFiniteIterable
with the provided separator value injected before each value of thisFiniteIterable
.default FiniteIterable<A>
reverse()
Returns a reversed representation of thisFiniteIterable
.default int
size()
Returns the number of elements in thisFiniteIterable
.default FiniteIterable<? extends NonEmptyFiniteIterable<A>>
slide(int k)
"Slides" a window ofk
elements across theFiniteIterable
by one element at a time.default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends FiniteIterable<A>,? extends FiniteIterable<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 thisFiniteIterable
matching a predicate and the second slot is all the remaining elements.default ImmutableNonEmptyIterable<? extends FiniteIterable<A>>
tails()
Returns anImmutableNonEmptyIterable
containing all of the subsequences of tail elements of thisFiniteIterable
, ordered by size, starting with the full list.default FiniteIterable<A>
takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
that limits to the first contiguous group of elements of thisFiniteIterable
that satisfy a predicate.default com.jnape.palatable.lambda.adt.Maybe<? extends FiniteIterable<A>>
toFinite()
Always succeeds becauseFiniteIterable
s are always finite.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyFiniteIterable<A>>
toNonEmpty()
Converts thisFiniteIterable
to aNonEmptyFiniteIterable
if it contains one or more elements.default <B,C>
FiniteIterable<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, java.lang.Iterable<B> other)
Zips together thisFiniteIterable
with anotherIterable
by applying a zipping function.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, find, isEmpty, take, toArray, toCollection, zipWith, zipWith
-
-
-
-
Method Detail
-
append
default NonEmptyFiniteIterable<A> append(A element)
Lazily appends an element to the end of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.- Specified by:
append
in interfaceEnhancedIterable<A>
- Parameters:
element
- the element to append- Returns:
- a
NonEmptyFiniteIterable<A>
-
concat
default FiniteIterable<A> concat(FiniteIterable<A> other)
Lazily concatenates anotherFiniteIterable
to the end of thisFiniteIterable
, yielding a newFiniteIterable
.- Parameters:
other
- the otherFiniteIterable
- Returns:
- a
FiniteIterable<A>
-
concat
default FiniteIterable<A> concat(java.util.Collection<A> other)
Lazily concatenates aCollection
to the end of thisFiniteIterable
, yielding a newFiniteIterable
.- Parameters:
other
- aCollection
- Returns:
- a
FiniteIterable<A>
-
concat
default NonEmptyFiniteIterable<A> concat(NonEmptyFiniteIterable<A> other)
Lazily concatenates aNonEmptyFiniteIterable
to the end of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.- Parameters:
other
- aNonEmptyFiniteIterable
- Returns:
- a
NonEmptyFiniteIterable<A>
-
cross
default <B> FiniteIterable<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(FiniteIterable<B> other)
Returns the lazily computed cartesian product of thisFiniteIterable
with anotherFiniteIterable
.- Type Parameters:
B
- the type of the otherFiniteIterable
- Parameters:
other
- aFiniteIterable
of any type- Returns:
- a
FiniteIterable<Tuple2<A, B>>
-
cross
default <B> FiniteIterable<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(java.util.Collection<B> other)
Returns the lazily computed cartesian product of thisFiniteIterable
with aCollection
.- Type Parameters:
B
- the type of the otherCollection
- Parameters:
other
- aCollection
of any type- Returns:
- a
FiniteIterable<Tuple2<A, B>>
-
cycle
default EnhancedIterable<A> cycle()
Returns an infiniteEnhancedIterable
that repeatedly cycles thisFiniteIterable
's elements, in order.- Returns:
- an
EnhancedIterable<A>
-
distinct
default FiniteIterable<A> distinct()
Returns aFiniteIterable
of the distinct values from thisFiniteIterable
.- Returns:
- a
FiniteIterable<A>
-
drop
default FiniteIterable<A> drop(int count)
Returns a newFiniteIterable
that drops the firstcount
elements of thisFiniteIterable
.- Specified by:
drop
in interfaceEnhancedIterable<A>
- Parameters:
count
- the number of elements to drop from thisFiniteIterable
. Must be >= 0. May exceed size of thisFiniteIterable
, in which case, the result will be an emptyFiniteIterable
.- Returns:
- a
FiniteIterable<A>
-
dropWhile
default FiniteIterable<A> dropWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
that skips the first contiguous group of elements of thisFiniteIterable
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
FiniteIterable<A>
-
filter
default FiniteIterable<A> filter(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
that contains all elements of thisFiniteIterable
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
FiniteIterable<A>
-
fmap
default <B> FiniteIterable<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Returns a newFiniteIterable
by applying a function to all elements of thisFiniteIterable
.- 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
FiniteIterableonEmptyFiniteIterable<B>
-
foldLeft
default <B> B foldLeft(com.jnape.palatable.lambda.functions.Fn2<? super B,? super A,? extends B> op, B z)
Applies a binary operator to a start value and all elements of thisFiniteIterable
, going left to right.- Type Parameters:
B
- the result type of the binary operator- Parameters:
z
- the start valueop
- the binary operator- Returns:
- the result of inserting
op
between consecutive elements of thisFiniteIterable
, going left to right with the start valuez
on the left:op(...op(z, x_1), x_2, ..., x_n)
wherex,,1,,, ..., x,,n,,
are the elements of thisFiniteIterable
Returnsz
if thisFiniteIterable
is empty.
-
foldRight
default <B> com.jnape.palatable.lambda.functor.builtin.Lazy<B> foldRight(com.jnape.palatable.lambda.functions.Fn2<? super A,? super com.jnape.palatable.lambda.functor.builtin.Lazy<B>,? extends com.jnape.palatable.lambda.functor.builtin.Lazy<B>> op, com.jnape.palatable.lambda.functor.builtin.Lazy<B> z)
Applies a binary operator to a start value and all elements of thisFiniteIterable
, going right to left.This method is computationally the iterative inverse of
foldLeft(com.jnape.palatable.lambda.functions.Fn2<? super B, ? super A, ? extends B>, B)
, but usesLazy
to support stack-safe execution.- Type Parameters:
B
- the result type of the binary operator- Parameters:
z
- the start valueop
- the binary operator- Returns:
- a
Lazy<B>
that evaluates to the result of insertingop
between consecutive elements of thisFiniteIterable
, going right to left with the start valuez
on the right:op(x_1, op(x_2, ... op(x_n, z)...))
wherex,,1,,, ..., x,,n,,
are the elements of thisFiniteIterable
Returnsz
if thisFiniteIterable
is empty.
-
inits
default ImmutableNonEmptyFiniteIterable<? extends FiniteIterable<A>> inits()
Returns aImmutableNonEmptyIterable
containing all of the subsequences of initial elements of thisFiniteIterable
, ordered by size, starting with the empty list. Example:FiniteIterable.of(1, 2, 3).inits(); // [[], [1], [1, 2], [1, 2, 3]]
- Returns:
- a
ImmutableNonEmptyFiniteIterable<FiniteIterable<A>>
-
intersperse
default FiniteIterable<A> intersperse(A separator)
Returns a newFiniteIterable
with the provided separator value injected between each value of thisFiniteIterable
.If this
FiniteIterable
contains fewer than two elements, it is left untouched.- Specified by:
intersperse
in interfaceEnhancedIterable<A>
- Parameters:
separator
- the separator value- Returns:
- a
FiniteIterable<A>
-
magnetizeBy
default FiniteIterable<? extends NonEmptyFiniteIterable<A>> magnetizeBy(com.jnape.palatable.lambda.functions.Fn2<A,A,java.lang.Boolean> predicate)
Returns anIterable
of contiguous groups of elements in thisFiniteIterable
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:
- a
FiniteIterable<NonEmptyFiniteIterable<A>>
containing the contiguous groups
-
partition
default <B,C> com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends FiniteIterable<B>,? extends FiniteIterable<C>> partition(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.adt.coproduct.CoProduct2<B,C,?>> function)
Partitions thisFiniteIterable
given a disjoint mapping function.- 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<FiniteIterable<B>, FiniteIterable<C>>
-
prepend
default NonEmptyFiniteIterable<A> prepend(A element)
Lazily prepends an element to the front of thisFiniteIterable
, yielding a newNonEmptyFiniteIterable
.- Specified by:
prepend
in interfaceEnhancedIterable<A>
- Parameters:
element
- the element to prepend- Returns:
- a
NonEmptyFiniteIterable<A>
-
prependAll
default FiniteIterable<A> prependAll(A separator)
Returns a newFiniteIterable
with the provided separator value injected before each value of thisFiniteIterable
.If this
FiniteIterable
is empty, it is left untouched.- Specified by:
prependAll
in interfaceEnhancedIterable<A>
- Parameters:
separator
- the separator value- Returns:
- a
FiniteIterable<A>
-
reverse
default FiniteIterable<A> reverse()
Returns a reversed representation of thisFiniteIterable
.Note that reversing is deferred until the returned
Iterable
is iterated.- Returns:
- a
FiniteIterable<A>
-
size
default int size()
Returns the number of elements in thisFiniteIterable
.If this
FiniteIterable
contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Returns:
- the number of elements in this
FiniteIterable
-
slide
default FiniteIterable<? extends NonEmptyFiniteIterable<A>> slide(int k)
"Slides" a window ofk
elements across theFiniteIterable
by one element at a time.Example:
FiniteIterable.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:
- a
FiniteIterable<NonEmptyFiniteIterable<A>>
-
span
default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends FiniteIterable<A>,? extends FiniteIterable<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 thisFiniteIterable
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<FiniteIterable<B>, FiniteIterable<C>>
-
tails
default ImmutableNonEmptyIterable<? extends FiniteIterable<A>> tails()
Returns anImmutableNonEmptyIterable
containing all of the subsequences of tail elements of thisFiniteIterable
, ordered by size, starting with the full list. Example:FiniteIterable.of(1, 2, 3).tails(); // [[1, 2, 3], [2, 3], [3], []]
- Specified by:
tails
in interfaceEnhancedIterable<A>
- Returns:
- an
ImmutableNonEmptyIterable<FiniteIterable<A>>
-
takeWhile
default FiniteIterable<A> takeWhile(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Returns a newFiniteIterable
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
FiniteIterable<A>
-
toFinite
default com.jnape.palatable.lambda.adt.Maybe<? extends FiniteIterable<A>> toFinite()
Always succeeds becauseFiniteIterable
s are always finite.- Specified by:
toFinite
in interfaceEnhancedIterable<A>
- Returns:
- this
FiniteIterable
wrapped in a `just`
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyFiniteIterable<A>> toNonEmpty()
Converts thisFiniteIterable
to aNonEmptyFiniteIterable
if it contains one or more elements.- Specified by:
toNonEmpty
in interfaceEnhancedIterable<A>
- Returns:
- a
Maybe<NonEmptyFiniteIterable<A>
-
zipWith
default <B,C> FiniteIterable<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, java.lang.Iterable<B> other)
Zips together thisFiniteIterable
with anotherIterable
by applying a zipping function.Applies the function to the successive elements of each
Iterable
until one of them runs out of elements.- Specified by:
zipWith
in interfaceEnhancedIterable<A>
- 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
FiniteIterable<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>
-
emptyFiniteIterable
static <A> ImmutableFiniteIterable<A> emptyFiniteIterable()
Creates an emptyFiniteIterable
.- Type Parameters:
A
- the element type- Returns:
- an
ImmutableFiniteIterable<A>
-
finiteIterable
static <A> FiniteIterable<A> finiteIterable(java.util.Collection<A> collection)
Creates aFiniteIterable
by wrapping aCollection
.Does not make a copy of the
Collection
.- Type Parameters:
A
- the element type- Parameters:
collection
- the sourceCollection
- Returns:
- a
FiniteIterable<A>
-
finiteIterable
static <A> FiniteIterable<A> finiteIterable(int maxCount, java.lang.Iterable<A> iterable)
Creates aFiniteIterable
by wrapping anIterable
.- 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.iterable
- the sourceIterable
- Returns:
- a
FiniteIterable<A>
-
of
@SafeVarargs static <A> ImmutableNonEmptyFiniteIterable<A> of(A first, A... more)
Creates aFiniteIterable
containing the given elements.Note that this method actually returns an
ImmutableNonEmptyFiniteIterable
, which is also aFiniteIterable
.- Type Parameters:
A
- the element type- Parameters:
first
- the first elementmore
- the remaining elements- Returns:
- an
ImmutableNonEmptyFiniteIterable<A>
-
-