Skip to content

Instantly share code, notes, and snippets.

@matfournier
Last active December 26, 2019 04:03
Show Gist options
  • Save matfournier/822c8084be61a86e83bd3671c33a9a6f to your computer and use it in GitHub Desktop.
Save matfournier/822c8084be61a86e83bd3671c33a9a6f to your computer and use it in GitHub Desktop.
conduit json parsing
avgForClient :: Purchase -> Double
avgForClient (Purchase _ products) =
let n = fromIntegral $ DT.length products
totals = sum $ price <$> products
in totals / n
-- outputs nullnullnullnull... unless the json is on a single line.
-- how to make this work for multi-line json?
main :: IO ()
main =
runConduitRes
$ B.sourceFile "ch10.json"
.| B.lines
.| L.map LB.fromStrict -- ?? L.foldMap (id . LB.fromStrict) to read all the files in memory at once?
.| L.map (\x -> (fmap avgForClient $ decode x))
.| L.map (LB.toStrict . encode)
.| B.sinkFile "ch10.out.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment