Last active
August 19, 2018 22:43
-
-
Save marrionluaka/989ab0ae05cad73ab5533ad8b90e559a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const applyDiscount = (price, discount) => | |
moneyToFloat(price) // => Box(3.99) | |
.fold(cost => | |
percentToFloat(discount) // => Box(0.15) | |
.fold(savings => cost - cost * savings)); // 3.99 - (3.99 * 0.15) |
This file contains hidden or 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
const Box = x => | |
({ | |
map: fn => Box(fn(x)), | |
fold: fn => fn(x) | |
}); |
This file contains hidden or 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
const moneyToFloat = str => | |
Box(str) | |
.map(s => s.replace(/\$/g, "")) | |
.fold(s => parseFloat(s)); |
This file contains hidden or 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
const moneyToFloat = str => parseFloat(str.replace(/\$/g, "")); |
This file contains hidden or 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
const moneyToFloat = str => | |
Box(str) | |
.map(s => s.replace(/\$/g, "")) | |
.map(s => parseFloat(s)); |
This file contains hidden or 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
const percentToFloat = str => | |
Box(str.replace(/\%/g, "")) | |
.map(replaced => parseFloat(replaced)) | |
.map(number => number * 0.01); |
This file contains hidden or 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
const result = applyDiscount("$3.99", "15%"); | |
// => 3.3915 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment