Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option<>.collect() not to call PartialFunction on args where it is not defined #2580

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/vavr/PartialFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public boolean isDefinedAt(V v) {
* if the function is defined for the given arguments, and {@code None} otherwise.
*/
default Function1<T, Option<R>> lift() {
return t -> Option.when(isDefinedAt(t), apply(t));
return t -> Option.when(isDefinedAt(t), () -> apply(t));
}

}
6 changes: 6 additions & 0 deletions src/test/java/io/vavr/control/OptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ public void shouldThrowExceptionOnNullCollectPartialFunction() {
Option.some(1).collect(pf);
}

@Test
public void shouldNotCallPartialFunctionOnUndefinedArg() {
final PartialFunction<Integer, Integer> pf = Function1.<Integer, Integer> of(x -> 1/x).partial(i -> i != 0);
assertThat(Option.of(0).collect(pf)).isEqualTo(Option.none());
}

// -- iterator

@Test
Expand Down