Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
Reimagined approach for Foldable
type hierarchy. Forbids usages
of length
function and similar over Maybe
and other potentially unsafe
data types. It was proposed to use -XTypeApplication
for such cases.
But this approach is not robust enough because programmers are human and can
easily forget to do this. For discussion see this topic:
Suggest explicit type application for Foldable length and friends
- type family Element t
- class Container t where
- class Container t => NontrivialContainer t where
- sum :: (NontrivialContainer t, Num (Element t)) => t -> Element t
- product :: (NontrivialContainer t, Num (Element t)) => t -> Element t
- mapM_ :: (NontrivialContainer t, Monad m) => (Element t -> m b) -> t -> m ()
- forM_ :: (NontrivialContainer t, Monad m) => t -> (Element t -> m b) -> m ()
- traverse_ :: (NontrivialContainer t, Applicative f) => (Element t -> f b) -> t -> f ()
- for_ :: (NontrivialContainer t, Applicative f) => t -> (Element t -> f b) -> f ()
- sequenceA_ :: (NontrivialContainer t, Applicative f, Element t ~ f a) => t -> f ()
- sequence_ :: (NontrivialContainer t, Monad m, Element t ~ m a) => t -> m ()
- asum :: (NontrivialContainer t, Alternative f, Element t ~ f a) => t -> f a
- class One x where
- type OneItem x
Foldable-like classes and methods
class Container t where Source #
Type class for container. Fully compatible with Foldable
.
Contains very small and safe subset of Foldable
functions.
toList :: t -> [Element t] Source #
Convert container to list of elements.
>>>
toList (Just True)
[True]>>>
toList @Text "aba"
"aba">>>
:t toList @Text "aba"
toList @Text "aba" :: [Char]
Checks whether container is empty.
>>>
null @Text ""
True>>>
null @Text "aba"
False
Container ByteString Source # | |
Container ByteString Source # | |
Container IntSet Source # | |
Container Text Source # | |
Container Text Source # | |
Foldable f => Container (f a) Source # | This instance makes |
TypeError Constraint ((:$$:) ((:<>:) (Text "Do not use 'Foldable' methods on ") (Text "tuples")) (Text "NB. If you tried to use 'for_' on Maybe or Either, use 'whenJust' or 'whenRight' instead")) => Container (a, b) Source # | |
class Container t => NontrivialContainer t where Source #
foldMap :: Monoid m => (Element t -> m) -> t -> m Source #
fold :: Monoid (Element t) => t -> Element t Source #
foldr :: (Element t -> b -> b) -> b -> t -> b Source #
foldr' :: (Element t -> b -> b) -> b -> t -> b Source #
foldl :: (b -> Element t -> b) -> b -> t -> b Source #
foldl' :: (b -> Element t -> b) -> b -> t -> b Source #
foldr1 :: (Element t -> Element t -> Element t) -> t -> Element t Source #
foldl1 :: (Element t -> Element t -> Element t) -> t -> Element t Source #
elem :: Eq (Element t) => Element t -> t -> Bool Source #
maximum :: Ord (Element t) => t -> Element t Source #
minimum :: Ord (Element t) => t -> Element t Source #
all :: (Element t -> Bool) -> t -> Bool Source #
any :: (Element t -> Bool) -> t -> Bool Source #
and :: Element t ~ Bool => t -> Bool Source #
or :: Element t ~ Bool => t -> Bool Source #
find :: (Element t -> Bool) -> t -> Maybe (Element t) Source #
NontrivialContainer ByteString Source # | |
NontrivialContainer ByteString Source # | |
NontrivialContainer IntSet Source # | |
NontrivialContainer Text Source # | |
NontrivialContainer Text Source # | |
TypeError Constraint ((:$$:) ((:<>:) (Text "Do not use 'Foldable' methods on ") (Text "Maybe")) (Text "NB. If you tried to use 'for_' on Maybe or Either, use 'whenJust' or 'whenRight' instead")) => NontrivialContainer (Maybe a) Source # | |
Foldable f => NontrivialContainer (f a) Source # | |
TypeError Constraint ((:$$:) ((:<>:) (Text "Do not use 'Foldable' methods on ") (Text "Identity")) (Text "NB. If you tried to use 'for_' on Maybe or Either, use 'whenJust' or 'whenRight' instead")) => NontrivialContainer (Identity a) Source # | |
TypeError Constraint ((:$$:) ((:<>:) (Text "Do not use 'Foldable' methods on ") (Text "Either")) (Text "NB. If you tried to use 'for_' on Maybe or Either, use 'whenJust' or 'whenRight' instead")) => NontrivialContainer (Either a b) Source # | |
TypeError Constraint ((:$$:) ((:<>:) (Text "Do not use 'Foldable' methods on ") (Text "tuples")) (Text "NB. If you tried to use 'for_' on Maybe or Either, use 'whenJust' or 'whenRight' instead")) => NontrivialContainer (a, b) Source # | |
sum :: (NontrivialContainer t, Num (Element t)) => t -> Element t Source #
Stricter version of sum
.
>>>
sum [1..10]
55>>>
sum (Just 3)
<interactive>:43:1: error: • Do not use 'Foldable' methods on Maybe • In the expression: sum (Just 3) In an equation for ‘it’: it = sum (Just 3)
product :: (NontrivialContainer t, Num (Element t)) => t -> Element t Source #
Stricter version of product
.
>>>
product [1..10]
3628800>>>
product (Right 3)
<interactive>:45:1: error: • Do not use 'Foldable' methods on Either • In the expression: product (Right 3) In an equation for ‘it’: it = product (Right 3)
mapM_ :: (NontrivialContainer t, Monad m) => (Element t -> m b) -> t -> m () Source #
Constrained to NonTrivialContainer
version of mapM_
.
forM_ :: (NontrivialContainer t, Monad m) => t -> (Element t -> m b) -> m () Source #
Constrained to NonTrivialContainer
version of forM_
.
traverse_ :: (NontrivialContainer t, Applicative f) => (Element t -> f b) -> t -> f () Source #
Constrained to NonTrivialContainer
version of traverse_
.
for_ :: (NontrivialContainer t, Applicative f) => t -> (Element t -> f b) -> f () Source #
Constrained to NonTrivialContainer
version of for_
.
sequenceA_ :: (NontrivialContainer t, Applicative f, Element t ~ f a) => t -> f () Source #
Constrained to NonTrivialContainer
version of sequenceA_
.
sequence_ :: (NontrivialContainer t, Monad m, Element t ~ m a) => t -> m () Source #
Constrained to NonTrivialContainer
version of sequence_
.
asum :: (NontrivialContainer t, Alternative f, Element t ~ f a) => t -> f a Source #
Constrained to NonTrivialContainer
version of asum
.
Others
Type class for types that can be created from one element. singleton
is lone name for this function. Also constructions of different type differ:
:[]
for lists, two arguments for Maps. Also some data types are monomorphic.
>>>
one True :: [Bool]
[True]>>>
one 'a' :: Text
"a">>>
one (3, "hello") :: HashMap Int String
fromList [(3,"hello")]
One ByteString Source # | |
One ByteString Source # | |
One IntSet Source # | |
One Text Source # | |
One Text Source # | |
One [a] Source # | |
One (NonEmpty a) Source # | |
One (IntMap v) Source # | |
One (Seq a) Source # | |
One (Set v) Source # | |
Hashable v => One (HashSet v) Source # | |
One (Vector a) Source # | |
Storable a => One (Vector a) Source # | |
Unbox a => One (Vector a) Source # | |
Prim a => One (Vector a) Source # | |
One (Map k v) Source # | |
Hashable k => One (HashMap k v) Source # | |