2{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE PatternSynonyms #-}
5{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE UndecidableInstances #-} -- XXX
7{-# LANGUAGE ViewPatterns #-}
10-- Module : Streamly.List
11-- Copyright : (c) 2018 Composewell Technologies
14-- Maintainer : harendra.kumar@gmail.com
15-- Stability : experimental
18-- Lists are just a special case of monadic streams. The stream type @SerialT
19-- Identity a@ can be used as a replacement for @[a]@. The 'List' type in this
20-- module is just a newtype wrapper around @SerialT Identity@ for better type
21-- inference when using the 'OverloadedLists' GHC extension. @List a@ provides
22-- better performance compared to @[a]@. Standard list, string and list
23-- comprehension syntax can be used with the 'List' type by enabling
24-- 'OverloadedLists', 'OverloadedStrings' and 'MonadComprehensions' GHC
25-- extensions. There would be a slight difference in the 'Show' and 'Read'
26-- strings of streamly list as compared to regular lists.
28-- Conversion to stream types is free, any stream combinator can be used on
29-- lists by converting them to streams. However, for convenience, this module
30-- provides combinators that work directly on the 'List' type.
34-- List $ S.map (+ 1) $ toSerial (1 \`Cons\` Nil)
37-- To convert a 'List' to regular lists, you can use any of the following:
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"
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:
49-- pattern Nil <- [] where Nil = []
50-- pattern Cons x xs = x : xs
52-- {-\# COMPLETE Cons, Nil #-}
55-- See <src/docs/streamly-vs-lists.md> for more details and
56-- <src/test/PureStreams.hs> for comprehensive usage examples.
60#if __GLASGOW_HASKELL__ >= 800
67 -- XXX we may want to use rebindable syntax for variants instead of using
68 -- different types (applicative do and apWith).
75import Control.Arrow (second)
76import Control.DeepSeq (NFData(..))
77#if MIN_VERSION_deepseq(1,4,3)
78import Control.DeepSeq (NFData1(..))
80import Data.Functor.Identity (Identity, runIdentity)
81import Data.Semigroup (Semigroup(..))
82import GHC.Exts (IsList(..), IsString(..))
84import Streamly.Streams.Serial (SerialT)
85import Streamly.Streams.Zip (ZipSerialM)
87import qualified Streamly.Streams.Prelude as P
88import qualified Streamly.Streams.StreamK as K
90-- We implement list as a newtype instead of a type synonym to make type
91-- inference easier when using -XOverloadedLists and -XOverloadedStrings. When
92-- using a stream type the programmer needs to specify the Monad otherwise the
93-- type remains ambiguous.
95-- XXX once we separate consM from IsStream or remove the MonadIO and
96-- MonadBaseControlIO dependency from it, then we can make this an instance of
97-- IsStream and use the regular polymorphic functions on Lists as well. Once
98-- that happens we can change the Show and Read instances as well to use "1 >:
99-- 2 >: nil" etc. or should we use a separate constructor indicating the "List"
100-- type ":>" for better inference?
102-- | @List a@ is a replacement for @[a]@.
105newtype List a = List { toSerial :: SerialT Identity a }
106 deriving (Show, Read, Eq, Ord, NFData
107#if MIN_VERSION_deepseq(1,4,3)
110 , Semigroup, Monoid, Functor, Foldable
111 , Applicative, Traversable, Monad)
113instance (a ~ Char) => IsString (List a) where
114 {-# INLINE fromString #-}
115 fromString = List . P.fromList
117-- GHC versions 8.0 and below cannot derive IsList
118instance IsList (List a) where
119 type (Item (List a)) = a
120 {-# INLINE fromList #-}
121 fromList = List . P.fromList
122 {-# INLINE toList #-}
123 toList = runIdentity . P.toList . toSerial
125------------------------------------------------------------------------------
127------------------------------------------------------------------------------
129-- Note: When using the OverloadedLists extension we should be able to pattern
130-- match using the regular list contructors. OverloadedLists uses 'toList' to
131-- perform the pattern match, it should not be too bad as it works lazily in
132-- the Identity monad. We need these patterns only when not using that
135-- | An empty list constructor and pattern that matches an empty 'List'.
136-- Corresponds to '[]' for Haskell lists.
140pattern Nil <- (runIdentity . K.null . toSerial -> True) where
145-- | A list constructor and pattern that deconstructs a 'List' into its head
146-- and tail. Corresponds to ':' for Haskell lists.
149pattern Cons :: a -> List a -> List a
151 (fmap (second List) . runIdentity . K.uncons . toSerial
152 -> Just (x, xs)) where
153 Cons x xs = List $ K.cons x (toSerial xs)
155#if __GLASGOW_HASKELL__ >= 802
156{-# COMPLETE Nil, Cons #-}
159------------------------------------------------------------------------------
161------------------------------------------------------------------------------
163-- | Just like 'List' except that it has a zipping 'Applicative' instance
164-- and no 'Monad' instance.
167newtype ZipList a = ZipList { toZipSerial :: ZipSerialM Identity a }
168 deriving (Show, Read, Eq, Ord, NFData
169#if MIN_VERSION_deepseq(1,4,3)
172 , Semigroup, Monoid, Functor, Foldable
173 , Applicative, Traversable)
175instance (a ~ Char) => IsString (ZipList a) where
176 {-# INLINE fromString #-}
177 fromString = ZipList . P.fromList
179-- GHC versions 8.0 and below cannot derive IsList
180instance IsList (ZipList a) where
181 type (Item (ZipList a)) = a
182 {-# INLINE fromList #-}
183 fromList = ZipList . P.fromList
184 {-# INLINE toList #-}
185 toList = runIdentity . P.toList . toZipSerial
187-- | Convert a 'ZipList' to a regular 'List'
190fromZipList :: ZipList a -> List a
191fromZipList = List . K.adapt . toZipSerial
193-- | Convert a regular 'List' to a 'ZipList'
196toZipList :: List a -> ZipList a
197toZipList = ZipList . K.adapt . toSerial
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list