Skip to content

Instantly share code, notes, and snippets.

@mgryszko
Last active February 9, 2021 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgryszko/1c46b302f2b75b080076a84d12ac5c29 to your computer and use it in GitHub Desktop.
Save mgryszko/1c46b302f2b75b080076a84d12ac5c29 to your computer and use it in GitHub Desktop.
type Quantity = Int
type Amount = BigDecimal
type Pricer = Quantity => (Quantity, Amount)
def total(pricings: List[(Quantity, List[Pricer])]): Amount = {
pricings.foldLeft(BigDecimal(0)) { case (grandTotal, (quantity, pricers)) =>
grandTotal + total(quantity, pricers)
}
}
private def total(quantity: Quantity, pricers: List[Pricer]) = {
val (_, total) = pricers.foldLeft((quantity, BigDecimal(0))) {
case ((quantity, total), pricer) =>
val (remainingQuantity, amount) = pricer(quantity)
(remainingQuantity, total + amount)
}
total
}
val priceByTier: (Quantity, Amount) => Pricer = { (quantityPerTier, tierPrice) => { quantity =>
val tiers = quantity / quantityPerTier
(quantity % quantityPerTier, tiers * tierPrice)
} }
val priceByUnit: Amount => Pricer = { unitPrice => { quantity =>
(0, quantity * unitPrice)
} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment