dev.marksman.collectionviews.Vectors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collection-views Show documentation
Show all versions of collection-views Show documentation
Low overhead, protected views over Java collections
package dev.marksman.collectionviews;
import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
import com.jnape.palatable.lambda.functions.Fn0;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.functions.builtin.fn2.Drop;
import com.jnape.palatable.lambda.functions.builtin.fn2.Take;
import dev.marksman.enhancediterables.ImmutableNonEmptyFiniteIterable;
import dev.marksman.enhancediterables.NonEmptyIterable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into;
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToCollection.toCollection;
import static com.jnape.palatable.lambda.functions.builtin.fn2.Tupler2.tupler;
import static dev.marksman.collectionviews.CrossJoinVector.crossJoinVector;
import static dev.marksman.collectionviews.ImmutableVectors.nonEmptyRange;
import static dev.marksman.collectionviews.MapperChain.mapperChain;
import static dev.marksman.collectionviews.Validation.*;
import static dev.marksman.collectionviews.VectorZip.vectorZip;
final class Vectors {
private Vectors() {
}
static Vector> cross(Vector first, Vector second) {
return second.toNonEmpty()., NonEmptyVector>>zip(first.toNonEmpty()
.fmap(tupler()))
.match(__ -> empty(),
into(Vectors::nonEmptyCross));
}
static Vector drop(int count, Vector source) {
return VectorSlicing.dropImpl(VectorSlice::vectorSlice, count, source);
}
static Vector dropRight(int count, Vector source) {
validateDrop(count, source);
int size = source.size();
if (count >= size) {
return empty();
} else {
return take(size - count, source);
}
}
static ImmutableVector empty() {
return EmptyVector.emptyVector();
}
static Maybe findIndex(Fn1 super A, ? extends Boolean> predicate, Vector vec) {
int size = vec.size();
for (int i = 0; i < size; i++) {
if (predicate.apply(vec.unsafeGet(i))) {
return just(i);
}
}
return nothing();
}
static int findPrefixLength(Fn1 super A, ? extends Boolean> predicate, Vector vec) {
int result = 0;
for (A current : vec) {
if (predicate.apply(current)) {
result += 1;
} else {
break;
}
}
return result;
}
static ImmutableNonEmptyFiniteIterable> inits(Vector source) {
return nonEmptyRange(source.size() + 1).fmap(source::dropRight);
}
static Vector map(Fn1 super A, ? extends B> f, Vector source) {
return maybeNonEmptyWrap(source)
.match(__ -> empty(),
nonEmpty -> nonEmptyMap(f, nonEmpty));
}
static Maybe> maybeNonEmptyWrap(A[] arr) {
Objects.requireNonNull(arr);
if (arr.length == 0) {
return nothing();
} else {
return just(new WrappedArrayVector<>(arr));
}
}
static Maybe> maybeNonEmptyWrap(List list) {
Objects.requireNonNull(list);
if (list.isEmpty()) {
return nothing();
} else {
return just(new WrappedListVector<>(list));
}
}
static Maybe> maybeNonEmptyWrap(Vector vec) {
Objects.requireNonNull(vec);
if (vec instanceof NonEmptyVector>) {
return just((NonEmptyVector) vec);
} else if (!vec.isEmpty()) {
return just(new VectorCons<>(vec.unsafeGet(0), vec.drop(1)));
} else {
return nothing();
}
}
static NonEmptyVector> nonEmptyCross(NonEmptyVector first, NonEmptyVector second) {
return crossJoinVector(first, second);
}
static Fn0 nonEmptyError() {
return () -> new IllegalArgumentException("Cannot construct NonEmptyVector from empty input");
}
static NonEmptyIterable> nonEmptyInits(NonEmptyVector source) {
return nonEmptyRange(source.size() + 1).fmap(source::dropRight);
}
@SuppressWarnings("unchecked")
static NonEmptyVector nonEmptyMap(Fn1 super A, ? extends B> f, NonEmptyVector source) {
return new MappedVector<>(mapperChain((Fn1