Skip to content

Commit d5e2291

Browse files
committed
Deprecating peek
1 parent 2f0cd6b commit d5e2291

File tree

21 files changed

+78
-144
lines changed

21 files changed

+78
-144
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
3535
- `WriterT`, a monad transformer for an accumulation and a value
3636
- `EquivalenceTrait`, a traitor `Trait` to make it easier to test properties of type-classes with a separate equivalence
3737
relation
38+
39+
### Deprecated
40+
- `Peek`, `Peek2`, `Maybe#peek`, and `Either#peek` in favor of explicitly matching into `IO` and running it
3841

3942
## [4.0.0] - 2019-05-20
4043
### Changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import com.jnape.palatable.lambda.functions.Fn0;
66
import com.jnape.palatable.lambda.functions.Fn1;
77
import com.jnape.palatable.lambda.functions.Fn2;
8-
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
9-
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2;
108
import com.jnape.palatable.lambda.functions.specialized.Pure;
119
import com.jnape.palatable.lambda.functions.specialized.SideEffect;
1210
import com.jnape.palatable.lambda.functor.Applicative;
@@ -19,9 +17,11 @@
1917

2018
import java.util.Objects;
2119

20+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
2221
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
2322
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
2423
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
24+
import static com.jnape.palatable.lambda.io.IO.io;
2525
import static java.util.Arrays.asList;
2626

2727
/**
@@ -166,9 +166,13 @@ public final Either<L, R> merge(Fn2<? super L, ? super L, ? extends L> leftFn,
166166
*
167167
* @param effect the effecting consumer
168168
* @return the Either, unaltered
169+
* @deprecated in favor of {@link Either#match(Fn1, Fn1) matching} into an {@link IO} and explicitly running it
169170
*/
171+
@Deprecated
170172
public Either<L, R> peek(Fn1<? super R, ? extends IO<?>> effect) {
171-
return Peek.peek(effect, this);
173+
return match(l -> io(Either.<L, R>left(l)),
174+
r -> effect.apply(r).fmap(constantly(this)))
175+
.unsafePerformIO();
172176
}
173177

174178
/**
@@ -177,9 +181,11 @@ public Either<L, R> peek(Fn1<? super R, ? extends IO<?>> effect) {
177181
* @param leftEffect the effecting consumer for left values
178182
* @param rightEffect the effecting consumer for right values
179183
* @return the Either, unaltered
184+
* @deprecated in favor of {@link Either#match(Fn1, Fn1) matching} into an {@link IO} and explicitly running it
180185
*/
186+
@Deprecated
181187
public Either<L, R> peek(Fn1<? super L, ? extends IO<?>> leftEffect, Fn1<? super R, ? extends IO<?>> rightEffect) {
182-
return Peek2.peek2(leftEffect, rightEffect, this);
188+
return match(leftEffect, rightEffect).fmap(constantly(this)).unsafePerformIO();
183189
}
184190

185191
/**

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
88
import com.jnape.palatable.lambda.functions.Fn0;
99
import com.jnape.palatable.lambda.functions.Fn1;
10-
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
1110
import com.jnape.palatable.lambda.functions.specialized.Pure;
1211
import com.jnape.palatable.lambda.functor.Applicative;
1312
import com.jnape.palatable.lambda.functor.Functor;
@@ -26,6 +25,7 @@
2625
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
2726
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
2827
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
28+
import static com.jnape.palatable.lambda.io.IO.io;
2929

3030
/**
3131
* The optional type, representing a potentially absent value. This is lambda's analog of {@link Optional}, supporting
@@ -223,9 +223,11 @@ public Choice2<A, Unit> invert() {
223223
*
224224
* @param effect the consumer
225225
* @return the same Maybe instance
226+
* @deprecated in favor of {@link Maybe#match(Fn1, Fn1) matching} into an {@link IO} and explicitly running it
226227
*/
228+
@Deprecated
227229
public final Maybe<A> peek(Fn1<? super A, ? extends IO<?>> effect) {
228-
return Peek.peek(effect, this);
230+
return match(constantly(io(this)), a -> effect.apply(a).fmap(constantly(this))).unsafePerformIO();
229231
}
230232

231233
@Override
@@ -302,7 +304,7 @@ public static <A> Maybe<A> nothing() {
302304
*
303305
* @return the {@link Pure} instance
304306
*/
305-
public static Pure<Maybe<? >> pureMaybe() {
307+
public static Pure<Maybe<?>> pureMaybe() {
306308
return Maybe::just;
307309
}
308310

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Peek.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*
1313
* @param <A> the functor parameter type
1414
* @param <FA> the functor type
15+
* @deprecated in favor of producing an {@link IO} from the given {@link Functor} and explicitly running it
1516
*/
17+
@Deprecated
1618
public final class Peek<A, FA extends Functor<A, ?>> implements Fn2<Fn1<? super A, ? extends IO<?>>, FA, FA> {
1719
private static final Peek<?, ?> INSTANCE = new Peek<>();
1820

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Peek2.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* @param <A> the bifunctor's first parameter type
1616
* @param <B> the bifunctor's second parameter type
1717
* @param <FAB> the bifunctor type
18+
* @deprecated in favor of producing an {@link IO} from the given {@link BoundedBifunctor} and explicitly running it
1819
*/
20+
@Deprecated
1921
public final class Peek2<A, B, FAB extends BoundedBifunctor<A, B, ? super A, ? super B, ?>> implements
2022
Fn3<Fn1<? super A, ? extends IO<?>>, Fn1<? super B, ? extends IO<?>>, FAB, FAB> {
2123
private static final Peek2<?, ?, ?> INSTANCE = new Peek2<>();

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
import testsupport.traits.TraversableLaws;
1616

1717
import java.util.concurrent.atomic.AtomicInteger;
18-
import java.util.concurrent.atomic.AtomicReference;
1918

2019
import static com.jnape.palatable.lambda.adt.Either.fromMaybe;
2120
import static com.jnape.palatable.lambda.adt.Either.left;
2221
import static com.jnape.palatable.lambda.adt.Either.right;
2322
import static com.jnape.palatable.lambda.adt.Maybe.just;
2423
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
2524
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
26-
import static com.jnape.palatable.lambda.functions.Effect.fromConsumer;
2725
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
2826
import static com.jnape.palatable.traitor.framework.Subjects.subjects;
2927
import static org.hamcrest.core.Is.is;
@@ -201,37 +199,6 @@ public void monadTryingWithRunnable() {
201199
assertEquals(left(expected), Either.trying(() -> {throw expected;}));
202200
}
203201

204-
@Test
205-
public void monadicPeekLiftsIOToTheRight() {
206-
Either<String, Integer> left = left("foo");
207-
Either<String, Integer> right = right(1);
208-
209-
AtomicInteger intRef = new AtomicInteger();
210-
211-
left.peek(fromConsumer(intRef::set));
212-
assertEquals(0, intRef.get());
213-
214-
right.peek(fromConsumer(intRef::set));
215-
assertEquals(1, intRef.get());
216-
}
217-
218-
@Test
219-
public void dyadicPeekDuallyLiftsIO() {
220-
Either<String, Integer> left = left("foo");
221-
Either<String, Integer> right = right(1);
222-
223-
AtomicReference<String> stringRef = new AtomicReference<>();
224-
AtomicInteger intRef = new AtomicInteger();
225-
226-
left.peek(fromConsumer(stringRef::set), fromConsumer(intRef::set));
227-
assertEquals("foo", stringRef.get());
228-
assertEquals(0, intRef.get());
229-
230-
right.peek(fromConsumer(stringRef::set), fromConsumer(intRef::set));
231-
assertEquals("foo", stringRef.get());
232-
assertEquals(1, intRef.get());
233-
}
234-
235202
@Test
236203
public void lazyZip() {
237204
assertEquals(right(2), right(1).lazyZip(lazy(right(x -> x + 1))).value());

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import testsupport.traits.TraversableLaws;
1414

1515
import java.util.Optional;
16-
import java.util.concurrent.atomic.AtomicInteger;
1716

1817
import static com.jnape.palatable.lambda.adt.Either.left;
1918
import static com.jnape.palatable.lambda.adt.Either.right;
@@ -25,7 +24,6 @@
2524
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
2625
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
2726
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
28-
import static com.jnape.palatable.lambda.io.IO.io;
2927
import static com.jnape.palatable.traitor.framework.Subjects.subjects;
3028
import static org.junit.Assert.assertEquals;
3129
import static org.junit.Assert.assertSame;
@@ -103,16 +101,6 @@ public void fromEither() {
103101
assertEquals(nothing(), Maybe.fromEither(left("failure")));
104102
}
105103

106-
@Test
107-
public void peek() {
108-
AtomicInteger ref = new AtomicInteger(0);
109-
assertEquals(just(1), just(1).peek(constantly(io(ref::incrementAndGet))));
110-
assertEquals(1, ref.get());
111-
112-
assertEquals(nothing(), nothing().peek(constantly(io(ref::incrementAndGet))));
113-
assertEquals(1, ref.get());
114-
}
115-
116104
@Test
117105
public void justOrThrow() {
118106
just(1).orElseThrow(IllegalStateException::new);

src/test/java/com/jnape/palatable/lambda/functions/builtin/fn2/Peek2Test.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/java/com/jnape/palatable/lambda/functions/builtin/fn2/PeekTest.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/java/testsupport/assertion/LensAssert.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static <S, A> void assertLensLawfulness(Optic<? super Fn1<?, ?>, Functor<
3434
.reduceLeft(asList(falsify("You get back what you put in", (s, b) -> view(lens, set(lens, b, s)), (s, b) -> b, cases),
3535
falsify("Putting back what you got changes nothing", (s, b) -> set(lens, view(lens, s), s), (s, b) -> s, cases),
3636
falsify("Setting twice is equivalent to setting once", (s, b) -> set(lens, b, set(lens, b, s)), (s, b) -> set(lens, b, s), cases)))
37-
.peek(failures -> IO.throwing(new AssertionError("Lens law failures\n\n" + failures)));
37+
.match(IO::io, failures -> IO.throwing(new AssertionError("Lens law failures\n\n" + failures)))
38+
.unsafePerformIO();
3839
}
3940

4041
private static <S, A, X> Maybe<String> falsify(String label, Fn2<S, A, X> l, Fn2<S, A, X> r,

src/test/java/testsupport/assertion/MonadErrorAssert.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ private MonadErrorAssert() {
2020
E e,
2121
Fn1<? super E, ? extends MA> recovery) {
2222
subjects.forEach(subject -> throwCatch(equivalence(subject, id()), e, recovery)
23-
.peek(failures -> IO.throwing(new AssertionError("MonadError law failures\n\n" + failures))));
23+
.match(IO::io, failures -> IO.throwing(new AssertionError("MonadError law failures\n\n" + failures)))
24+
.unsafePerformIO());
2425
}
2526

2627
public static <E, A, M extends MonadError<E, ?, M>, MA extends MonadError<E, A, M>> void assertLawsEq(
2728
Subjects<Equivalence<MA>> subjects,
2829
E e,
2930
Fn1<? super E, ? extends MA> recovery) {
3031
subjects.forEach(subject -> throwCatch(subject, e, recovery)
31-
.peek(failures -> IO.throwing(new AssertionError("MonadError law failures\n\n" + failures))));
32+
.match(IO::io, failures -> IO.throwing(new AssertionError("MonadError law failures\n\n" + failures)))
33+
.unsafePerformIO());
3234
}
3335

3436
private static <E, A, M extends MonadError<E, ?, M>, MA extends MonadError<E, A, M>> Maybe<String> throwCatch(

src/test/java/testsupport/assertion/PrismAssert.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public static <S, A> void assertPrismLawfulness(Prism<S, S, A, A> prism,
4242
falsify("A non-match result can always be converted back to an input",
4343
(s, b) -> new PrismResult<>(matching(prism, s).projectA().fmap(matching(prism))),
4444
(s, b) -> new PrismResult<>(just(left(s))), cases)))
45-
.peek(failures -> IO.throwing(new AssertionError("Lens law failures\n\n" + failures)));
45+
.match(IO::io, failures -> IO.throwing(new AssertionError("Lens law failures\n\n" + failures)))
46+
.unsafePerformIO();
4647
}
4748

4849
private static <S, A, X> Maybe<String> falsify(String label, Fn2<S, A, X> l, Fn2<S, A, X> r,

src/test/java/testsupport/matchers/LeftMatcher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public void describeTo(Description description) {
3030
@Override
3131
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
3232
mismatchDescription.appendText("was ");
33-
item.peek(l -> io(() -> {
34-
mismatchDescription.appendText("Left value of ");
35-
lMatcher.describeMismatch(l, mismatchDescription);
36-
}),
37-
r -> io(() -> mismatchDescription.appendValue(item)));
33+
item.match(l -> io(() -> {
34+
mismatchDescription.appendText("Left value of ");
35+
lMatcher.describeMismatch(l, mismatchDescription);
36+
}),
37+
r -> io(() -> mismatchDescription.appendValue(item)))
38+
.unsafePerformIO();
3839
}
3940

4041
public static <L, R> LeftMatcher<L, R> isLeftThat(Matcher<L> lMatcher) {

src/test/java/testsupport/matchers/RightMatcher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public void describeTo(Description description) {
3030
@Override
3131
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
3232
mismatchDescription.appendText("was ");
33-
item.peek(l -> io(() -> mismatchDescription.appendValue(item)),
34-
r -> io(() -> {
35-
mismatchDescription.appendText("Right value of ");
36-
rMatcher.describeMismatch(r, mismatchDescription);
37-
}));
33+
item.match(l -> io(() -> mismatchDescription.appendValue(item)),
34+
r -> io(() -> {
35+
mismatchDescription.appendText("Right value of ");
36+
rMatcher.describeMismatch(r, mismatchDescription);
37+
}))
38+
.unsafePerformIO();
3839
}
3940

4041
public static <L, R> RightMatcher<L, R> isRightThat(Matcher<R> rMatcher) {

src/test/java/testsupport/traits/ApplicativeLaws.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1414
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
1515
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
16+
import static com.jnape.palatable.lambda.io.IO.throwing;
1617
import static java.util.Arrays.asList;
1718

1819
public class ApplicativeLaws<App extends Applicative<?, App>> implements EquivalenceTrait<Applicative<?, App>> {
@@ -33,10 +34,11 @@ public void test(Equivalence<Applicative<?, App>> equivalence) {
3334
this::testInterchange,
3435
this::testDiscardL,
3536
this::testDiscardR,
36-
this::testLazyZip)
37-
)
38-
.peek(s -> IO.throwing(new AssertionError("The following Applicative laws did not hold for instance of "
39-
+ equivalence + ": \n\t - " + s)));
37+
this::testLazyZip))
38+
.match(IO::io,
39+
s -> throwing(new AssertionError("The following Applicative laws did not hold for instance of "
40+
+ equivalence + ": \n\t - " + s)))
41+
.unsafePerformIO();
4042
}
4143

4244
private Maybe<String> testIdentity(Equivalence<Applicative<?, App>> equivalence) {

src/test/java/testsupport/traits/BifunctorLaws.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static com.jnape.palatable.lambda.adt.Maybe.just;
1010
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
1111
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
12+
import static com.jnape.palatable.lambda.io.IO.throwing;
1213
import static java.util.Arrays.asList;
1314

1415
public class BifunctorLaws<BF extends Bifunctor<?, ?, BF>> implements EquivalenceTrait<Bifunctor<?, ?, BF>> {
@@ -27,8 +28,10 @@ public void test(Equivalence<Bifunctor<?, ?, BF>> equivalence) {
2728
this::testRightIdentity,
2829
this::testMutualIdentity)
2930
)
30-
.peek(s -> IO.throwing(new AssertionError("The following Bifunctor laws did not hold for instance of " +
31-
equivalence + ": \n\t - " + s)));
31+
.match(IO::io,
32+
s -> throwing(new AssertionError("The following Bifunctor laws did not hold for instance of " +
33+
equivalence + ": \n\t - " + s)))
34+
.unsafePerformIO();
3235
}
3336

3437
private Maybe<String> testLeftIdentity(Equivalence<Bifunctor<?, ?, BF>> equivalence) {

0 commit comments

Comments
 (0)