Created
July 28, 2017 09:15
-
-
Save whirlwin/7a5ae60d655a706e06118f62ce56bfe8 to your computer and use it in GitHub Desktop.
vavr-optional-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imperativt | |
Person person = new Person("John Doe"); | |
Optional<Person> maybePerson = getPersonFromCache(); // Option<Person> | |
if (!maybePersonFromCache.isPresent()) { | |
Optional<Person> maybePersonFromRepo = getPersonFromRepo(); | |
if (!maybePersonFromRepo.isPresent()) { | |
Optional<Person> maybePersonFromApi = getFromAPI(); | |
if (maybePersonFromApi.isPresent()) { | |
person = person.get(); | |
} | |
} | |
} | |
// Funksjonelt | |
getPersonFromCache() | |
.orElse(getPersonFromRepo()) | |
.orElse(getPersonFromAPI()) | |
.getOrElse(new Person("John Doe")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dette er et eksempel der funksjonell programmering gjør koden lettere å lese, og forhåpentligvis forstå!