4 ms·
In support of the article, your code would still throw an exception if all the customers in the list have missing credit. https://github.com/dotnet/fsharp/blob
by vikestep 7y ago
In support of the article, your code would still throw an exception if all the customers in the list have missing credit.
https://github.com/dotnet/fsharp/blob/master/src/fsharp/FSharp.Core/list.fs#L627 https://github.com/dotnet/fsharp/blob/master/src/fsharp/FSha...
You could instead write it as the following to handle that.
let average (customers : Customer list) =
customers
|> List.choose (fun c -> c.Credit)
|> function
| [] -> Error "no customers with credit"
| xs -> Ok (List.average xs)
- UK-Al05 7y agoIndeed. I was remembering the behaviour of average off the top of my head.