type Foo(...) =
...
let a = Array.create 10 (new Foo())
let sum = Array.sum a
You need to add a few members that are used by the sum
function to do the calculation:
type Foo(value:int) =
member x.Value = value
// Two members that are needed by 'Array.sum'
static member (+) (a:Foo, b:Foo) = Foo(a.Value + b.Value)
static member Zero = Foo(0)
// Not needed for 'Array.sum', but allows 'Array.average'
static member DivideByInt(a:Foo, n:int) = Foo(a.Value / n)
The sum
function starts with the value returned by Zero
and then adds values of Foo
using the overloaded +
operator (average
then divides the result by an integer):
let a = Array.init 10 (fun n -> Foo(n))
let sum = Array.sum a
sum.Value // Returns 45
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With