Skip to content

Commit

Permalink
add sum sumBy and CountIf functionality to ResArr
Browse files Browse the repository at this point in the history
  • Loading branch information
DoganCK committed Nov 1, 2023
1 parent a235fd3 commit 066c414
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/FSharpAux.Core/ResizeArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,25 @@ module ResizeArray =
if curr > acc then
acc <- curr
accv <- currv
accv
accv

let inline sum (array: ResizeArray< ^T>) =
checkNonNull "array" array
let mutable acc = LanguagePrimitives.GenericZero< ^T>
for i = 0 to array.Count - 1 do
acc <- Checked.(+) acc array.[i]
acc

let inline sumBy ([<InlineIfLambda>] projection: 'T -> ^R) (array: ResizeArray<'T>) =
checkNonNull "array" array
let mutable acc = LanguagePrimitives.GenericZero< ^R>
for i = 0 to array.Count - 1 do
acc <- Checked.(+) acc (projection array.[i])
acc

let countIf (projection: 'T -> bool) (arr: ResizeArray<'T>): int =
let mutable acc = 0
for i=0 to arr.Count - 1 do
if projection arr.[i] then
acc <- acc + 1
acc

0 comments on commit 066c414

Please sign in to comment.