Save as trie

This commit is contained in:
Mats Rauhala 2020-12-11 17:55:35 +02:00
parent 22b143aac7
commit 91578bfb03
2 changed files with 16 additions and 4 deletions

View File

@ -36,6 +36,7 @@ library
, bytestring-trie , bytestring-trie
, vector , vector
, containers , containers
, filepath
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010
ghc-options: -Wall ghc-options: -Wall

View File

@ -5,6 +5,7 @@ import qualified Data.Text as T
import Conduit import Conduit
import qualified Data.Conduit.Binary as CB import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.Combinators as C import qualified Data.Conduit.Combinators as C
import qualified Data.Conduit.List as CL
import qualified Data.Conduit.Text as CT import qualified Data.Conduit.Text as CT
import Data.Email import Data.Email
@ -16,7 +17,14 @@ import System.IO
import qualified Data.Foldable as F import qualified Data.Foldable as F
import qualified Data.Set as S import qualified Data.Trie as Trie
import Data.Maybe
(fromMaybe)
import System.Environment
(lookupEnv)
import System.FilePath
((</>))
combine :: (MonadUnliftIO m, MonadResource m, MonadThrow m, MonadIO m) => ConduitM FilePath Header m () combine :: (MonadUnliftIO m, MonadResource m, MonadThrow m, MonadIO m) => ConduitM FilePath Header m ()
combine = await >>= \case combine = await >>= \case
@ -25,8 +33,10 @@ combine = await >>= \case
run :: IO () run :: IO ()
run = do run = do
x <- runResourceT $ runConduit stream datDir <- fromMaybe "./" <$> lookupEnv "HOME"
F.for_ x print runResourceT $ do
x <- runConduit stream
runConduit (CL.sourceList (Trie.keys x) .| C.map (<> "\n") .| CB.sinkFileCautious (datDir </> ".addressbook.dat"))
where where
separate = \case separate = \case
From x -> [x] From x -> [x]
@ -38,4 +48,5 @@ run = do
.| C.map T.unpack .| C.map T.unpack
.| combine .| combine
.| C.concatMap separate .| C.concatMap separate
.| C.foldMap (S.singleton) .| CT.encode CT.utf8
.| C.foldMap (`Trie.singleton` ())