on 40e89fb help sign in
104 lines 3.2 kB history raw
1-- |
2-- Module : Streamly.Internal.Unicode.Array
3-- Copyright : (c) 2018 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
10-- XXX Remove this or move this to Unicode.Utf32 making "Array Char" as a
11-- newtype wrapper for Utf32. Is this any better than the [Char] (String) type?
12-- This provides random access and the length of the string in O(1). Also,
13-- better append performance.
14--
15module Streamly.Internal.Unicode.Array
16 (
17 -- * Setup
18 -- | To execute the code examples provided in this module in ghci, please
19 -- run the following commands first.
20 --
21 -- $setup
23 -- * Streams of Strings
24 lines
25 , words
26 , unlines
27 , unwords
28 )
29where
31import Control.Monad.IO.Class (MonadIO)
32import Streamly.Data.Stream (Stream)
33import Streamly.Internal.Data.Array (Array)
35import qualified Streamly.Data.Array as A
36import qualified Streamly.Internal.Unicode.Stream as S
38import Prelude hiding (String, lines, words, unlines, unwords)
40-- $setup
41-- >>> :m
42-- >>> :set -XOverloadedStrings
43-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
44-- >>> import qualified Streamly.Data.Stream as Stream
45-- >>> import qualified Streamly.Data.Fold as Fold
46-- >>> import qualified Streamly.Internal.Unicode.Array as Unicode
48-- | Break a string up into a stream of strings at newline characters.
49-- The resulting strings do not contain newlines.
50--
51-- > lines = S.lines A.write
52--
53-- >>> Stream.fold Fold.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
54-- [fromList "lines",fromList "this",fromList "string",fromList "",fromList ""]
55--
56{-# INLINE lines #-}
57lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
58lines = S.lines A.write
60-- | Break a string up into a stream of strings, which were delimited
61-- by characters representing white space.
62--
63-- > words = S.words A.write
64--
65-- >>> Stream.fold Fold.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
66-- [fromList "A",fromList "newline",fromList "is",fromList "considered",fromList "white",fromList "space?"]
67--
68{-# INLINE words #-}
69words :: MonadIO m => Stream m Char -> Stream m (Array Char)
70words = S.words A.write
72-- | Flattens the stream of @Array Char@, after appending a terminating
73-- newline to each string.
74--
75-- 'unlines' is an inverse operation to 'lines'.
76--
77-- >>> Stream.fold Fold.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
78-- "lines\nthis\nstring\n"
79--
80-- > unlines = S.unlines A.read
81--
82-- Note that, in general
83--
84-- > unlines . lines /= id
85{-# INLINE unlines #-}
86unlines :: MonadIO m => Stream m (Array Char) -> Stream m Char
87unlines = S.unlines A.reader
89-- | Flattens the stream of @Array Char@, after appending a separating
90-- space to each string.
91--
92-- 'unwords' is an inverse operation to 'words'.
93--
94-- >>> Stream.fold Fold.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
95-- "unwords this string"
96--
97-- > unwords = S.unwords A.read
98--
99-- Note that, in general
100--
101-- > unwords . words /= id
102{-# INLINE unwords #-}
103unwords :: MonadIO m => Stream m (Array Char) -> Stream m Char
104unwords = S.unwords A.reader