Slist.Type

slist-0.2.1.0: Sized list

Copyright | (c) 2021-2022 Kowainik
License | MPL-2.0
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | None
Language | Haskell2010

Slist.Type

Contents

- Smart constructors
- Basic functions

Description

The main `Slist` data types and instances. Provides smart constructors and a few basic functions.

Synopsis

- data Slist a = Slist {
  - sList :: [a]
  - sSize :: Size
  }
- slist :: [a] -> Slist a
- infiniteSlist :: [a] -> Slist a
- one :: a -> Slist a
- len :: Slist a -> Int
- size :: Slist a -> Size
- isEmpty :: Slist a -> Bool
- cons :: a -> Slist a -> Slist a
- map :: (a -> b) -> Slist a -> Slist b

# Documentation

data Slist a #

Data type that represents sized list. Size can be both finite or infinite, it is established using `Size` data type.

Constructors

Slist |
Fields sList :: [a] sSize :: Size

#### Instances

Instances details

Alternative Slist # |
Instance details Defined in Slist.Type Methods empty :: Slist a # (<|>) :: Slist a -> Slist a -> Slist a # some :: Slist a -> Slist [a] # many :: Slist a -> Slist [a] #
Applicative Slist # |
Instance details Defined in Slist.Type Methods pure :: a -> Slist a # (<*>) :: Slist (a -> b) -> Slist a -> Slist b # liftA2 :: (a -> b -> c) -> Slist a -> Slist b -> Slist c # (*>) :: Slist a -> Slist b -> Slist b # (<*) :: Slist a -> Slist b -> Slist a #
Functor Slist # |
Instance details Defined in Slist.Type Methods fmap :: (a -> b) -> Slist a -> Slist b # (<$) :: a -> Slist b -> Slist a #
Monad Slist # |
Instance details Defined in Slist.Type Methods (>>=) :: Slist a -> (a -> Slist b) -> Slist b # (>>) :: Slist a -> Slist b -> Slist b # return :: a -> Slist a #
Foldable Slist # | Efficient implementation of `sum` and `product` functions. `length` returns `Int`s `maxBound` on infinite lists.
Instance details Defined in Slist.Type Methods fold :: Monoid m => Slist m -> m # foldMap :: Monoid m => (a -> m) -> Slist a -> m # foldMap' :: Monoid m => (a -> m) -> Slist a -> m # foldr :: (a -> b -> b) -> b -> Slist a -> b # foldr' :: (a -> b -> b) -> b -> Slist a -> b # foldl :: (b -> a -> b) -> b -> Slist a -> b # foldl' :: (b -> a -> b) -> b -> Slist a -> b # foldr1 :: (a -> a -> a) -> Slist a -> a # foldl1 :: (a -> a -> a) -> Slist a -> a # toList :: Slist a -> [a] # null :: Slist a -> Bool # length :: Slist a -> Int # elem :: Eq a => a -> Slist a -> Bool # maximum :: Ord a => Slist a -> a # minimum :: Ord a => Slist a -> a # sum :: Num a => Slist a -> a # product :: Num a => Slist a -> a #
Traversable Slist # |
Instance details Defined in Slist.Type Methods traverse :: Applicative f => (a -> f b) -> Slist a -> f (Slist b) # sequenceA :: Applicative f => Slist (f a) -> f (Slist a) # mapM :: Monad m => (a -> m b) -> Slist a -> m (Slist b) # sequence :: Monad m => Slist (m a) -> m (Slist a) #
Monoid (Slist a) # |
Instance details Defined in Slist.Type Methods mempty :: Slist a # mappend :: Slist a -> Slist a -> Slist a # mconcat :: [Slist a] -> Slist a #
Semigroup (Slist a) # | List appending. Use `<>` for `Slist` concatenation instead of `++` operator that is common in ordinary list concatenations.
Instance details Defined in Slist.Type Methods (<>) :: Slist a -> Slist a -> Slist a # sconcat :: NonEmpty (Slist a) -> Slist a # stimes :: Integral b => b -> Slist a -> Slist a #
IsList (Slist a) # |
Instance details Defined in Slist.Type Associated Types
type Item (Slist a) |
Instance details Defined in Slist.Type type Item (Slist a) = a
Methods fromList :: [Item (Slist a)] -> Slist a # fromListN :: Int -> [Item (Slist a)] -> Slist a # toList :: Slist a -> [Item (Slist a)] #
Read a => Read (Slist a) # |
Instance details Defined in Slist.Type Methods readsPrec :: Int -> ReadS (Slist a) # readList :: ReadS [Slist a] # readPrec :: ReadPrec (Slist a) # readListPrec :: ReadPrec [Slist a] #
Show a => Show (Slist a) # |
Instance details Defined in Slist.Type Methods showsPrec :: Int -> Slist a -> ShowS # show :: Slist a -> String # showList :: [Slist a] -> ShowS #
Eq a => Eq (Slist a) # | Equality of sized lists is checked more efficiently due to the fact that the check on the list sizes can be done first for the constant time.
Instance details Defined in Slist.Type Methods (==) :: Slist a -> Slist a -> Bool # (/=) :: Slist a -> Slist a -> Bool #
Ord a => Ord (Slist a) # | Lexicographical comparison of the lists.
Instance details Defined in Slist.Type Methods compare :: Slist a -> Slist a -> Ordering # (<) :: Slist a -> Slist a -> Bool # (<=) :: Slist a -> Slist a -> Bool # (>) :: Slist a -> Slist a -> Bool # (>=) :: Slist a -> Slist a -> Bool # max :: Slist a -> Slist a -> Slist a # min :: Slist a -> Slist a -> Slist a #
type Item (Slist a) # |
Instance details Defined in Slist.Type type Item (Slist a) = a

## Smart constructors

slist :: [a] -> Slist a #

`O(n)`. Constructs `Slist` from the given list.

    >>> slist [1..5]
    Slist {sList = [1,2,3,4,5], sSize = Size 5}

Note: works with finite lists. Use `infiniteSlist` to construct infinite lists.

infiniteSlist :: [a] -> Slist a #

`O(1)`. Constructs `Slist` from the given list.

    >> infiniteSlist [1..]
    Slist {sList = [1..], sSize = Infinity}

Note: works with infinite lists. Use `slist` to construct finite lists.

one :: a -> Slist a #

`O(1)`. Creates `Slist` with a single element. The size of such `Slist` is always equals to `Size 1`.

    >>> one "and only"
    Slist {sList = ["and only"], sSize = Size 1}

# Basic functions

len :: Slist a -> Int #

`O(1)`. Returns the length of a structure as an `Int`. On infinite lists returns the `Int`s `maxBound`.

    >>> len $ one 42
    1
    >>> len $ slist [1..3]
    3
    >>> len $ infiniteSlist [1..]
    9223372036854775807

size :: Slist a -> Size #

`O(1)`. Returns the `Size` of the slist.

    >>> size $ slist "Hello World!"
    Size 12
    >>> size $ infiniteSlist [1..]
    Infinity

isEmpty :: Slist a -> Bool #

`O(1)`. Checks if `Slist` is empty

    >>> isEmpty mempty
    True
    >>> isEmpty $ slist []
    True
    >>> isEmpty $ slist "Not Empty"
    False

cons :: a -> Slist a -> Slist a #

`O(1)`. `cons` is `Slist` analogue to `:` for lists. It adds the given element to the beginning of the list.

The following property is preserved:

      size (cons x xs) == size xs + 1

Examples:

    >>> cons 'a' $ one 'b'
    Slist {sList = "ab", sSize = Size 2}

    >> cons 0 $ infiniteSlist [1..]
    Slist {sList = [0..], sSize = Infinity}

map :: (a -> b) -> Slist a -> Slist b #

`O(n)`. Applies the given function to each element of the slist.

    map f (slist [x1, x2, ..., xn])     == slist [f x1, f x2, ..., f xn]
    map f (infiniteSlist [x1, x2, ...]) == infiniteSlist [f x1, f x2, ...]