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