Skip to content

Commit

Permalink
add shorthand to filter out nan values from array #249
Browse files Browse the repository at this point in the history
  • Loading branch information
DoganCK committed Feb 28, 2023
1 parent 4780203 commit 6b83908
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/FSharp.Stats/Array.fs
Expand Up @@ -500,4 +500,10 @@ module Array =
let inline covBy f (array: 'T array) : 'U =
array
|> Array.map f
|> covOfPairs
|> covOfPairs

/// Filters out all nan values from an array
let dropNaN (array: float array) =
array
|> Array.filter (System.Double.IsNaN >> not)

10 changes: 10 additions & 0 deletions tests/FSharp.Stats.Tests/Array.fs
Expand Up @@ -39,3 +39,13 @@ let medianTests =
let median = Array.median testArrayOddCountsInt
Expect.equal median 5 "Median should be 5"
]

[<Tests>]
let dropNanTests =
testList "Array" [
testCase "dropNaN" <| fun () ->
let testArray = [|-infinity; 0.5; 1.5; 1000.; nan; 5.0; nan|]
let expected = [|-infinity; 0.5; 1.5; 1000.; 5.0|]
let actual = Array.dropNaN testArray
Expect.equal expected actual "Filtered array is incorrect"
]
1 change: 1 addition & 0 deletions tests/FSharp.Stats.Tests/Main.fs
Expand Up @@ -34,6 +34,7 @@ let main argv =

//================================== Array ==============================================================
Tests.runTestsWithCLIArgs [] argv ArrayTests.medianTests |> ignore
Tests.runTestsWithCLIArgs [] argv ArrayTests.dropNanTests |> ignore

//================================== Interval ===========================================================
Tests.runTestsWithCLIArgs [] argv IntervalTests.intervalTests |> ignore
Expand Down

0 comments on commit 6b83908

Please sign in to comment.