mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
add basic functional types again
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.dfsek.terra.api.util.function;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.either.Either;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -14,4 +17,9 @@ public final class FunctionUtils {
|
||||
};
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, L> Either<L, T> toEither(Optional<T> o, L de) {
|
||||
return (Either<L, T>) o.map(Either::right).orElseGet(() -> Either.left(de));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface Functor<T, F extends Functor<?, F>> {
|
||||
<U> Functor<U, F> map(Function<T, U> map);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public interface Maybe<T> {
|
||||
default Optional<T> toOptional() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
/**
|
||||
* A monad is a monoid in the category of endofunctors.
|
||||
*/
|
||||
public interface Monad<T, M extends Monad<?, M>> extends Functor<T, M>, Monoid<T, M> {
|
||||
<T2> Monad<T2, M> bind(Function<T, Monad<T2, M>> map);
|
||||
<T2> Monad<T2, M> pure(T2 t);
|
||||
|
||||
@Override
|
||||
default <U> Monad<U, M> map(Function<T, U> map) {
|
||||
return bind(m -> pure(map.apply(m)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
public interface Monoid<T, M extends Monoid<?, M>> extends Semigroup<T, M>{
|
||||
<T1> Monoid<T1, M> pure(T1 t);
|
||||
|
||||
@Override
|
||||
Monoid<T, M> multiply(M t);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
public interface Semigroup<T, S extends Semigroup<?, S>> {
|
||||
Semigroup<T, S> multiply(S t);
|
||||
}
|
||||
Reference in New Issue
Block a user