Skip to content

Commit 5a01397

Browse files
authored
Option<>.collect() not to call PartialFunction collector on arguments where it is not defined (#2580)
1 parent a878d5d commit 5a01397

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/main/java/io/vavr/PartialFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public boolean isDefinedAt(V v) {
123123
* if the function is defined for the given arguments, and {@code None} otherwise.
124124
*/
125125
default Function1<T, Option<R>> lift() {
126-
return t -> Option.when(isDefinedAt(t), apply(t));
126+
return t -> Option.when(isDefinedAt(t), () -> apply(t));
127127
}
128128

129129
}

src/test/java/io/vavr/control/OptionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ public void shouldThrowExceptionOnNullCollectPartialFunction() {
563563
Option.some(1).collect(pf);
564564
}
565565

566+
@Test
567+
public void shouldNotCallPartialFunctionOnUndefinedArg() {
568+
final PartialFunction<Integer, Integer> pf = Function1.<Integer, Integer> of(x -> 1/x).partial(i -> i != 0);
569+
assertThat(Option.of(0).collect(pf)).isEqualTo(Option.none());
570+
}
571+
566572
// -- iterator
567573

568574
@Test

0 commit comments

Comments
 (0)