From a4da4f73da5bf51e6957dea75a2436ba335b4a05 Mon Sep 17 00:00:00 2001 From: Mats Rauhala Date: Thu, 10 Dec 2020 23:20:38 +0200 Subject: [PATCH] Reduce into unique addresses --- addressbook.cabal | 1 + default.nix | 8 ++++---- src/Control/Addressbook/Streaming.hs | 21 +++++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/addressbook.cabal b/addressbook.cabal index cad62e9..7f70620 100644 --- a/addressbook.cabal +++ b/addressbook.cabal @@ -35,6 +35,7 @@ library , text , bytestring-trie , vector + , containers hs-source-dirs: src default-language: Haskell2010 diff --git a/default.nix b/default.nix index 53b64d1..1ab2289 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,7 @@ { mkDerivation, attoparsec, base, bytestring, bytestring-trie -, conduit, conduit-extra, hedgehog, hedgehog-corpus, HUnit, lens -, mtl, optparse-applicative, stdenv, tasty, tasty-hedgehog -, tasty-hunit, text, vector +, conduit, conduit-extra, containers, hedgehog, hedgehog-corpus +, HUnit, lens, mtl, optparse-applicative, stdenv, tasty +, tasty-hedgehog, tasty-hunit, text, vector }: mkDerivation { pname = "addressbook"; @@ -11,7 +11,7 @@ mkDerivation { isExecutable = true; libraryHaskellDepends = [ attoparsec base bytestring bytestring-trie conduit conduit-extra - lens mtl text vector + containers lens mtl text vector ]; executableHaskellDepends = [ base optparse-applicative ]; testHaskellDepends = [ diff --git a/src/Control/Addressbook/Streaming.hs b/src/Control/Addressbook/Streaming.hs index d9f90f8..b209773 100644 --- a/src/Control/Addressbook/Streaming.hs +++ b/src/Control/Addressbook/Streaming.hs @@ -9,11 +9,15 @@ import qualified Data.Conduit.Text as CT import Data.Email import Data.Email.Header - (Header) + (Header(..)) import System.IO (stdin) +import qualified Data.Foldable as F + +import qualified Data.Set as S + combine :: (MonadUnliftIO m, MonadResource m, MonadThrow m, MonadIO m) => ConduitM FilePath Header m () combine = await >>= \case Nothing -> pure () @@ -21,4 +25,17 @@ combine = await >>= \case run :: IO () run = do - runResourceT $ runConduit (CB.sourceHandle stdin .| CT.decode CT.utf8 .| CT.lines .| C.map T.unpack .| combine .| C.mapM_ (liftIO . print)) + x <- runResourceT $ runConduit stream + F.for_ x print + where + separate = \case + From x -> [x] + To xs -> F.toList xs + stream = + CB.sourceHandle stdin + .| CT.decode CT.utf8 + .| CT.lines + .| C.map T.unpack + .| combine + .| C.concatMap separate + .| C.foldMap (S.singleton)