on e3e60ca help sign in
94 lines 2.8 kB history raw
1-- |
2-- Module : Streamly.Internal.Unicode.Array.Char
3-- Copyright : (c) 2018 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
9--
10module Streamly.Internal.Unicode.Array.Char
11 (
12 -- * Streams of Strings
13 lines
14 , words
15 , unlines
16 , unwords
17 )
18where
20import Control.Monad.IO.Class (MonadIO)
21import Streamly.Prelude (MonadAsync)
22import Prelude hiding (String, lines, words, unlines, unwords)
23import Streamly.Internal.Data.Array.Unboxed (Array)
24import Streamly.Internal.Data.Stream.IsStream (IsStream)
26import qualified Streamly.Internal.Data.Array.Unboxed as A
27 (write, read)
29import qualified Streamly.Internal.Unicode.Stream as S
31-- $setup
32-- >>> :m
33-- >>> :set -XOverloadedStrings
34-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
35-- >>> import qualified Streamly.Prelude as Stream
36-- >>> import qualified Streamly.Internal.Unicode.Array.Char as Unicode
38-- | Break a string up into a stream of strings at newline characters.
39-- The resulting strings do not contain newlines.
40--
41-- > lines = S.lines A.write
42--
43-- >>> Stream.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
44-- ["lines","this","string","",""]
45--
46{-# INLINE lines #-}
47lines :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
48lines = S.lines A.write
50-- | Break a string up into a stream of strings, which were delimited
51-- by characters representing white space.
52--
53-- > words = S.words A.write
54--
55-- >>> Stream.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
56-- ["A","newline","is","considered","white","space?"]
57--
58{-# INLINE words #-}
59words :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
60words = S.words A.write
62-- | Flattens the stream of @Array Char@, after appending a terminating
63-- newline to each string.
64--
65-- 'unlines' is an inverse operation to 'lines'.
66--
67-- >>> Stream.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
68-- "lines\nthis\nstring\n"
69--
70-- > unlines = S.unlines A.read
71--
72-- Note that, in general
73--
74-- > unlines . lines /= id
75{-# INLINE unlines #-}
76unlines :: (MonadIO m, IsStream t) => t m (Array Char) -> t m Char
77unlines = S.unlines A.read
79-- | Flattens the stream of @Array Char@, after appending a separating
80-- space to each string.
81--
82-- 'unwords' is an inverse operation to 'words'.
83--
84-- >>> Stream.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
85-- "unwords this string"
86--
87-- > unwords = S.unwords A.read
88--
89-- Note that, in general
90--
91-- > unwords . words /= id
92{-# INLINE unwords #-}
93unwords :: (MonadAsync m, IsStream t) => t m (Array Char) -> t m Char
94unwords = S.unwords A.read