All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stonehorse.grit.PersistentVector Maven / Gradle / Ivy

Go to download

Persistent Collections for Java. Immutable containers with mutation as expression

The newest version!
package stonehorse.grit;

import stonehorse.candy.Iterables;

import java.io.Serializable;
import java.util.List;
import java.util.RandomAccess;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import static stonehorse.grit.tools.Util.defaultReduce;

/**
 * A random access sequential collection with mutation as expression. Side effecting mutation throws UnsupportedOperationException
 */
public interface PersistentVector extends Indexed, Traversable,
		 List, Serializable, RandomAccess {
	/**
	 * Vector with val at index, where index is at most size
	 * 
{@code
	 * vector(1, 2).withAt(30, 0) => [30, 2]
	 * }
*/ @Override PersistentVector withAt(T val, int i); /** * The value at index or value if this is smaller */ default T getOrDefault(int i, T value){ return getOr(i, ()->value); } /** * Vector with val at the tail */ @Override PersistentVector with(T val); /** * Vector without the tail */ @Override PersistentVector without(); /** * Functional interface of the value at index */ @Override default T apply(int index){ return get(index); } /** * subvector delimited by indexes from and to. * Returns a delimited view on this, that will keep on referring to this * even when this otherwise is forgotten. */ @Override PersistentVector subList(int from, int to); /** * The vector of applying f to each of this */ @Override PersistentVector map(Function f); /** * Vector of the concatenation of the results of applying f to each of this */ @Override PersistentVector flatMap(Function> f); /** * Vector of all elements matching predicate */ @Override PersistentVector filter(Predicate predicate); /** * The vector with all elements added to end */ @Override PersistentVector withAll(Iterable elements); /** * Vector with num elements dropped from the end */ @Override PersistentVector drop(int num); /** * The remaining value of repeatedly combining accumulation with each element in this, starting with an initial accumulator value. */ @Override default V fold(BiFunction fn, V acc) { return Iterables.fold(fn,acc, this); } /** * The remaining value of repeatedly combining accumulation with each element in this except the first, which is used as initial accumulator value */ @Override default T reduce(BiFunction fn) { return defaultReduce(fn, this); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy