Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Last active December 15, 2015 19:03
Show Gist options
  • Save charles-cooper/153a539617a3fd01a31b to your computer and use it in GitHub Desktop.
Save charles-cooper/153a539617a3fd01a31b to your computer and use it in GitHub Desktop.
nan = 0/0
silenceNaN d = if isNaN d then 0 else d
cumSumWithMissing :: [Double] -> [Double]
cumSumWithMissing ds = let
ret = scanl1 (+) $ silenceNaN <$> ds
go x y = if isNaN x then x else y
in zipWith go ds ret
foo :: [Double] -> [Double]
foo ds = let
go x y = if isNaN x then x else y
in zipWith go ds $ silenceNaN <$> ds
main = do
print $ foo [1,2,nan,3]
print $ cumSumWithMissing [1,2,nan,3]
$ runghc foo.hs
[1.0,2.0,NaN,3.0]
[1.0,3.0,NaN,6.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment