1+ package com .jnape .palatable .lambda .functions .specialized ;
2+
3+ import com .jnape .palatable .lambda .functions .Fn1 ;
4+ import com .jnape .palatable .lambda .functor .Applicative ;
5+ import com .jnape .palatable .lambda .monad .Monad ;
6+
7+ import java .util .function .Function ;
8+
9+ /**
10+ * The Kleisli arrow of a {@link Monad}, manifest as simply an <code>{@link Fn1}<A, MB></code>. This can be
11+ * thought of as a fixed, portable {@link Monad#flatMap(Function)}.
12+ *
13+ * @param <A> the input argument type
14+ * @param <M> the {@link Monad} unification parameter
15+ * @param <MB> the output {@link Monad} type
16+ */
17+ @ FunctionalInterface
18+ public interface Kleisli <A , B , M extends Monad , MB extends Monad <B , M >> extends Fn1 <A , MB > {
19+
20+ /**
21+ * Left-to-right composition of two compatible {@link Kleisli} arrows, yielding a new {@link Kleisli} arrow.
22+ *
23+ * @param after the arrow to execute after this one
24+ * @param <C> the new return parameter type
25+ * @param <MC> the {@link Monad} instance to return
26+ * @return the composition of the two arrows as a new {@link Kleisli} arrow
27+ */
28+ default <C , MC extends Monad <C , M >> Kleisli <A , C , M , MC > andThen (Kleisli <B , C , M , MC > after ) {
29+ return a -> apply (a ).flatMap (after ).coerce ();
30+ }
31+
32+ /**
33+ * Right-to-left composition of two compatible {@link Kleisli} arrows, yielding a new {@link Kleisli} arrow.
34+ *
35+ * @param before the arrow to execute before this one
36+ * @param <Z> the new input argument type
37+ * @param <MA> the {@link Monad} instance to flatMap with this arrow
38+ * @return the composition of the two arrows as a new {@link Kleisli} arrow
39+ */
40+ default <Z , MA extends Monad <A , M >> Kleisli <Z , B , M , MB > compose (Kleisli <Z , A , M , MA > before ) {
41+ return z -> before .apply (z ).flatMap (this ).coerce ();
42+ }
43+
44+ /**
45+ * {@inheritDoc}
46+ */
47+ @ Override
48+ default <Z > Kleisli <Z , B , M , MB > compose (Function <? super Z , ? extends A > before ) {
49+ return Fn1 .super .compose (before )::apply ;
50+ }
51+
52+ /**
53+ * {@inheritDoc}
54+ */
55+ @ Override
56+ default <C > Kleisli <A , B , M , MB > discardR (Applicative <C , Fn1 <A , ?>> appB ) {
57+ return Fn1 .super .discardR (appB )::apply ;
58+ }
59+
60+ /**
61+ * {@inheritDoc}
62+ */
63+ @ Override
64+ default <Z > Kleisli <Z , B , M , MB > contraMap (Function <? super Z , ? extends A > fn ) {
65+ return Fn1 .super .contraMap (fn )::apply ;
66+ }
67+
68+ /**
69+ * {@inheritDoc}
70+ */
71+ @ Override
72+ default <Z > Kleisli <Z , B , M , MB > diMapL (Function <? super Z , ? extends A > fn ) {
73+ return Fn1 .super .diMapL (fn )::apply ;
74+ }
75+
76+ /**
77+ * Adapt a compatible function into a {@link Kleisli} arrow.
78+ *
79+ * @param fn the function
80+ * @param <A> the input argument type
81+ * @param <B> the output parameter type
82+ * @param <M> the {@link Monad} unification parameter
83+ * @param <MB> the returned {@link Monad} instance
84+ * @return the function adapted as a {@link Kleisli} arrow
85+ */
86+ static <A , B , M extends Monad , MB extends Monad <B , M >> Kleisli <A , B , M , MB > kleisli (
87+ Function <? super A , ? extends MB > fn ) {
88+ return fn ::apply ;
89+ }
90+ }
0 commit comments