33import com .jnape .palatable .lambda .functor .Applicative ;
44import com .jnape .palatable .lambda .functor .Profunctor ;
55
6+ import java .util .function .BiFunction ;
67import java .util .function .Function ;
78
89/**
@@ -30,7 +31,9 @@ public interface Fn1<A, B> extends Applicative<B, Fn1<A, ?>>, Profunctor<A, B, F
3031 * @param f the function to invoke with this function's return value
3132 * @param <C> the return type of the next function to invoke
3233 * @return a function representing the composition of this function and f
34+ * @deprecated in favor of {@link Fn1#andThen(Function)}
3335 */
36+ @ Deprecated
3437 default <C > Fn1 <A , C > then (Function <? super B , ? extends C > f ) {
3538 return fmap (f );
3639 }
@@ -41,34 +44,47 @@ default <C> Fn1<A, C> then(Function<? super B, ? extends C> f) {
4144 * @param <C> the return type of the next function to invoke
4245 * @param f the function to invoke with this function's return value
4346 * @return a function representing the composition of this function and f
44- * @see Fn1#then(Function)
4547 */
4648 @ Override
47- @ SuppressWarnings ("unchecked" )
4849 default <C > Fn1 <A , C > fmap (Function <? super B , ? extends C > f ) {
49- return ( Fn1 < A , C >) Applicative .super .fmap (f );
50+ return Applicative .super .< C > fmap (f ). coerce ( );
5051 }
5152
53+ /**
54+ * {@inheritDoc}
55+ */
5256 @ Override
5357 default <C > Fn1 <A , C > pure (C c ) {
5458 return __ -> c ;
5559 }
5660
61+ /**
62+ * {@inheritDoc}
63+ */
5764 @ Override
5865 default <C > Fn1 <A , C > zip (Applicative <Function <? super B , ? extends C >, Fn1 <A , ?>> appFn ) {
5966 return a -> appFn .<Fn1 <A , Function <? super B , ? extends C >>>coerce ().apply (a ).apply (apply (a ));
6067 }
6168
69+ /**
70+ * {@inheritDoc}
71+ */
6272 @ SuppressWarnings ("unchecked" )
6373 default <C > Fn1 <A , C > zip (Fn2 <A , B , C > appFn ) {
6474 return zip ((Fn1 <A , Function <? super B , ? extends C >>) (Object ) appFn );
6575 }
6676
77+ /**
78+ * {@inheritDoc}
79+ */
6780 @ Override
6881 default <C > Fn1 <A , C > discardL (Applicative <C , Fn1 <A , ?>> appB ) {
6982 return Applicative .super .discardL (appB ).coerce ();
7083 }
7184
85+ /**
86+ * {@inheritDoc}
87+ */
7288 @ Override
7389 default <C > Fn1 <A , B > discardR (Applicative <C , Fn1 <A , ?>> appB ) {
7490 return Applicative .super .discardR (appB ).coerce ();
@@ -127,6 +143,32 @@ default <Z> Fn1<Z, B> compose(Function<? super Z, ? extends A> before) {
127143 return z -> apply (before .apply (z ));
128144 }
129145
146+ /**
147+ * Right-to-left composition between different arity functions. Preserves highest arity in the return type,
148+ * specialized to lambda types (in this case, {@link BiFunction} -> {@link Fn2}).
149+ *
150+ * @param before the function to pass its return value to this function's input
151+ * @param <Y> the resulting function's first argument type
152+ * @param <Z> the resulting function's second argument type
153+ * @return a new function from Y x Z to B
154+ */
155+ default <Y , Z > Fn2 <Y , Z , B > compose (BiFunction <? super Y , ? super Z , ? extends A > before ) {
156+ return (y , z ) -> apply (before .apply (y , z ));
157+ }
158+
159+ /**
160+ * Left-to-right composition between different arity functions. Preserves highest arity in the return type,
161+ * specialized to lambda types (in this case, {@link BiFunction} -> {@link Fn2}).
162+ *
163+ * @param after the function to invoke on this function's return value
164+ * @param <C> the resulting function's second argument type
165+ * @param <D> the resulting function's return type
166+ * @return a new function from A x C to D
167+ */
168+ default <C , D > Fn2 <A , C , D > andThen (BiFunction <? super B , ? super C , ? extends D > after ) {
169+ return (a , c ) -> after .apply (apply (a ), c );
170+ }
171+
130172 /**
131173 * Override of {@link Function#andThen(Function)}, returning an instance of <code>Fn1</code> for compatibility.
132174 * Left-to-right composition.
@@ -148,8 +190,23 @@ default <C> Fn1<A, C> andThen(Function<? super B, ? extends C> after) {
148190 * @param <A> the input argument type
149191 * @param <B> the output type
150192 * @return the Fn1
193+ * @deprecated in favor of {@link Fn1#fn1(Function)}
151194 */
195+ @ Deprecated
152196 static <A , B > Fn1 <A , B > adapt (Function <A , B > function ) {
153197 return function ::apply ;
154198 }
199+
200+ /**
201+ * Static factory method for wrapping a {@link Function} in an {@link Fn1}. Useful for avoid explicit casting when
202+ * using method references as {@link Fn1}s.
203+ *
204+ * @param function the function to adapt
205+ * @param <A> the input argument type
206+ * @param <B> the output type
207+ * @return the Fn1
208+ */
209+ static <A , B > Fn1 <A , B > fn1 (Function <A , B > function ) {
210+ return function ::apply ;
211+ }
155212}
0 commit comments