more either stuff

This commit is contained in:
dfsek
2025-12-29 18:18:14 -07:00
parent 87e437f889
commit 7d3382e623
2 changed files with 47 additions and 19 deletions

View File

@@ -6,4 +6,8 @@ import java.util.function.Function;
public interface BiFunctor<T, U, B extends BiFunctor<?, ?, B>> {
<V> BiFunctor<V, U, B> mapLeft(Function<T, V> map);
<V> BiFunctor<T, V, B> mapRight(Function<U, V> map);
default <V, W> BiFunctor<V, W, B> bimap(Function<T, V> left, Function<U, W> right) {
return mapLeft(left).mapRight(right);
}
}

View File

@@ -59,36 +59,40 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
boolean isRight();
Either<R, L> flip();
<U> U collect(Function<L, U> left, Function<R, U> right);
@SuppressWarnings({ "unchecked" })
@NotNull
@Contract("_ -> new")
static <L1, R1> Either<L1, R1> left(L1 left) {
record Left<T>(T value) implements Either<T, Void> {
record Left<L, R>(L value) implements Either<L, R> {
@Override
@SuppressWarnings("unchecked")
public <T2> Either<T, T2> bind(Function<Void, Monad<T2, Either<?, ?>>> map) {
return (Either<T, T2>) this;
public <T2> Either<L, T2> bind(Function<R, Monad<T2, Either<?, ?>>> map) {
return (Either<L, T2>) this;
}
@Override
public <L1> Either<L1, Void> mapLeft(Function<T, L1> f) {
public <L1> Either<L1, R> mapLeft(Function<L, L1> f) {
return new Left<>(f.apply(value));
}
@SuppressWarnings({ "unchecked" })
@Override
public <R1> Either<T, R1> mapRight(Function<Void, R1> f) {
return (Either<T, R1>) this;
public <R1> Either<L, R1> mapRight(Function<R, R1> f) {
return (Either<L, R1>) this;
}
@Override
public Optional<T> getLeft() {
public Optional<L> getLeft() {
return Optional.of(value);
}
@Override
public Optional<Void> getRight() {
public Optional<R> getRight() {
return Optional.empty();
}
@@ -101,39 +105,49 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
public boolean isRight() {
return false;
}
@Override
public Either<R, L> flip() {
return right(value);
}
@Override
public <U> U collect(Function<L, U> left, Function<R, U> right) {
return left.apply(value);
}
}
return new Left(Objects.requireNonNull(left));
return new Left<>(Objects.requireNonNull(left));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({"unchecked" })
@NotNull
@Contract("_ -> new")
static <L1, R1> Either<L1, R1> right(R1 right) {
record Right<T>(T value) implements Either<Void, T> {
record Right<L, R>(R value) implements Either<L, R> {
@Override
public <T2> Either<Void, T2> bind(Function<T, Monad<T2, Either<?, ?>>> map) {
return (Either<Void, T2>) map.apply(value);
public <T2> Either<L, T2> bind(Function<R, Monad<T2, Either<?, ?>>> map) {
return (Either<L, T2>) map.apply(value);
}
@SuppressWarnings({ "unchecked" })
@Override
public <L1> Either<L1, T> mapLeft(Function<Void, L1> f) {
return (Either<L1, T>) this;
public <L1> Either<L1, R> mapLeft(Function<L, L1> f) {
return (Either<L1, R>) this;
}
@Override
public <R1> Either<Void, R1> mapRight(Function<T, R1> f) {
public <R1> Either<L, R1> mapRight(Function<R, R1> f) {
return new Right<>(f.apply(value));
}
@Override
public Optional<Void> getLeft() {
public Optional<L> getLeft() {
return Optional.empty();
}
@Override
public Optional<T> getRight() {
public Optional<R> getRight() {
return Optional.of(value);
}
@@ -146,8 +160,18 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
public boolean isRight() {
return true;
}
@Override
public Either<R, L> flip() {
return left(value);
}
@Override
public <U> U collect(Function<L, U> left, Function<R, U> right) {
return right.apply(value);
}
}
return new Right(Objects.requireNonNull(right));
return new Right<>(Objects.requireNonNull(right));
}
}