Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sum sumBy and CountIf functionality to ResArr #41

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/FSharpAux.Core/ResizeArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,36 @@ module ResizeArray =
if curr > acc then
acc <- curr
accv <- currv
accv
accv

/// <summary>Sums all the values in the <paramref name="array"/>.</summary>
/// <param name="array">The input ResizeArray.</param>
/// <returns>The resulting sum.</returns>
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

/// <summary>Returns the sum of the results generated by applying the function (<paramref name="projection"/>) to each element of the <paramref name="array"/>.</summary>
/// <param name="projection">The function to transform the ResizeArray elements into the type to be summed.</param>
/// <param name="array">The input ResizeArray.</param>
/// <returns>The resulting sum.</returns>
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

/// <summary>Counts the number of elements in the <paramref name="array"/> satisfying the <paramref name="predicate"/>.</summary>
/// <param name="predicate">The function to transform the ResizeArray elements into the type to be summed.</param>
/// <param name="array">The input ResizeArray.</param>
/// <returns>Number of elements satisfying the <paramref name="predicate"/>.</returns>
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
1 change: 1 addition & 0 deletions tests/FSharpAux.Tests/FSharpAux.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<Compile Include="SeqTests.fs" />
<Compile Include="ArrayTests.fs" />
<Compile Include="ResizeArrayTests.fs" />
<Compile Include="Array2DTests.fs" />
<Compile Include="JaggedArrayTest.fs" />
<Compile Include="ListTests.fs" />
Expand Down
1 change: 1 addition & 0 deletions tests/FSharpAux.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let all =
[
SeqTests.seqTests
ArrayTests.arrayTests
ResizeArrayTests.resizeArrayTests
Array2DTests.array2dTests
JaggedArrayTest.main
ListTests.listTests
Expand Down
35 changes: 35 additions & 0 deletions tests/FSharpAux.Tests/ResizeArrayTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module ResizeArrayTests

open FSharpAux
open Expecto

let private emptyArray : ResizeArray<int> = 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"
)
]
]