Safe Haskell | None |
---|---|
Language | Haskell2010 |
A data type of run-length-encoded lists.
This module is meant to be imported qualified with the exception of the type RLE itself. It exports names that clash with things in Prelude and many other data structure modules.
Synopsis
- data RLE a
- toList :: RLE a -> [a]
- fromList :: Eq a => [a] -> RLE a
- singleton :: a -> RLE a
- empty :: RLE a
- cons :: Eq a => a -> RLE a -> RLE a
- uncons :: Eq a => RLE a -> Maybe (a, RLE a)
- reverse :: RLE a -> RLE a
- splitAt :: (HasCallStack, Eq a) => Int -> RLE a -> (RLE a, RLE a)
- take :: Int -> RLE a -> RLE a
- init :: HasCallStack => RLE a -> RLE a
- null :: RLE a -> Bool
- length :: RLE a -> Int
- (++) :: Eq a => RLE a -> RLE a -> RLE a
- map :: Eq b => (a -> b) -> RLE a -> RLE b
- mapInvertible :: (a -> b) -> RLE a -> RLE b
- traverse :: (Eq b, Applicative f) => (a -> f b) -> RLE a -> f (RLE b)
- zipWith :: Eq c => (a -> b -> c) -> RLE a -> RLE b -> RLE c
- data Run a = Int :>< a
- toRuns :: RLE a -> [Run a]
- fromRuns :: Eq a => [Run a] -> RLE a
- consRun :: forall a. Eq a => Run a -> RLE a -> RLE a
- unconsRun :: RLE a -> Maybe (Run a, RLE a)
- runs :: (Contravariant f, Applicative f) => (Run a -> f (Run a)) -> RLE a -> f (RLE a)
Run-Length Encoded Lists
A run-length encoded representation of a [a]
.
This doesn't have a Functor
or Traversable
instance because it would
need an Eq
constraint on the element type to uphold invariants, but there
are map
and traverse
functions exported.
Instances
Foldable RLE Source # | |
Defined in Data.RLE fold :: Monoid m => RLE m -> m # foldMap :: Monoid m => (a -> m) -> RLE a -> m # foldMap' :: Monoid m => (a -> m) -> RLE a -> m # foldr :: (a -> b -> b) -> b -> RLE a -> b # foldr' :: (a -> b -> b) -> b -> RLE a -> b # foldl :: (b -> a -> b) -> b -> RLE a -> b # foldl' :: (b -> a -> b) -> b -> RLE a -> b # foldr1 :: (a -> a -> a) -> RLE a -> a # foldl1 :: (a -> a -> a) -> RLE a -> a # elem :: Eq a => a -> RLE a -> Bool # maximum :: Ord a => RLE a -> a # | |
Eq a => IsList (RLE a) Source # | |
Eq a => Eq (RLE a) Source # | |
Show a => Show (RLE a) Source # | |
a ~ Char => IsString (RLE a) Source # | |
Defined in Data.RLE fromString :: String -> RLE a # | |
Generic (RLE a) Source # | |
Eq a => Semigroup (RLE a) Source # | |
Eq a => Monoid (RLE a) Source # | |
Serialize a => Serialize (RLE a) Source # | |
NFData a => NFData (RLE a) Source # | |
Portray a => Portray (RLE a) Source # | |
(Portray a, Diff a) => Diff (RLE a) Source # | |
type Rep (RLE a) Source # | |
type Item (RLE a) Source # | |
fromList :: Eq a => [a] -> RLE a Source #
Run-length-encode a list by testing adjacent elements for equality.
uncons :: Eq a => RLE a -> Maybe (a, RLE a) Source #
Split the first element from the rest of the sequence.
splitAt :: (HasCallStack, Eq a) => Int -> RLE a -> (RLE a, RLE a) Source #
Returns a tuple where the first element contains the first n elements of the sequence, and the second element is the remainder of the sequence.
init :: HasCallStack => RLE a -> RLE a Source #
Return an RLE
containing all but the last element of the input.
map :: Eq b => (a -> b) -> RLE a -> RLE b Source #
Map the given function over each element of the sequence.
mapInvertible :: (a -> b) -> RLE a -> RLE b Source #
Map the given invertible function over each element of the sequence. This is only safe when the function is invertible.
This is slightly faster than map
and does not require an Eq constraint on
the result type.
traverse :: (Eq b, Applicative f) => (a -> f b) -> RLE a -> f (RLE b) Source #
Visit each element of the sequence in an Applicative
.
traverse :: Eq b => Traversal (RLE a) (RLE b) a b
Runs
n :>< x
denotes a sequence of n
copies of x
, as part of an RLE
.
Instances
Monad Run Source # | |
Functor Run Source # | |
Applicative Run Source # | After all, why not? This is basically Writer (Product Int). |
Foldable Run Source # | |
Defined in Data.RLE fold :: Monoid m => Run m -> m # foldMap :: Monoid m => (a -> m) -> Run a -> m # foldMap' :: Monoid m => (a -> m) -> Run a -> m # foldr :: (a -> b -> b) -> b -> Run a -> b # foldr' :: (a -> b -> b) -> b -> Run a -> b # foldl :: (b -> a -> b) -> b -> Run a -> b # foldl' :: (b -> a -> b) -> b -> Run a -> b # foldr1 :: (a -> a -> a) -> Run a -> a # foldl1 :: (a -> a -> a) -> Run a -> a # elem :: Eq a => a -> Run a -> Bool # maximum :: Ord a => Run a -> a # | |
Eq a => Eq (Run a) Source # | |
Show a => Show (Run a) Source # | |
Generic (Run a) Source # | |
Serialize a => Serialize (Run a) Source # | |
NFData a => NFData (Run a) Source # | |
Portray a => Portray (Run a) Source # | |
Diff a => Diff (Run a) Source # | |
type Rep (Run a) Source # | |
Defined in Data.RLE type Rep (Run a) = D1 ('MetaData "Run" "Data.RLE" "rle-0.1.0.1-IoLX9sRwIfiLS57snIrklB" 'False) (C1 ('MetaCons ":><" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))) |
consRun :: forall a. Eq a => Run a -> RLE a -> RLE a Source #
Add a run of equal elements onto the beginning of the sequence.
unconsRun :: RLE a -> Maybe (Run a, RLE a) Source #
Split the first run of equal elements from the rest of the sequence.
runs :: (Contravariant f, Applicative f) => (Run a -> f (Run a)) -> RLE a -> f (RLE a) Source #
Fold
over the contained runs in order.
This is as strong a type as this can have without breaking any laws, due to
the invariants that no empty or mergeable runs exist: if we make it a
Traversal
, it can end up changing the number of targets, and if we make it
an Iso
to [(Int, a)]
, the reverse direction is not an isomorphism.
If you want to use a law-breaking Iso
or Traversal
for this anyway, use
iso
to inline the problematic fromRuns
toRuns
Iso
.
runs :: Fold (RLE a) (Int, a)