Opps... Yes. You are right. Thanks. As I understand if names2.txt is a "big" file this is a best way to process it. Another way is to load all data of the file into memory (to [], for example). Or there is another better way to generate all combinations of data?
пятница, 6 февраля 2015 г., 0:06:14 UTC+6 пользователь Лев Никитин написал: > > > I'm trying to generate pairs of words from two files. Here is a Main.hs: > > > {-# LANGUAGE OverloadedStrings, FlexibleContexts #-} > > import qualified Data.Text as T > > import System.IO > > import Control.Monad.Managed > > import Pipes > import qualified Pipes.Prelude as P hiding (fromHandle) > import qualified Pipes.Text.IO as P > import qualified Pipes.Text as PT > import qualified Pipes.Group as PG > import Lens.Family (view) > > inFile ⦂ FilePath → Managed Handle > inFile filePath = managed (withFile filePath ReadMode) > > badTest ⦂ MonadIO m => Handle → Handle → Producer T.Text m () > badTest names1 names2 = > for (PG.concats ∘ view PT.lines $ P.fromHandle names1) $ λn1 → > for (PG.concats ∘ view PT.lines $ P.fromHandle names2) $ λn2 → yield $ > n1 `T.append` " ||| " `T.append` n2 > > goodTest ⦂ MonadIO m => Producer T.Text m () > goodTest = > for (each ["A", "B", "C", "D"]) $ λn1 → > for (each ["1", "2", "3", "4"]) $ λn2 → yield $ n1 `T.append` " ||| " > `T.append` n2 > > main ⦂ IO () > main = runManaged $ do > names1 ← inFile "names1.txt" > names2 ← inFile "names2.txt" > liftIO $ runEffect $ badTest names1 names2 >-> P.map ((`T.append` "\n")) > >-> P.stdout > liftIO $ putStrLn "-------------------------" > liftIO $ runEffect $ goodTest >-> P.map ((`T.append` "\n")) >-> P.stdout > > > where names1.txt containts: > A > B > C > D > > names2.txt: > 1 > 2 > 3 > > I've got result: > > A ||| 1 > A ||| 2 > A ||| 3 > ------------------------- > A ||| 1 > A ||| 2 > A ||| 3 > A ||| 4 > B ||| 1 > B ||| 2 > B ||| 3 > B ||| 4 > C ||| 1 > C ||| 2 > C ||| 3 > C ||| 4 > D ||| 1 > D ||| 2 > D ||| 3 > D ||| 4 > > It means that badTest produces only first line of names1.txt file. But > similar goodTest does work right. What I am missed? > > -- You received this message because you are subscribed to the Google Groups "Haskell Pipes" group. To unsubscribe from this group and stop receiving emails from it, send an email to haskell-pipes+unsubscr...@googlegroups.com. To post to this group, send email to haskell-pipes@googlegroups.com.