From 8538ee68045753c7157a7fcddeb508a0779542d4 Mon Sep 17 00:00:00 2001 From: dfsek Date: Mon, 15 Aug 2022 10:33:25 -0700 Subject: [PATCH] add simple do notation for monadic binding --- .../terra/addons/manifest/api/monad/Do.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/api/monad/Do.java diff --git a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/api/monad/Do.java b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/api/monad/Do.java new file mode 100644 index 000000000..3ab297e2d --- /dev/null +++ b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/api/monad/Do.java @@ -0,0 +1,64 @@ +package com.dfsek.terra.addons.manifest.api.monad; + +import io.vavr.Function1; +import io.vavr.Function2; +import io.vavr.Function3; +import io.vavr.Function4; + +import com.dfsek.terra.api.util.function.monad.Monad; + + +public final class Do { + private Do() { + + } + + public static > Monad with(Monad monad) { + return monad; + } + + public static > Monad with(Monad monad, + Function1> bind) { + return monad + .bind(bind); + } + + public static > Monad with(Monad monad, + Function1> bind, + Function2> bind2) { + return monad + .bind(t -> monad + .bind(bind) + .bind(bind2.apply(t))); + } + + public static > Monad with(Monad monad, + Function1> bind, + Function2> bind2, + Function3> bind3) { + return monad + .bind(t -> monad + .bind(bind) + .bind(u -> bind2.apply(t) + .andThen(vmMonad -> vmMonad + .bind(bind3.apply(t, u))) + .apply(u))); + } + + public static > Monad with(Monad monad, + Function1> bind, + Function2> bind2, + Function3> bind3, + Function4> bind4) { + return monad + .bind(t -> monad + .bind(bind) + .bind(u -> bind2.apply(t) + .andThen(vmMonad -> vmMonad + .bind(v -> bind3.apply(t, u) + .andThen(wmMonad -> wmMonad + .bind(bind4.apply(t, u, v))) + .apply(v))) + .apply(u))); + } +}