Skip to content

Instantly share code, notes, and snippets.

@afcastano
Last active July 7, 2016 04:58
Show Gist options
  • Save afcastano/80783aeef3bc8478cb4f280e9c865335 to your computer and use it in GitHub Desktop.
Save afcastano/80783aeef3bc8478cb4f280e9c865335 to your computer and use it in GitHub Desktop.
Add two optionals using bind and unit functions.
public Optional<Integer> optionalAdd(Optional<Integer> val1, Optional<Integer> val2) {
return
val1.flatMap( first ->
val2.flatMap( second ->
Optional.of(first + second)
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment