on e856425 help sign in
92 lines 2.7 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.Data.Stream (Stream)
22import Streamly.Internal.Data.Array.Unboxed (Array)
24import qualified Streamly.Data.Array.Unboxed as A
25import qualified Streamly.Internal.Unicode.Stream as S
27import Prelude hiding (String, lines, words, unlines, unwords)
29-- $setup
30-- >>> :m
31-- >>> :set -XOverloadedStrings
32-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
33-- >>> import qualified Streamly.Prelude as Stream
34-- >>> import qualified Streamly.Internal.Unicode.Array.Char as Unicode
36-- | Break a string up into a stream of strings at newline characters.
37-- The resulting strings do not contain newlines.
38--
39-- > lines = S.lines A.write
40--
41-- >>> Stream.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
42-- ["lines","this","string","",""]
43--
44{-# INLINE lines #-}
45lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
46lines = S.lines A.write
48-- | Break a string up into a stream of strings, which were delimited
49-- by characters representing white space.
50--
51-- > words = S.words A.write
52--
53-- >>> Stream.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
54-- ["A","newline","is","considered","white","space?"]
55--
56{-# INLINE words #-}
57words :: MonadIO m => Stream m Char -> Stream m (Array Char)
58words = S.words A.write
60-- | Flattens the stream of @Array Char@, after appending a terminating
61-- newline to each string.
62--
63-- 'unlines' is an inverse operation to 'lines'.
64--
65-- >>> Stream.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
66-- "lines\nthis\nstring\n"
67--
68-- > unlines = S.unlines A.read
69--
70-- Note that, in general
71--
72-- > unlines . lines /= id
73{-# INLINE unlines #-}
74unlines :: MonadIO m => Stream m (Array Char) -> Stream m Char
75unlines = S.unlines A.reader
77-- | Flattens the stream of @Array Char@, after appending a separating
78-- space to each string.
79--
80-- 'unwords' is an inverse operation to 'words'.
81--
82-- >>> Stream.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
83-- "unwords this string"
84--
85-- > unwords = S.unwords A.read
86--
87-- Note that, in general
88--
89-- > unwords . words /= id
90{-# INLINE unwords #-}
91unwords :: MonadIO m => Stream m (Array Char) -> Stream m Char
92unwords = S.unwords A.reader