Skip to content

Commit d2e0c1d

Browse files
committed
Adding Either#trying back until I decide if I should deprecate it
1 parent 150d9e6 commit d2e0c1d

4 files changed

Lines changed: 20 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2929
- `IfThenElse`, an expression form for `if` statements
3030
- `CheckedRunnable` and `CheckedSupplier` conversion and convenience methods
3131

32-
### Deprecated
33-
- `Either#trying` in favor of `Try#trying`
34-
3532
## [2.1.1] - 2018-01-16
3633
### Changed
3734
- ***Breaking Change***: Moved `Trampoline` and `RecursiveResult` to better package

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,7 @@ public static <L, R> Either<L, R> fromMaybe(Maybe<R> maybe, Supplier<L> leftFn)
286286
* @param <L> the left parameter type
287287
* @param <R> the right parameter type
288288
* @return the supplier result as a right value, or leftFn's mapping result as a left value
289-
* @deprecated in favor of {@link Try#trying(CheckedSupplier)}
290289
*/
291-
@Deprecated
292-
@SuppressWarnings("unchecked")
293290
public static <E extends Exception, L, R> Either<L, R> trying(CheckedSupplier<E, ? extends R> supplier,
294291
Function<? super E, ? extends L> leftFn) {
295292
return Try.<E, R>trying(supplier::get).toEither(leftFn);
@@ -303,9 +300,7 @@ public static <E extends Exception, L, R> Either<L, R> trying(CheckedSupplier<E,
303300
* @param <E> the left parameter type (the most contravariant exception that supplier might throw)
304301
* @param <R> the right parameter type
305302
* @return the supplier result as a right value, or a left value of the thrown exception
306-
* @deprecated in favor of {@link Try#trying(CheckedSupplier)}
307303
*/
308-
@Deprecated
309304
public static <E extends Exception, R> Either<E, R> trying(CheckedSupplier<E, R> supplier) {
310305
return trying(supplier, id());
311306
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import java.util.function.Supplier;
1515

1616
import static com.jnape.palatable.lambda.adt.Either.left;
17-
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
18-
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
1917

2018
/**
2119
* The optional type, representing a potentially absent value. This is lambda's analog of {@link Optional}, supporting
@@ -153,7 +151,7 @@ public final Maybe<A> peek(Consumer<A> consumer) {
153151
* @return "Just" the right value, or nothing
154152
*/
155153
public static <A> Maybe<A> fromEither(Either<?, A> either) {
156-
return either.match(constantly(nothing()), Maybe::just);
154+
return either.toMaybe();
157155
}
158156

159157
/**
@@ -164,7 +162,7 @@ public static <A> Maybe<A> fromEither(Either<?, A> either) {
164162
* @return the equivalent Maybe instance
165163
*/
166164
public static <A> Maybe<A> fromOptional(Optional<? extends A> optional) {
167-
return optional.<A>map(id()).map(Maybe::just).orElse(Maybe.nothing());
165+
return optional.map(Maybe::<A>just).orElse(Maybe.nothing());
168166
}
169167

170168
/**

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ public void fromMaybeDoesNotEvaluateLeftFnForRight() {
168168
assertThat(atomicInteger.get(), is(0));
169169
}
170170

171+
@Test
172+
public void monadicTryingLiftsCheckedSupplier() {
173+
assertEquals(right(1), Either.trying(() -> 1));
174+
175+
Exception checkedException = new Exception("expected");
176+
assertEquals(left(checkedException), Either.trying(() -> {
177+
throw checkedException;
178+
}));
179+
}
180+
181+
@Test
182+
public void dyadicTryingLiftsCheckedSupplierMappingAnyThrownExceptions() {
183+
assertEquals(right(1), Either.trying(() -> 1, Throwable::getMessage));
184+
assertEquals(left("expected"), Either.trying(() -> {
185+
throw new Exception("expected");
186+
}, Throwable::getMessage));
187+
}
188+
171189
@Test
172190
public void monadicPeekLiftsIOToTheRight() {
173191
Either<String, Integer> left = left("foo");

0 commit comments

Comments
 (0)