|
4 | 4 | import com.jnape.palatable.lambda.functions.builtin.fn2.Peek; |
5 | 5 | import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2; |
6 | 6 | import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1; |
| 7 | +import com.jnape.palatable.lambda.functions.specialized.checked.CheckedRunnable; |
7 | 8 | import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier; |
8 | 9 | import com.jnape.palatable.lambda.functor.Applicative; |
9 | 10 | import com.jnape.palatable.lambda.functor.Bifunctor; |
|
21 | 22 | import static java.util.Arrays.asList; |
22 | 23 |
|
23 | 24 | /** |
24 | | - * The binary tagged union. General semantics tend to connote "success" values via the right value and "failure" values |
25 | | - * via the left values. <code>Either</code>s are both <code>Functor</code>s over their right value and |
26 | | - * <code>Bifunctor</code>s over both values. |
| 25 | + * The binary tagged union, implemented as a specialized {@link CoProduct2}. General semantics tend to connote "success" |
| 26 | + * values via the right value and "failure" values via the left values. {@link Either}s are both {@link Monad}s and |
| 27 | + * {@link Traversable}s over their right value and are {@link Bifunctor}s over both values. |
27 | 28 | * |
28 | 29 | * @param <L> The left parameter type |
29 | 30 | * @param <R> The right parameter type |
@@ -305,6 +306,33 @@ public static <E extends Exception, R> Either<E, R> trying(CheckedSupplier<E, R> |
305 | 306 | return trying(supplier, id()); |
306 | 307 | } |
307 | 308 |
|
| 309 | + /** |
| 310 | + * Attempt to execute the {@link CheckedRunnable}, returning {@link Unit} in a right value. If the runnable throws |
| 311 | + * an exception, apply <code>leftFn</code> to it, wrap it in a left value, and return it. |
| 312 | + * |
| 313 | + * @param runnable the runnable |
| 314 | + * @param leftFn a function mapping E to L |
| 315 | + * @param <E> the most contravariant exception that the runnable might throw |
| 316 | + * @param <L> the left parameter type |
| 317 | + * @return {@link Unit} as a right value, or leftFn's mapping result as a left value |
| 318 | + */ |
| 319 | + public static <E extends Exception, L> Either<L, Unit> trying(CheckedRunnable<E> runnable, |
| 320 | + Function<? super E, ? extends L> leftFn) { |
| 321 | + return Try.trying(runnable).toEither(leftFn); |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Attempt to execute the {@link CheckedRunnable}, returning {@link Unit} in a right value. If the runnable throws |
| 326 | + * exception, wrap it in a left value and return it. |
| 327 | + * |
| 328 | + * @param runnable the runnable |
| 329 | + * @param <E> the left parameter type (the most contravariant exception that runnable might throw) |
| 330 | + * @return {@link Unit} as a right value, or a left value of the thrown exception |
| 331 | + */ |
| 332 | + public static <E extends Exception> Either<E, Unit> trying(CheckedRunnable<E> runnable) { |
| 333 | + return trying(runnable, id()); |
| 334 | + } |
| 335 | + |
308 | 336 | /** |
309 | 337 | * Static factory method for creating a left value. |
310 | 338 | * |
|
0 commit comments