dev.marksman.enhancediterables.ImmutableIterable Maven / Gradle / Ivy
Show all versions of enhanced-iterables Show documentation
package dev.marksman.enhancediterables;
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.functions.builtin.fn1.Tails;
import com.jnape.palatable.lambda.functions.builtin.fn2.*;
import com.jnape.palatable.lambda.functions.builtin.fn3.ZipWith;
import com.jnape.palatable.lambda.monoid.builtin.Concat;
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
import static dev.marksman.enhancediterables.EnhancedIterables.*;
import static dev.marksman.enhancediterables.Validation.*;
import static java.util.Objects.requireNonNull;
/**
* An {@code EnhancedIterable} that is safe from mutation.
*
* May be infinite, finite, or empty.
*
* @param the element type
*/
public interface ImmutableIterable extends EnhancedIterable {
/**
* Lazily appends an element to the end of this {@code ImmutableIterable}, yielding a new {@code ImmutableNonEmptyIterable}.
*
* @param element the element to append
* @return a ImmutableNonEmptyIterable<A>
*/
@Override
default ImmutableNonEmptyIterable append(A element) {
return immutableNonEmptyIterableOrThrow(Snoc.snoc(element, this));
}
/**
* Lazily concatenates another {@code ImmutableIterable} to the end of this {@code ImmutableIterable},
* yielding a new {@code ImmutableIterable}.
*
* @param other the other {@link ImmutableIterable}
* @return an {@code ImmutableIterable}
*/
default ImmutableIterable concat(ImmutableIterable other) {
requireNonNull(other);
return EnhancedIterables.immutableIterable(Concat.concat(this, other));
}
/**
* Lazily concatenates an {@code ImmutableNonEmptyIterable} to the end of this {@code ImmutableIterable},
* yielding a new {@code ImmutableNonEmptyIterable}.
*
* @param other an {@link ImmutableNonEmptyIterable}
* @return an {@code ImmutableNonEmptyIterable}
*/
default ImmutableNonEmptyIterable concat(ImmutableNonEmptyIterable other) {
requireNonNull(other);
return EnhancedIterables.immutableNonEmptyIterableOrThrow(Concat.concat(this, other));
}
/**
* Returns a new {@code ImmutableIterable} that drops the first {@code count} elements of this {@code ImmutableIterable}.
*
* @param count the number of elements to drop from this {@code ImmutableIterable}.
* Must be >= 0.
* May exceed size of this {@code ImmutableIterable}, in which case, the result will be an
* empty {@code ImmutableIterable}.
* @return an {@code ImmutableIterable}
*/
@Override
default ImmutableIterable drop(int count) {
validateDrop(count);
return immutableIterable(Drop.drop(count, this));
}
/**
* Returns a new {@code ImmutableIterable} that skips the first contiguous group of elements of this
* {@code ImmutableIterable} that satisfy a predicate.
*
* Iteration begins at the first element for which the predicate evaluates to false.
*
* @param predicate a predicate; should be referentially transparent and not have side-effects
* @return a ImmutableIterable<A>
*/
@Override
default ImmutableIterable dropWhile(Fn1 super A, ? extends Boolean> predicate) {
requireNonNull(predicate);
return immutableIterable(DropWhile.dropWhile(predicate, this));
}
/**
* Returns a new {@code ImmutableIterable} that contains all elements of this {@code ImmutableIterable}
* that satisfy a predicate.
*
* @param predicate a predicate; should be referentially transparent and not have side-effects
* @return a ImmutableIterable<A>
*/
@Override
default ImmutableIterable filter(Fn1 super A, ? extends Boolean> predicate) {
requireNonNull(predicate);
return immutableIterable(Filter.filter(predicate).apply(this));
}
/**
* Returns a new {@code ImmutableIterable} by applying a function to all elements of this {@code ImmutableIterable}.
*
* @param f a function from {@code A} to {@code B}.
* This function should be referentially transparent and not perform side-effects.
* It may be called zero or more times for each element.
* @param the type returned by {@code f}
* @return a ImmutableIterable<B>
*/
@Override
default ImmutableIterable fmap(Fn1 super A, ? extends B> f) {
requireNonNull(f);
return immutableIterable(Map.map(f, this));
}
/**
* Returns a new {@code ImmutableIterable} with the provided separator value injected between each value of this
* {@code ImmutableIterable}.
*
* If this {@code ImmutableIterable} contains fewer than two elements, it is left untouched.
*
* @param separator the separator value
* @return a ImmutableIterable<A>
*/
@Override
default ImmutableIterable intersperse(A separator) {
return immutableIterable(Intersperse.intersperse(separator, this));
}
/**
* Partitions this {@code ImmutableIterable} 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.
*
* @param function the mapping function
* @param The output left Iterable element type, as well as the CoProduct2 A type
* @param The output right Iterable element type, as well as the CoProduct2 B type
* @return a Tuple2<ImmutableIterable<B>, ImmutableIterable<C>>
*/
@Override
default Tuple2 extends ImmutableIterable, ? extends ImmutableIterable> partition(
Fn1 super A, ? extends CoProduct2> function) {
requireNonNull(function);
Tuple2, Iterable> partitionResult = Partition.partition(function, this);
return tuple(immutableIterable(partitionResult._1()),
immutableIterable(partitionResult._2()));
}
/**
* Lazily prepends an element to the front of this {@code ImmutableIterable}, yielding a new {@code ImmutableNonEmptyIterable}.
*
* @param element the element to prepend
* @return an ImmutableNonEmptyIterable<A>
*/
@Override
default ImmutableNonEmptyIterable prepend(A element) {
return ImmutableNonEmptyIterable.immutableNonEmptyIterable(element, this);
}
/**
* Returns a new {@code ImmutableIterable} with the provided separator value injected before each value of this
* {@code ImmutableIterable}.
*
* If this {@code ImmutableIterable} is empty, it is left untouched.
*
* @param separator the separator value
* @return a ImmutableIterable<A>
*/
@Override
default ImmutableIterable prependAll(A separator) {
return immutableIterable(PrependAll.prependAll(separator, this));
}
/**
* "Slide" a window of {@code k} elements across the {@code ImmutableIterable} by one element at a time.
*
* Example:
*
* ImmutableIterable.of(1, 2, 3, 4, 5).slide(2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
*
* @param k the number of elements in the sliding window. Must be >= 1.
* @return an {@code ImmutableIterable>}
*/
@Override
default ImmutableIterable extends ImmutableNonEmptyFiniteIterable> slide(int k) {
validateSlide(k);
return immutableIterable(Map.map(EnhancedIterables::immutableNonEmptyFiniteIterableOrThrow,
Slide.slide(k, this)));
}
/**
* Returns a {@code Tuple2} where the first slot is the front contiguous elements of this
* {@code ImmutableIterable} matching a predicate and the second slot is all the remaining elements.
*
* @param predicate a predicate; should be referentially transparent and not have side-effects
* @return a Tuple2<ImmutableIterable<B>, ImmutableIterable<C>>
*/
@Override
default Tuple2 extends ImmutableIterable, ? extends ImmutableIterable> span(Fn1 super A, ? extends Boolean> predicate) {
requireNonNull(predicate);
Tuple2, Iterable> spanResult = Span.span(predicate).apply(this);
return tuple(immutableIterable(spanResult._1()),
immutableIterable(spanResult._2()));
}
/**
* Returns a {@code NonEmptyIterable} containing all of the subsequences of tail
* elements of this {@code ImmutableIterable}, ordered by size, starting with the full list.
* Example:
*
* ImmutableIterable.of(1, 2, 3).tails(); // [[1, 2, 3], [2, 3], [3], []]
*
* @return a {@code NonEmptyIterable>}
*/
@Override
default NonEmptyIterable extends ImmutableIterable> tails() {
return nonEmptyIterableOrThrow(Map.map(EnhancedIterables::immutableIterable, Tails.tails(this)));
}
/**
* Returns a new {@code ImmutableFiniteIterable} that takes the first {@code count} elements of this {@code ImmutableIterable}.
*
* @param count the number of elements to take from this {@code EnhancedIterable}.
* Must be >= 0.
* May exceed size of this {@code ImmutableIterable}, in which case, the result will contain
* as many elements available.
* @return an {@code ImmutableFiniteIterable}
*/
@Override
default ImmutableFiniteIterable take(int count) {
validateTake(count);
return immutableFiniteIterable(Take.take(count, this));
}
@Override
default ImmutableIterable takeWhile(Fn1 super A, ? extends Boolean> predicate) {
requireNonNull(predicate);
return immutableIterable(TakeWhile.takeWhile(predicate, this));
}
default ImmutableIterable zipWith(Fn2 fn, ImmutableIterable other) {
requireNonNull(fn);
requireNonNull(other);
return immutableIterable(ZipWith.zipWith(fn, this, other));
}
default ImmutableFiniteIterable zipWith(Fn2 fn, ImmutableFiniteIterable other) {
requireNonNull(fn);
requireNonNull(other);
return immutableFiniteIterable(ZipWith.zipWith(fn, this, other));
}
@SafeVarargs
static ImmutableNonEmptyFiniteIterable of(A first, A... more) {
return EnhancedIterables.of(first, more);
}
}