Skip to content

Instantly share code, notes, and snippets.

@Thorium
Created September 15, 2025 08:54
Show Gist options
  • Save Thorium/99bebae07c0d6663014c8f9627581e8b to your computer and use it in GitHub Desktop.
Save Thorium/99bebae07c0d6663014c8f9627581e8b to your computer and use it in GitHub Desktop.
Takes a list of (quantity/amount and price/rate) as decimals, and gives a weighted average
/// Takes a list of (quantity/amount and price/rate) as decimals, and gives a weighted average
let weightedAvg mylist =
let weightedValue = mylist |> List.sumBy(fun (isum,irate) -> isum*irate)
let weightedSum = mylist |> List.sumBy(fun (isum,irate) -> isum)
match weightedSum with
| 0m<GBP> -> 0m
| x -> weightedValue / x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment