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