Skip to content

Commit

Permalink
Option<>.collect() not to call PartialFunction collector on arguments…
Browse files Browse the repository at this point in the history
… where it is not defined (#2580)
  • Loading branch information
sleepytomcat authored and danieldietrich committed Jul 14, 2021
1 parent dba275f commit bd2127a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vavr/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 vavr/src/test/java/io/vavr/control/OptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,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

0 comments on commit bd2127a

Please sign in to comment.