mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
finish either
This commit is contained in:
@@ -22,4 +22,14 @@ public final class FunctionUtils {
|
||||
return (Either<L, T>) o.map(Either::right).orElseGet(() -> Either.left(de));
|
||||
}
|
||||
|
||||
public static <T, U> Function<T, Either<Exception, U>> liftTry(Function<T, U> f) {
|
||||
return s -> {
|
||||
try {
|
||||
return Either.right(f.apply(s));
|
||||
} catch(Exception e) {
|
||||
return Either.left(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,12 +3,7 @@ package com.dfsek.terra.api.util.generic.data;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface BiFunctor<T, U, B extends BiFunctor<?, ?, B>> extends Functor<T, B> {
|
||||
@Override
|
||||
default <V> BiFunctor<V, U, B> map(Function<T, V> map) {
|
||||
return mapLeft(map);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ package com.dfsek.terra.api.util.generic.data.types;
|
||||
|
||||
import com.dfsek.terra.api.util.function.FunctionUtils;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.control.Monad;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.BiFunctor;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -18,26 +22,87 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface Either<L, R> {
|
||||
public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Either<?, ?>> {
|
||||
default Either<L, R> ifLeft(Consumer<L> action) {
|
||||
return mapLeft(FunctionUtils.lift(action));
|
||||
}
|
||||
|
||||
default Either<L, R> ifRight(Consumer<R> action) {
|
||||
return mapRight(FunctionUtils.lift(action));
|
||||
}
|
||||
|
||||
// Either is a functor in its right parameter.
|
||||
@Override
|
||||
default <U> Either<L, U> map(Function<R, U> map) {
|
||||
return mapRight(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
default <T1> Either<?, T1> pure(T1 t) {
|
||||
return right(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
<T2> Either<L, T2> bind(Function<R, Monad<T2, Either<?, ?>>> map);
|
||||
|
||||
@Override
|
||||
<L1> Either<L1, R> mapLeft(Function<L, L1> f);
|
||||
|
||||
@Override
|
||||
<R1> Either<L, R1> mapRight(Function<R, R1> f);
|
||||
|
||||
Optional<L> getLeft();
|
||||
|
||||
Optional<R> getRight();
|
||||
|
||||
boolean isLeft();
|
||||
|
||||
boolean isRight();
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
static <L1, R1> Either<L1, R1> left(L1 left) {
|
||||
record Left<T>(T value) implements Either<T, Void> {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T2> Either<T, T2> bind(Function<Void, Monad<T2, Either<?, ?>>> map) {
|
||||
return (Either<T, T2>) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <L1> Either<L1, Void> mapLeft(Function<T, 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> getLeft() {
|
||||
return Optional.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Void> getRight() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return new Left(Objects.requireNonNull(left));
|
||||
}
|
||||
|
||||
@@ -45,71 +110,44 @@ public interface Either<L, R> {
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
static <L1, R1> Either<L1, R1> right(R1 right) {
|
||||
record Right<T>(T value) implements Either<Void, T> {
|
||||
@Override
|
||||
public <T2> Either<Void, T2> bind(Function<T, Monad<T2, Either<?, ?>>> map) {
|
||||
return (Either<Void, T2>) map.apply(value);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@Override
|
||||
public <L1> Either<L1, T> mapLeft(Function<Void, L1> f) {
|
||||
return (Either<L1, T>) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R1> Either<Void, R1> mapRight(Function<T, R1> f) {
|
||||
return new Right<>(f.apply(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Void> getLeft() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> getRight() {
|
||||
return Optional.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return new Right(Objects.requireNonNull(right));
|
||||
}
|
||||
record Left<T>(T value) implements Either<T, Void> {
|
||||
|
||||
@Override
|
||||
public <L1> Either<L1, Void> mapLeft(Function<T, 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> getLeft() {
|
||||
return Optional.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Void> getRight() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
record Right<T>(T value) implements Either<Void, T> {
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@Override
|
||||
public <L1> Either<L1, T> mapLeft(Function<Void, L1> f) {
|
||||
return (Either<L1, T>) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R1> Either<Void, R1> mapRight(Function<T, R1> f) {
|
||||
return new Right<>(f.apply(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Void> getLeft() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> getRight() {
|
||||
return Optional.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user