Last active
December 21, 2015 19:19
-
-
Save TimBeyer/6353217 to your computer and use it in GitHub Desktop.
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
var items = [{'price': 123, 'tax': 0.1}, {'price': 234, 'tax': 0.3}]; | |
var taxCalculator = function (items) { | |
var taxedMapper = function (item) { | |
return item.price + item.price * item.tax; | |
}; | |
var nonTaxedMapper = function (item) { | |
return item.price; | |
}; | |
var total = function (items, mapper) { | |
var total = items.map(mapper).reduce(function (totalPrice, currentPrice) { | |
return totalPrice + currentPrice; | |
}, 0); | |
return total; | |
} | |
return { | |
taxedTotal: total.bind(null, items, taxedMapper), | |
nonTaxedTotal: total.bind(null, items, nonTaxedMapper) | |
} | |
}; | |
var calc = taxCalculator(items); | |
calc.taxedTotal(); | |
calc.nonTaxedTotal(); |
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
items = [{"price": 123, "tax": 0.1}, {"price": 234, "tax": 0.3}] | |
class TaxCalculator(object): | |
def __init__(self, items): | |
self.items = items | |
def taxedTotal(self): | |
return sum([item["price"]+ item["price"] * item["tax"] for item in self.items]) | |
def nonTaxedTotal(self): | |
return sum(item["price"] for item in self.items) | |
calc = TaxCalculator(items) | |
calc.taxedTotal() | |
calc.nonTaxedTotal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment