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