Last active
July 30, 2017 18:45
-
-
Save nachivpn/9cd66eedd1576bf20419f08992845539 to your computer and use it in GitHub Desktop.
ProductPrice.java
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
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