slist-0.2.0.0: Sized list
Copyright(c) 2021 Kowainik
LicenseMPL-2.0
MaintainerKowainik <xrom.xkov@gmail.com>
StabilityStable
PortabilityPortable
Safe HaskellNone
LanguageHaskell2010

Slist.Maybe

Description

Useful Maybe combinators to work with the Maybe data type and Slist together.

Since: 0.2.0.0

Synopsis

Documentation

maybeToSlist :: Maybe a -> Slist a Source #

Returns an empty list when given Nothing or a singleton list when given Just.

>>> maybeToSlist (Just 42)
Slist {sList = [42], sSize = Size 1}
>>> maybeToSlist Nothing
Slist {sList = [], sSize = Size 0}

Since: 0.2.0.0

slistToMaybe :: Slist a -> Maybe a Source #

Returns Nothing on an empty list or Just a where a is the first element of the slist.

Examples

Expand

Basic usage:

>>> slistToMaybe mempty
Nothing
>>> slistToMaybe (one 42)
Just 42
>>> slistToMaybe (cons 1 $ cons 2 $ one 3)
Just 1

Laws :

slistToMaybe . maybeToList ≡ id

Reverse is right only on singleton/empty lists

maybeToList . slistToMaybe {empty, singleton slist} ≡ {empty, singleton slist}

Since: 0.2.0.0

catMaybes :: Slist (Maybe a) -> Slist a Source #

Takes a slist of Maybes and returns a slist of all the Just values.

>>> catMaybes (cons (Just 1) $ cons Nothing $ one $ Just 3)
Slist {sList = [1,3], sSize = Size 2}

Since: 0.2.0.0

mapMaybe :: forall b a. (a -> Maybe b) -> Slist a -> Slist b Source #

The Maybe version of map which can throw out elements.

If appliying the given function returns Nothing, no element is added on to the result list. If it is Just b, then b is included in the result list.

>>> maybeEven x = if even x then Just x else Nothing
>>> s = cons 1 $ cons 2 $ one 3
>>> mapMaybe maybeEven s
Slist {sList = [2], sSize = Size 1}

If we map the Just constructor, the entire list should be returned:

>>> mapMaybe Just s
Slist {sList = [1,2,3], sSize = Size 3}

Since: 0.2.0.0

slistWith :: forall b a. (a -> Maybe b) -> [a] -> Slist b Source #

Similar to mapMaybe but works with the ordinary list as the input:

>>> maybeEven x = if even x then Just x else Nothing
>>> slistWith maybeEven [1,2,3]
Slist {sList = [2], sSize = Size 1}

Since: 0.2.0.0