on 63407dc help sign in
98 lines 3.1 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 -- * Streams of Strings
18 lines
19 , words
20 , unlines
21 , unwords
22 )
23where
25import Control.Monad.IO.Class (MonadIO)
26import Streamly.Data.Stream (Stream)
27import Streamly.Internal.Data.Array (Array)
29import qualified Streamly.Data.Array as A
30import qualified Streamly.Internal.Unicode.Stream as S
32import Prelude hiding (String, lines, words, unlines, unwords)
34-- $setup
35-- >>> :m
36-- >>> :set -XOverloadedStrings
37-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
38-- >>> import qualified Streamly.Data.Stream as Stream
39-- >>> import qualified Streamly.Data.Fold as Fold
40-- >>> import qualified Streamly.Internal.Unicode.Array as Unicode
42-- | Break a string up into a stream of strings at newline characters.
43-- The resulting strings do not contain newlines.
44--
45-- > lines = S.lines A.write
46--
47-- >>> Stream.fold Fold.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
48-- [fromList "lines",fromList "this",fromList "string",fromList "",fromList ""]
49--
50{-# INLINE lines #-}
51lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
52lines = S.lines A.write
54-- | Break a string up into a stream of strings, which were delimited
55-- by characters representing white space.
56--
57-- > words = S.words A.write
58--
59-- >>> Stream.fold Fold.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
60-- [fromList "A",fromList "newline",fromList "is",fromList "considered",fromList "white",fromList "space?"]
61--
62{-# INLINE words #-}
63words :: MonadIO m => Stream m Char -> Stream m (Array Char)
64words = S.words A.write
66-- | Flattens the stream of @Array Char@, after appending a terminating
67-- newline to each string.
68--
69-- 'unlines' is an inverse operation to 'lines'.
70--
71-- >>> Stream.fold Fold.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
72-- "lines\nthis\nstring\n"
73--
74-- > unlines = S.unlines A.read
75--
76-- Note that, in general
77--
78-- > unlines . lines /= id
79{-# INLINE unlines #-}
80unlines :: MonadIO m => Stream m (Array Char) -> Stream m Char
81unlines = S.unlines A.reader
83-- | Flattens the stream of @Array Char@, after appending a separating
84-- space to each string.
85--
86-- 'unwords' is an inverse operation to 'words'.
87--
88-- >>> Stream.fold Fold.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
89-- "unwords this string"
90--
91-- > unwords = S.unwords A.read
92--
93-- Note that, in general
94--
95-- > unwords . words /= id
96{-# INLINE unwords #-}
97unwords :: MonadIO m => Stream m (Array Char) -> Stream m Char
98unwords = S.unwords A.reader