on a05abfc help sign in
91 lines 2.8 kB history raw
1-- |
2-- Module : Streamly.Internal.Unicode.Array.Prim.Pinned
3-- Copyright : (c) 2020 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.Prim.Pinned
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.Internal.Data.Stream.IsStream (IsStream)
24import Streamly.Internal.Data.Array.Prim.Pinned (Array)
26import qualified Streamly.Internal.Unicode.Stream as S
27import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
29-- $setup
30-- >>> :m
31-- >>> :set -XOverloadedStrings
32-- >>> import qualified Streamly.Prelude as Stream
33-- >>> import qualified Streamly.Internal.Unicode.Array.Prim.Pinned as Unicode
35-- | Break a string up into a stream of strings at newline characters.
36-- The resulting strings do not contain newlines.
37--
38-- > lines = S.lines A.write
39--
40-- >>> Stream.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
41-- [fromListN 5 "lines",fromListN 4 "this",fromListN 6 "string",fromListN 0 "",fromListN 0 ""]
42--
43{-# INLINE lines #-}
44lines :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
45lines = S.lines A.write
47-- | Break a string up into a stream of strings, which were delimited
48-- by characters representing white space.
49--
50-- > words = S.words A.write
51--
52-- >>> Stream.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
53-- [fromListN 1 "A",fromListN 7 "newline",fromListN 2 "is",fromListN 10 "considered",fromListN 5 "white",fromListN 6 "space?"]
54--
55{-# INLINE words #-}
56words :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
57words = S.words A.write
59-- | Flattens the stream of @Array Char@, after appending a terminating
60-- newline to each string.
61--
62-- 'unlines' is an inverse operation to 'lines'.
63--
64-- >>> Stream.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
65-- "lines\nthis\nstring\n"
66--
67-- > unlines = S.unlines A.read
68--
69-- Note that, in general
70--
71-- > unlines . lines /= id
72{-# INLINE unlines #-}
73unlines :: (MonadAsync m, IsStream t) => t m (Array Char) -> t m Char
74unlines = S.unlines A.read
76-- | Flattens the stream of @Array Char@, after appending a separating
77-- space to each string.
78--
79-- 'unwords' is an inverse operation to 'words'.
80--
81-- >>> Stream.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
82-- "unwords this string"
83--
84-- > unwords = S.unwords A.read
85--
86-- Note that, in general
87--
88-- > unwords . words /= id
89{-# INLINE unwords #-}
90unwords :: (MonadAsync m, IsStream t) => t m (Array Char) -> t m Char
91unwords = S.unwords A.read