diff options
-rw-r--r-- | Pipes/Prelude/Text.hs | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/Pipes/Prelude/Text.hs b/Pipes/Prelude/Text.hs index d573e51..ee3e104 100644 --- a/Pipes/Prelude/Text.hs +++ b/Pipes/Prelude/Text.hs | |||
@@ -42,7 +42,7 @@ import Prelude hiding (readFile, writeFile) | |||
42 | 42 | ||
43 | >>> import Pipes | 43 | >>> import Pipes |
44 | >>> import qualified Pipes.Prelude as P | 44 | >>> import qualified Pipes.Prelude as P |
45 | >>> import qualified Pipes.Text.IO as Text | 45 | >>> import qualified Pipes.Prelude.Text as Text |
46 | >>> import qualified Data.Text as T | 46 | >>> import qualified Data.Text as T |
47 | >>> Text.runSafeT $ runEffect $ Text.stdinLn >-> P.take 3 >-> P.map T.toUpper >-> Text.writeFileLn "threelines.txt" | 47 | >>> Text.runSafeT $ runEffect $ Text.stdinLn >-> P.take 3 >-> P.map T.toUpper >-> Text.writeFileLn "threelines.txt" |
48 | one<Enter> | 48 | one<Enter> |
@@ -53,33 +53,38 @@ ONE | |||
53 | TWO | 53 | TWO |
54 | THREE | 54 | THREE |
55 | 55 | ||
56 | The point of view is very much that of @Pipes.Prelude@. It would still be the same even if | 56 | Here @runSafeT@ from @Pipes.Safe@ just makes sure to close any handles opened in its scope. |
57 | Otherwise the point of view is very much that of @Pipes.Prelude@, substituting @Text@ for @String@. | ||
58 | It would still be the same even if | ||
57 | we did something a bit more sophisticated, like run an ordinary attoparsec 'Text' parser on | 59 | we did something a bit more sophisticated, like run an ordinary attoparsec 'Text' parser on |
58 | each line, as is frequently desirable. Here we run | 60 | each line, as is frequently desirable. Here we use |
59 | a minimal attoparsec number parser, @scientific@, on separate lines of standard input, | 61 | a minimal attoparsec number parser, @scientific@, on separate lines of standard input, |
60 | dropping bad parses with @P.concat@: | 62 | dropping bad parses with @P.concat@: |
61 | 63 | ||
62 | >>> import qualified Data.Attoparsec.Text as A | 64 | >>> import Data.Attoparsec.Text (parseOnly, scientific) |
63 | >>> P.toListM $ Text.stdinLn >-> P.map (A.parseOnly A.scientific) >-> P.concat >-> P.take 3 | 65 | >>> P.toListM $ Text.stdinLn >-> P.takeWhile (/= "quit") >-> P.map (parseOnly scientific) >-> P.concat |
64 | 1<Enter> | 66 | 1<Enter> |
65 | 2<Enter> | 67 | 2<Enter> |
66 | bad<Enter> | 68 | bad<Enter> |
67 | 3<Enter> | 69 | 3<Enter> |
70 | quit<Enter> | ||
68 | [1.0,2.0,3.0] | 71 | [1.0,2.0,3.0] |
69 | 72 | ||
70 | |||
71 | The line-based operations are, however, subject to a number of caveats. | 73 | The line-based operations are, however, subject to a number of caveats. |
72 | First, where they read from a handle, they will of course happily | 74 | First, where they read from a handle, they will of course happily |
73 | accumulate indefinitely long lines. This is likely to be legitimate for input | 75 | accumulate indefinitely long lines. This is likely to be legitimate for input |
74 | typed in by a user, and for locally produced log files and other known material, but | 76 | typed in by a user, and for locally produced files of known characteristics, but |
75 | otherwise not. See the post on | 77 | otherwise not. See the post on |
76 | <http://www.haskellforall.com/2013/09/perfect-streaming-using-pipes-bytestring.html perfect streaming> | 78 | <http://www.haskellforall.com/2013/09/perfect-streaming-using-pipes-bytestring.html perfect streaming> |
77 | to see why @pipes-bytestring@ and this package take a different approach. Furthermore, | 79 | to see why @pipes-bytestring@ and this package, outside this module, take a different approach. |
78 | like those in @Data.Text.IO@, the operations use the system encoding (and @T.hGetLine@) | 80 | Furthermore, the line-based operations, |
79 | and thus are slower than the \'official\' route, which would use bytestring IO and | 81 | like those in @Data.Text.IO@, use the system encoding (and @T.hGetLine@) |
80 | the encoding and decoding functions in @Pipes.Text.Encoding@. Finally, they will generate | 82 | and thus are slower than the \'official\' route, which would use the very fast |
81 | text exceptions after the fashion of @Data.Text.Encoding@ rather than returning the | 83 | bytestring IO operations from @Pipes.ByteString@ and |
82 | undigested bytes in the style of @Pipes.Text.Encoding@ | 84 | encoding and decoding functions in @Pipes.Text.Encoding@. Finally, the line-based |
85 | operations will generate text exceptions after the fashion of | ||
86 | @Data.Text.Encoding@, rather than returning the undigested bytes in the | ||
87 | style of @Pipes.Text.Encoding@. | ||
83 | 88 | ||
84 | -} | 89 | -} |
85 | 90 | ||