Skip to content

Commit 1db219c

Browse files
committed
Lens#both specialized for simple lenses
1 parent e4a6b30 commit 1db219c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/main/java/com/jnape/palatable/lambda/lens/Lens.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ static <S, A, B, C, D> Lens<S, S, Tuple2<A, B>, Tuple2<C, D>> both(Lens<S, S, A,
288288
return lens(Both.both(view(f), view(g)), (s, cd) -> cd.biMap(set(f), set(g)).into(Fn1::compose).apply(s));
289289
}
290290

291+
/**
292+
* Dually focus on two simple lenses at the same time.
293+
*
294+
* @param f the first lens
295+
* @param g the second lens
296+
* @param <S> both larger values
297+
* @param <A> both smaller viewing values
298+
* @param <B> both smaller setting values
299+
* @return the dual-focus simple lens
300+
*/
301+
static <S, A, B> Lens.Simple<S, Tuple2<A, B>> both(Lens.Simple<S, A> f, Lens.Simple<S, B> g) {
302+
return adapt(both((Lens<S, S, A, A>) f, g));
303+
}
304+
291305
/**
292306
* A convenience type with a simplified type signature for common lenses with both unified "larger" values and
293307
* unified "smaller" values.

src/test/java/com/jnape/palatable/lambda/lens/LensTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@ public void bothSplitsFocusBetweenLenses() {
9494
assertEquals(tuple('a', 3), view(both, "abc"));
9595
assertEquals("zb", set(both, tuple('z', 2), "abc"));
9696
}
97+
98+
@Test
99+
public void bothForSimpleLenses() {
100+
Lens.Simple<String, Integer> stringToInt = simpleLens(Integer::parseInt, (s, i) -> s + i.toString());
101+
Lens.Simple<String, Character> stringToChar = simpleLens(s -> s.charAt(0), (s, c) -> s + c.toString());
102+
103+
assertEquals(tuple(3, '3'), view(both(stringToInt, stringToChar), "3"));
104+
assertEquals("133", set(both(stringToInt, stringToChar), tuple(3, '3'), "1"));
105+
}
97106
}

0 commit comments

Comments
 (0)