Skip to content

Instantly share code, notes, and snippets.

@afcastano
Last active July 14, 2016 13:42
Show Gist options
  • Save afcastano/90ddfd7c9b4c861c9cf7f7d617d4c628 to your computer and use it in GitHub Desktop.
Save afcastano/90ddfd7c9b4c861c9cf7f7d617d4c628 to your computer and use it in GitHub Desktop.
Monadic way of dealing with colour count.
private abstract Optional<Counter> fetchThisMonthCounts();
private abstract Optional<Counter> fetchPreviousMonthCounts();
public Optional<Integer> totalColour() {
return fetchThisMonthCounts().flatMap(Counter::colour).flatMap(colour1 ->
fetchPreviousMonthCounts().flatMap(Counter::colour).flatMap(colour2 ->
Optional.of(colour1 + colour2)
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment