Skip to content

Commit c6a9ff9

Browse files
committed
Adding Peek2
1 parent 6b7a34b commit c6a9ff9

4 files changed

Lines changed: 92 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
3030
- `CollectionLens#asSet(Function)`, a proper analog of `CollectionLens#asSet()` that uses defensive copies
3131
- `CollectionLens#asStream(Function)`, a proper analog of `CollectionLens#asStream()` that uses defensive copies
3232
- Explicitly calling attention to all unlawful lenses in their documentation
33-
- `Peek`, for "peeking" at the value contained inside any given `Functor` with a given side-effect
33+
- `Peek` and `Peek2`, for "peeking" at the value contained inside any given `Functor` or `Bifunctor` with given side-effects
3434

3535
### Removed
3636
- `Either#toOptional`, deprecated in previous release

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
44
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
5+
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2;
56
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
67
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
78
import com.jnape.palatable.lambda.functor.Applicative;
@@ -183,13 +184,7 @@ public Either<L, R> peek(Consumer<R> rightConsumer) {
183184
* @return the Either, unaltered
184185
*/
185186
public Either<L, R> peek(Consumer<L> leftConsumer, Consumer<R> rightConsumer) {
186-
return flatMap(l -> {
187-
leftConsumer.accept(l);
188-
return this;
189-
}, r -> {
190-
rightConsumer.accept(r);
191-
return this;
192-
});
187+
return Peek2.peek2(leftConsumer, rightConsumer, this);
193188
}
194189

195190
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
import com.jnape.palatable.lambda.functions.Fn3;
6+
import com.jnape.palatable.lambda.functor.Bifunctor;
7+
8+
import java.util.function.Consumer;
9+
import java.util.function.Function;
10+
11+
/**
12+
* Given two {@link Consumer}s, "peek" at the values contained inside a {@link Bifunctor} via
13+
* {@link Bifunctor#biMap(Function, Function)}, applying the {@link Consumer}s to the contained values,
14+
* if there are any.
15+
*
16+
* @param <A> the bifunctor's first parameter type
17+
* @param <B> the bifunctor's second parameter type
18+
* @param <FAB> the bifunctor type
19+
*/
20+
public final class Peek2<A, B, FAB extends Bifunctor<A, B, ?>> implements Fn3<Consumer<? super A>, Consumer<? super B>, FAB, FAB> {
21+
private static final Peek2 INSTANCE = new Peek2<>();
22+
23+
private Peek2() {
24+
}
25+
26+
@Override
27+
@SuppressWarnings("unchecked")
28+
public FAB apply(Consumer<? super A> aConsumer, Consumer<? super B> bConsumer, FAB fab) {
29+
return (FAB) fab.biMap(a -> {
30+
aConsumer.accept(a);
31+
return a;
32+
}, b -> {
33+
bConsumer.accept(b);
34+
return b;
35+
});
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
public static <A, B, FAB extends Bifunctor<A, B, ?>> Peek2<A, B, FAB> peek2() {
40+
return INSTANCE;
41+
}
42+
43+
public static <A, B, FAB extends Bifunctor<A, B, ?>> Fn2<Consumer<? super B>, FAB, FAB> peek2(
44+
Consumer<? super A> aConsumer) {
45+
return Peek2.<A, B, FAB>peek2().apply(aConsumer);
46+
}
47+
48+
public static <A, B, FAB extends Bifunctor<A, B, ?>> Fn1<FAB, FAB> peek2(Consumer<? super A> aConsumer,
49+
Consumer<? super B> bConsumer) {
50+
return Peek2.<A, B, FAB>peek2(aConsumer).apply(bConsumer);
51+
}
52+
53+
public static <A, B, FAB extends Bifunctor<A, B, ?>> FAB peek2(Consumer<? super A> aConsumer,
54+
Consumer<? super B> bConsumer,
55+
FAB fab) {
56+
return Peek2.<A, B, FAB>peek2(aConsumer, bConsumer).apply(fab);
57+
}
58+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.adt.Either;
4+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
5+
import org.junit.Test;
6+
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
import static com.jnape.palatable.lambda.adt.Either.right;
10+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Peek2.peek2;
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class Peek2Test {
15+
16+
@Test
17+
public void peeksAtBothBifunctorValues() {
18+
AtomicInteger counter = new AtomicInteger(0);
19+
Tuple2<Integer, Integer> tuple = tuple(1, 2);
20+
assertEquals(tuple, peek2(__ -> counter.incrementAndGet(), __ -> counter.incrementAndGet(), tuple));
21+
assertEquals(2, counter.get());
22+
}
23+
24+
@Test
25+
public void followsSameConventionsAsBimap() {
26+
AtomicInteger counter = new AtomicInteger(0);
27+
Either<Object, Integer> either = right(1);
28+
peek2(__ -> counter.incrementAndGet(), __ -> counter.incrementAndGet(), either);
29+
assertEquals(1, counter.get());
30+
}
31+
}

0 commit comments

Comments
 (0)