Last active
December 26, 2019 04:03
-
-
Save matfournier/822c8084be61a86e83bd3671c33a9a6f to your computer and use it in GitHub Desktop.
conduit json parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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