DEV Community

Discussion on: Using the new INumber type to generify math functions in .NET 7

Collapse
 
pbouillon profile image
Pierre Bouillon

You are absolutely right, thanks for pointing that!

Indeed my example might not have been the most explicit one, I should have talked about summing only taxes (double) to avoid confusion

Collapse
 
armousness profile image
Sean Williams

The reason I like this is that it lets you turn fold into sum easily, something like,

let fold collection initial f =
    let mutable res = initial
    for item in collection do
        res <- f res item
    res

let sum (numbers: #INumber<'t>) = fold numbers (INumber<'t>.Zero) (+)

let product (numbers: #INumber<'t>) = fold numbers (INumber<'t>.One) (*)
Enter fullscreen mode Exit fullscreen mode

or however that syntax is going to work out. Then there are a lot of other things you can do with that sort of pattern. Being able to write more complex stuff, like weighted average and standard deviation, is pretty hot.