Safe Haskell | None |
---|---|
Language | Haskell2010 |
Useful for describing zippers and functional queues/buffers more naturally and safely.
We call it an RList
because this is really just a vanilla list, but where
the semantics are that the last-added thing (internally cons'ed) is
understood to be at the "end" of the list.
WARNING: the Foldable
instance provides a toList
; this simply unwraps the RList
rather than reversing it.
If you need to convert from revered semantics to forward semantics, use this module's toList
.
Synopsis
- data RList a
- type Tsil = RList
- nil :: RList a
- snoc :: RList a -> a -> RList a
- singleton :: a -> RList a
- unsnoc :: RList a -> Maybe (RList a, a)
- pattern Nil :: RList a
- pattern Snoc :: RList a -> a -> RList a
- null :: RList a -> Bool
- init :: RList a -> Maybe (RList a)
- last :: RList a -> Maybe a
- catMaybes :: RList (Maybe a) -> RList a
- toList :: RList a -> [a]
- fromList :: [a] -> RList a
- reverseIn :: [a] -> RList a
- reverseOut :: RList a -> [a]
- toArrayN :: (Contiguous arr, Element arr a) => Int -> RList a -> arr a
- toSet :: Ord a => RList a -> Set a
Documentation
This datatype defines snoc-lists: lists with O(1) append and O(n) prepend. Underneath the hood, it is just a plain list, but understood as containing its elements in reverse order.
Instances
Functor RList Source # | |
Applicative RList Source # | |
Alternative RList Source # | |
Read a => Read (RList a) Source # | |
Show a => Show (RList a) Source # | |
Generic (RList a) Source # | |
Semigroup (RList a) Source # | |
Monoid (RList a) Source # | |
NFData a => NFData (RList a) Source # | |
Defined in Data.List.Snoc | |
type Rep (RList a) Source # | |
Defined in Data.List.Snoc |
See? It's "List" in reverse?
I dunno, I just think RList
is an inelegant name, and word-initial t͜s
is one of my favorite phonemes.
Introduction and Elimination
snoc :: RList a -> a -> RList a Source #
O(1)
Append an element.
If you are looking for cons
, you should use a plain list, or a finite sequence/queue type.
Patterns
Queries
Traversal
Conversion
toList :: RList a -> [a] Source #
O(n)
Convert to a plain list, maintaining order.
This is here so that you can escape back out to normal cons-list land once you're done building your list.
See reverseOut
for when order doesn't matter.
reverseIn :: [a] -> RList a Source #
O(0)
Reverse a plain cons list, rerutning an RList
.
See reverseOut
for the inverse, and why you might use these.
reverseOut :: RList a -> [a] Source #
toArrayN :: (Contiguous arr, Element arr a) => Int -> RList a -> arr a Source #
Write the contents of the RList
into an array, assuming you know the length of the array.
This is useful in the common case of buffering an unknown-length stream before allocating contiguous space for the elements.
If you sepcify to small a langth, the initial part of the array will be uninitialized. If you specify to large a length, the initial part of the list will not be written.
If you are unaware of the size of the list, `Arr.fromList . fromList` will do the trick, but will obviously be slower.