diff --git a/src/FSharpAux.Core/ResizeArray.fs b/src/FSharpAux.Core/ResizeArray.fs index aeb9cae..3543e70 100644 --- a/src/FSharpAux.Core/ResizeArray.fs +++ b/src/FSharpAux.Core/ResizeArray.fs @@ -400,4 +400,36 @@ module ResizeArray = if curr > acc then acc <- curr accv <- currv - accv \ No newline at end of file + accv + +/// Sums all the values in the . + /// The input ResizeArray. + /// The resulting sum. + 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 + + /// Returns the sum of the results generated by applying the function () to each element of the . + /// The function to transform the ResizeArray elements into the type to be summed. + /// The input ResizeArray. + /// The resulting sum. + let inline sumBy ([] 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 + + /// Counts the number of elements in the satisfying the . + /// The function to transform the ResizeArray elements into the type to be summed. + /// The input ResizeArray. + /// Number of elements satisfying the . + let countIf (predicate: 'T -> bool) (arr: ResizeArray<'T>): int = + let mutable acc = 0 + for i=0 to arr.Count - 1 do + if predicate arr.[i] then + acc <- acc + 1 + acc \ No newline at end of file diff --git a/tests/FSharpAux.Tests/FSharpAux.Tests.fsproj b/tests/FSharpAux.Tests/FSharpAux.Tests.fsproj index 6dcd99a..6c2773b 100644 --- a/tests/FSharpAux.Tests/FSharpAux.Tests.fsproj +++ b/tests/FSharpAux.Tests/FSharpAux.Tests.fsproj @@ -9,6 +9,7 @@ + diff --git a/tests/FSharpAux.Tests/Main.fs b/tests/FSharpAux.Tests/Main.fs index 3699ef4..c3c946c 100644 --- a/tests/FSharpAux.Tests/Main.fs +++ b/tests/FSharpAux.Tests/Main.fs @@ -8,6 +8,7 @@ let all = [ SeqTests.seqTests ArrayTests.arrayTests + ResizeArrayTests.resizeArrayTests Array2DTests.array2dTests JaggedArrayTest.main ListTests.listTests diff --git a/tests/FSharpAux.Tests/ResizeArrayTests.fs b/tests/FSharpAux.Tests/ResizeArrayTests.fs new file mode 100644 index 0000000..debff3d --- /dev/null +++ b/tests/FSharpAux.Tests/ResizeArrayTests.fs @@ -0,0 +1,35 @@ +module ResizeArrayTests + +open FSharpAux +open Expecto + +let private emptyArray : ResizeArray = ResizeArray() +let private intArray = [6; 5; 2; 3; 2; 8] |> ResizeArray.ofList + +let resizeArrayTests = + testList "ResizeArrayTests" [ + testList "ResizeArray.sum" [ + testCase "Empty array sum is 0" (fun _ -> + Expect.equal (ResizeArray.sum emptyArray) 0 "ResizeArray.sum of empty array is not 0." + ) + testCase "returns correct sum" (fun _ -> + Expect.equal (ResizeArray.sum intArray) 26 "ResizeArray.sum calculates incorrectly" + ) + ] + testList "ResizeArray.sumBy" [ + testCase "Empty array sumBy is 0" (fun _ -> + Expect.equal (emptyArray |> ResizeArray.sumBy (fun x -> x * 2)) 0 "ResizeArray.sumBy of empty array is not 0." + ) + testCase "returns correct sum" (fun _ -> + Expect.equal (intArray |> ResizeArray.sumBy (fun x -> x * 2)) 52 "ResizeArray.sumBy calculates incorrectly" + ) + ] + testList "ResizeArray.countIf" [ + testCase "Empty array count is 0" (fun _ -> + Expect.equal (emptyArray |> ResizeArray.countIf (fun x -> x % 2 = 0)) 0 "ResizeArray.countIf of empty array is not 0." + ) + testCase "returns correct count" (fun _ -> + Expect.equal (intArray |> ResizeArray.countIf (fun x -> x % 2 = 0)) 4 "ResizeArray.countIf calculates incorrectly" + ) + ] + ] \ No newline at end of file