I have a function to calculate the cumulated sum of a sequence.
let cumsum<'T> = Seq.scan (+) 0 >> Seq.skip 1 >> Seq.toArray
Though it looks generic, the integer 0
makes it non-generic, and thus I cannot call the function with a sequence of floats.
Is there a generic zero that can replace my hardcoded 0
, or maybe a different way of making the function generic.
You can use the GenericZero primitive but you need to make your function inline and make it explicitly a function (right now your function is written in point-free style) since in principle values cannot be made inline.
let inline cumsum s =
s |> Seq.scan (+) LanguagePrimitives.GenericZero |> Seq.skip 1 |> Seq.toArray
Note that by removing the Type parameter 'T
the static member constraints are inferred automatically by the compiler:
val inline cumsum :
s:seq< ^a> -> ^b []
when ( ^b or ^a) : (static member ( + ) : ^b * ^a -> ^b) and
^b : (static member get_Zero : -> ^b)
LanguagePrimitives.GenericZero
:)
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