From 066c414a0eab050975f859db5a639b8a917c1b67 Mon Sep 17 00:00:00 2001 From: Dogan Kazakli Date: Wed, 1 Nov 2023 21:30:39 +0300 Subject: [PATCH 1/3] add sum sumBy and CountIf functionality to ResArr --- src/FSharpAux.Core/ResizeArray.fs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/FSharpAux.Core/ResizeArray.fs b/src/FSharpAux.Core/ResizeArray.fs index aeb9cae..17c5d2e 100644 --- a/src/FSharpAux.Core/ResizeArray.fs +++ b/src/FSharpAux.Core/ResizeArray.fs @@ -400,4 +400,25 @@ module ResizeArray = if curr > acc then acc <- curr accv <- currv - accv \ No newline at end of file + 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 ([] 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 \ No newline at end of file From 8eae9a95375954e12bc757f7130a8aee36749b39 Mon Sep 17 00:00:00 2001 From: Dogan Kazakli Date: Sat, 4 Nov 2023 13:33:45 +0300 Subject: [PATCH 2/3] add resizearray tests --- tests/FSharpAux.Tests/FSharpAux.Tests.fsproj | 1 + tests/FSharpAux.Tests/Main.fs | 1 + tests/FSharpAux.Tests/ResizeArrayTests.fs | 35 ++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/FSharpAux.Tests/ResizeArrayTests.fs 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 From 19358ac222a14033644610f73d52d5cfefbfb98c Mon Sep 17 00:00:00 2001 From: Dogan Kazakli Date: Mon, 6 Nov 2023 19:44:14 +0300 Subject: [PATCH 3/3] add docs for resizearray --- src/FSharpAux.Core/ResizeArray.fs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/FSharpAux.Core/ResizeArray.fs b/src/FSharpAux.Core/ResizeArray.fs index 17c5d2e..3543e70 100644 --- a/src/FSharpAux.Core/ResizeArray.fs +++ b/src/FSharpAux.Core/ResizeArray.fs @@ -402,6 +402,9 @@ module ResizeArray = accv <- currv 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> @@ -409,6 +412,10 @@ module ResizeArray = 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> @@ -416,9 +423,13 @@ module ResizeArray = acc <- Checked.(+) acc (projection array.[i]) acc - let countIf (projection: 'T -> bool) (arr: ResizeArray<'T>): int = + /// 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 projection arr.[i] then + if predicate arr.[i] then acc <- acc + 1 acc \ No newline at end of file