Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created January 14, 2026 14:19
Show Gist options
  • Select an option

  • Save jacobsapps/7c8796ddb4e113c0f2fa18098f137258 to your computer and use it in GitHub Desktop.

Select an option

Save jacobsapps/7c8796ddb4e113c0f2fa18098f137258 to your computer and use it in GitHub Desktop.
struct PurchaseEvent: Hashable {
let customerId: String
let date: String
let amount: Double
}
final class RevenueTracker {
private var totalsByDate: [String: [String: Double]] = [:]
func addPurchase(_ event: PurchaseEvent) {
var totals = totalsByDate[event.date, default: [:]]
totals[event.customerId, default: 0] += event.amount
totalsByDate[event.date] = totals
}
func averageRevenuePerCustomer(on date: String) -> Double {
guard let totals = totalsByDate[date], !totals.isEmpty else {
return 0
}
let totalRevenue = totals.values.reduce(0, +)
return totalRevenue / Double(totals.count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment