on de680fb help sign in
85 lines 2.4 kB history raw
1-- |
2-- Module : Streamly.Internal.Unicode.Array.Char
3-- Copyright : (c) 2018 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
9--
10module Streamly.Internal.Unicode.Array.Char
11 (
12 -- * Streams of Strings
13 lines
14 , words
15 , unlines
16 , unwords
17 )
18where
20import Control.Monad.IO.Class (MonadIO)
21import Streamly.Prelude (MonadAsync)
22import Prelude hiding (String, lines, words, unlines, unwords)
23import Streamly.Data.Array.Storable.Foreign (Array)
24import Streamly.Internal.Data.Stream.IsStream (IsStream)
26import qualified Streamly.Internal.Unicode.Stream as S
27import qualified Streamly.Data.Array.Storable.Foreign 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