Skip to content

Instantly share code, notes, and snippets.

@TimBeyer
Last active December 21, 2015 19:19
Show Gist options
  • Save TimBeyer/6353217 to your computer and use it in GitHub Desktop.
Save TimBeyer/6353217 to your computer and use it in GitHub Desktop.
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();
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