@@ -22,19 +22,8 @@
2222-- better performance compared to @[a]@. Standard list, string and list
2323-- comprehension syntax can be used with the 'List' type by enabling
2424-- 'OverloadedLists', 'OverloadedStrings' and 'MonadComprehensions' GHC
25−-- extensions. If you want to replace streamly lists with standard lists, all
26−-- you need to do is import these definitions:
27−--
28−-- @
29−-- type List = []
30−-- pattern Nil <- [] where Nil = []
31−-- pattern Cons x xs = x : xs
32−-- infixr 5 `Cons`
33−-- {-\# COMPLETE Cons, Nil #-}
34−-- @
35−--
36−-- Other than that there would be a slight difference in the 'Show' and
37−-- 'Read' strings.
25+-- extensions. There would be a slight difference in the 'Show' and 'Read'
26+-- strings of streamly list as compared to regular lists.
3827--
3928-- Conversion to stream types is free, any stream combinator can be used on
4029-- lists by converting them to streams. However, for convenience, this module
@@ -45,9 +34,23 @@
4534-- List $ S.map (+ 1) $ toSerial (1 \`Cons\` Nil)
4635-- @
4736--
48−-- 'Data.Foldable.toList' from 'Foldable' and 'GHC.Exts.toList' and
49−-- 'GHC.Exts.fromList' from 'IsList' can also be used to convert to and from
50−-- standard lists.
37+-- To convert a 'List' to regular lists, you can use any of the following:
38+--
39+-- * @toList . toSerial@ and @toSerial . fromList@
40+-- * 'Data.Foldable.toList' from "Data.Foldable"
41+-- * 'GHC.Exts.toList' and 'GHC.Exts.fromList' from 'IsList' in "GHC.Exts"
42+--
43+-- If you have made use of 'Nil' and 'Cons' constructors in the code and you
44+-- want to replace streamly lists with standard lists, all you need to do is
45+-- import these definitions:
46+--
47+-- @
48+-- type List = []
49+-- pattern Nil <- [] where Nil = []
50+-- pattern Cons x xs = x : xs
51+-- infixr 5 `Cons`
52+-- {-\# COMPLETE Cons, Nil #-}
53+-- @
5154--
5255-- See <src/docs/streamly-vs-lists.md> for more details and
5356-- <src/test/PureStreams.hs> for comprehensive usage examples.
@@ -61,6 +64,8 @@ module Streamly.List
6164 , pattern Nil
6265 , pattern Cons
6366#endif
67+ -- XXX we may want to use rebindable syntax for variants instead of using
68+ -- different types (applicative do and apWith).
6469 , ZipList (..)
6570 , fromZipList
6671 , toZipList
@@ -84,9 +89,16 @@ import qualified Streamly.Streams.StreamK as K
8489-- using a stream type the programmer needs to specify the Monad otherwise the
8590-- type remains ambiguous.
8691--
92+-- XXX once we separate consM from IsStream or remove the MonadIO and
93+-- MonadBaseControlIO dependency from it, then we can make this an instance of
94+-- IsStream and use the regular polymorphic functions on Lists as well. Once
95+-- that happens we can change the Show and Read instances as well to use "1 >:
96+-- 2 >: nil" etc. or should we use a separate constructor indicating the "List"
97+-- type ":>" for better inference?
98+--
8799-- | @List a@ is a replacement for @[a]@.
88100--
89−-- @since 0.5.3
101+-- @since 0.6.0
90102newtype List a = List { toSerial :: SerialT Identity a }
91103 deriving (Show, Read, Eq, Ord, NFData, NFData1
92104 , Semigroup, Monoid, Functor, Foldable
@@ -108,10 +120,16 @@ instance IsList (List a) where
108120-- Patterns
109121------------------------------------------------------------------------------
110122
123+-- Note: When using the OverloadedLists extension we should be able to pattern
124+-- match using the regular list contructors. OverloadedLists uses 'toList' to
125+-- perform the pattern match, it should not be too bad as it works lazily in
126+-- the Identity monad. We need these patterns only when not using that
127+-- extension.
128+--
111129-- | An empty list constructor and pattern that matches an empty 'List'.
112130-- Corresponds to '[]' for Haskell lists.
113131--
114−-- @since 0.5.3
132+-- @since 0.6.0
115133pattern Nil :: List a
116134pattern Nil <- (runIdentity . K.null . toSerial -> True) where
117135 Nil = List K.nil
@@ -121,7 +139,7 @@ infixr 5 `Cons`
121139-- | A list constructor and pattern that deconstructs a 'List' into its head
122140-- and tail. Corresponds to ':' for Haskell lists.
123141--
124−-- @since 0.5.3
142+-- @since 0.6.0
125143pattern Cons :: a -> List a -> List a
126144pattern Cons x xs <-
127145 (fmap (second List) . runIdentity . K.uncons . toSerial
@@ -139,7 +157,7 @@ pattern Cons x xs <-
139157-- | Just like 'List' except that it has a zipping 'Applicative' instance
140158-- and no 'Monad' instance.
141159--
142−-- @since 0.5.3
160+-- @since 0.6.0
143161newtype ZipList a = ZipList { toZipSerial :: ZipSerialM Identity a }
144162 deriving (Show, Read, Eq, Ord, NFData, NFData1
145163 , Semigroup, Monoid, Functor, Foldable
@@ -159,12 +177,12 @@ instance IsList (ZipList a) where
159177
160178-- | Convert a 'ZipList' to a regular 'List'
161179--
162−-- @since 0.5.3
180+-- @since 0.6.0
163181fromZipList :: ZipList a -> List a
164182fromZipList = List . K.adapt . toZipSerial
165183
166184-- | Convert a regular 'List' to a 'ZipList'
167185--
168−-- @since 0.5.3
186+-- @since 0.6.0
169187toZipList :: List a -> ZipList a
170188toZipList = ZipList . K.adapt . toSerial