Skip to content

Commit b2d598e

Browse files
committed
Either#trying that supports CheckedRunnable
1 parent 11d0b6a commit b2d598e

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
4343
- `Under`, the inverse of `Over` for `Iso`
4444
- `TypeSafeKey` is an `Iso` and supports mapping
4545
- `TypeSafeKey.Simple`, the single parameter version of `TypeSafeKey`
46+
- `Either#trying` overloads that accept `CheckedRunnable`
4647

4748
### Deprecated
4849
- `MapLens#mappingValues(Function)` is now deprecated in favor of the overload that takes an <code>Iso</code>

src/main/java/com/jnape/palatable/lambda/adt/Either.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
55
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2;
66
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
7+
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedRunnable;
78
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
89
import com.jnape.palatable.lambda.functor.Applicative;
910
import com.jnape.palatable.lambda.functor.Bifunctor;
@@ -21,9 +22,9 @@
2122
import static java.util.Arrays.asList;
2223

2324
/**
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.
2728
*
2829
* @param <L> The left parameter type
2930
* @param <R> The right parameter type
@@ -305,6 +306,33 @@ public static <E extends Exception, R> Either<E, R> trying(CheckedSupplier<E, R>
305306
return trying(supplier, id());
306307
}
307308

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+
308336
/**
309337
* Static factory method for creating a left value.
310338
*

src/test/java/com/jnape/palatable/lambda/adt/EitherTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.jnape.palatable.lambda.adt.Either.right;
2323
import static com.jnape.palatable.lambda.adt.Maybe.just;
2424
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
25+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
2526
import static com.jnape.palatable.traitor.framework.Subjects.subjects;
2627
import static org.hamcrest.core.Is.is;
2728
import static org.junit.Assert.assertEquals;
@@ -186,6 +187,21 @@ public void dyadicTryingLiftsCheckedSupplierMappingAnyThrownExceptions() {
186187
}, Throwable::getMessage));
187188
}
188189

190+
@Test
191+
public void dyadicTryingWithRunnable() {
192+
assertEquals(right(UNIT), Either.trying(() -> {}, Throwable::getMessage));
193+
assertEquals(left("expected"), Either.trying(() -> {
194+
throw new IllegalStateException("expected");
195+
}, Throwable::getMessage));
196+
}
197+
198+
@Test
199+
public void monadTryingWithRunnable() {
200+
assertEquals(right(UNIT), Either.trying(() -> {}));
201+
IllegalStateException expected = new IllegalStateException("expected");
202+
assertEquals(left(expected), Either.trying(() -> {throw expected;}));
203+
}
204+
189205
@Test
190206
public void monadicPeekLiftsIOToTheRight() {
191207
Either<String, Integer> left = left("foo");

0 commit comments

Comments
 (0)