Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Last active July 30, 2017 18:45
Show Gist options
  • Save nachivpn/9cd66eedd1576bf20419f08992845539 to your computer and use it in GitHub Desktop.
Save nachivpn/9cd66eedd1576bf20419f08992845539 to your computer and use it in GitHub Desktop.
ProductPrice.java
Map<String,Component> components;
public List<String> getComponentNames(String product){
return productDetails.get(product);
}
//given a product, return the sum of prices of it's components
//if anything fails, return that specific exception
public Either<Exception,Double> getProductPrice(String product){
return getComponentNames(product)
.stream()
//from product name string, get componenet as Either<Exception,Component>
.map(this::getComponent)
//given an Either<Exception, Component>, return it's price as Either<Exception,Double>
.map(this::getComponentPrice)
//given two Either<Exception,Double> prices, add their prices and make a new Either<Exception,Double>
.reduce((x,y)-> x.bind(xPrice -> y.bind(yPrice -> new Right<>(xPrice + yPrice))))
//get the final sum of prices as Either<Exception,Double>
.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment