on 0780dbb help sign in
93 lines 2.7 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.Data.Array.Unboxed (Array)
22import Streamly.Internal.Data.Stream.IsStream (IsStream)
23import Streamly.Prelude (MonadAsync)
25import qualified Streamly.Data.Array.Unboxed as A
26import qualified Streamly.Internal.Unicode.Stream as S
28import Prelude hiding (String, lines, words, unlines, unwords)
30-- $setup
31-- >>> :m
32-- >>> :set -XOverloadedStrings
33-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
34-- >>> import qualified Streamly.Prelude as Stream
35-- >>> import qualified Streamly.Internal.Unicode.Array.Char as Unicode
37-- | Break a string up into a stream of strings at newline characters.
38-- The resulting strings do not contain newlines.
39--
40-- > lines = S.lines A.write
41--
42-- >>> Stream.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
43-- ["lines","this","string","",""]
44--
45{-# INLINE lines #-}
46lines :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
47lines = S.lines A.write
49-- | Break a string up into a stream of strings, which were delimited
50-- by characters representing white space.
51--
52-- > words = S.words A.write
53--
54-- >>> Stream.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
55-- ["A","newline","is","considered","white","space?"]
56--
57{-# INLINE words #-}
58words :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
59words = S.words A.write
61-- | Flattens the stream of @Array Char@, after appending a terminating
62-- newline to each string.
63--
64-- 'unlines' is an inverse operation to 'lines'.
65--
66-- >>> Stream.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
67-- "lines\nthis\nstring\n"
68--
69-- > unlines = S.unlines A.read
70--
71-- Note that, in general
72--
73-- > unlines . lines /= id
74{-# INLINE unlines #-}
75unlines :: (MonadIO m, IsStream t) => t m (Array Char) -> t m Char
76unlines = S.unlines A.read
78-- | Flattens the stream of @Array Char@, after appending a separating
79-- space to each string.
80--
81-- 'unwords' is an inverse operation to 'words'.
82--
83-- >>> Stream.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
84-- "unwords this string"
85--
86-- > unwords = S.unwords A.read
87--
88-- Note that, in general
89--
90-- > unwords . words /= id
91{-# INLINE unwords #-}
92unwords :: (MonadAsync m, IsStream t) => t m (Array Char) -> t m Char
93unwords = S.unwords A.read