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