Copyright | (c) Carl Howells 2021-2024 |
---|---|
License | MIT |
Maintainer | chowells79@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- withered :: (Applicative f, Witherable t) => (a -> Withering f b) -> t a -> f (t b)
- unwithered :: Functor f => (a -> f b) -> a -> Withering f b
- rewithered :: (Applicative f, Witherable t) => (a -> Withering f b) -> t a -> Withering f (t b)
- decayed :: Applicative f => pafb -> s -> Withering f t
- guarded :: Applicative f => (a -> Bool) -> (a -> Withering f b) -> a -> Withering f b
- witherOf :: ((a -> Withering f b) -> s -> f t) -> (a -> f (Maybe b)) -> s -> f t
- forMaybeOf :: ((a -> Withering f b) -> s -> f t) -> s -> (a -> f (Maybe b)) -> f t
- mapMaybeOf :: ((a -> Withering Identity b) -> s -> Identity t) -> (a -> Maybe b) -> s -> t
- catMaybesOf :: ((Maybe a -> Withering Identity a) -> s -> Identity t) -> s -> t
- filterAOf :: Applicative f => ((a -> Withering f a) -> s -> f s) -> (a -> f Bool) -> s -> f s
- filterOf :: ((a -> Withering Identity a) -> s -> Identity s) -> (a -> Bool) -> s -> s
- ordNubOf :: Ord a => ((a -> Withering (State (Set a)) a) -> s -> State (Set a) s) -> s -> s
- ordNubOnOf :: Ord b => ((a -> Withering (State (Set b)) a) -> s -> State (Set b) s) -> (a -> b) -> s -> s
- hashNubOf :: (Eq a, Hashable a) => ((a -> Withering (State (HashSet a)) a) -> s -> State (HashSet a) s) -> s -> s
- hashNubOnOf :: (Eq b, Hashable b) => ((a -> Withering (State (HashSet b)) a) -> s -> State (HashSet b) s) -> (a -> b) -> s -> s
Basic API
withered :: (Applicative f, Witherable t) => (a -> Withering f b) -> t a -> f (t b) Source #
A variant on traverse
that allows the targets to be filtered
out of the Witherable
structure. Note that this introduces a
change in types down the lens composition chain, which means that
it is not a a valid optic at all. The use of Withering
in the
changed type also means that standard lens combinators don't fit
To address these issues, you can use unwithered
to strip the
Withering
type back out. This allows the composed optic to be
used with standard combinators from lens. In addition, the sequence
will act like a type-restricted version
of withered
. unwithered
traverse
for all lawful instances of Witherable
.
In some sense, this is a catch
-like combinator. This marks the
point where removing elements stops propagating and actually
modifies the structure being focused.
unwithered :: Functor f => (a -> f b) -> a -> Withering f b Source #
Restore types in a lens composition chain that has had
Withering
introduced. Makes no changes to what elements are
focused on.
rewithered :: (Applicative f, Witherable t) => (a -> Withering f b) -> t a -> Withering f (t b) Source #
A variant of withered for when you're already working in a Withering chain and want to change what structure elements are being removed from.
rewithered
=unwithered
.withered
decayed :: Applicative f => pafb -> s -> Withering f t Source #
The trivial optic in a Withering chain that removes everything.
The arguments are unused.
Functions consuming withering lens-likes
forMaybeOf :: ((a -> Withering f b) -> s -> f t) -> s -> (a -> f (Maybe b)) -> f t Source #
A version of witherOf
with arguments re-arranged.
mapMaybeOf :: ((a -> Withering Identity b) -> s -> Identity t) -> (a -> Maybe b) -> s -> t Source #
Transform and filter elements matched by a specific Withering
context, a la mapMaybe
. See witherOf
for a more
flexible version that works within arbitrary Applicative
effects.
>>>
mapMaybeOf (withered . _1) (\x -> if even x then Just (show x) else Nothing) [(1,2),(2,4),(3,6),(4,8)]
[("2",4),("4",8)]
catMaybesOf :: ((Maybe a -> Withering Identity a) -> s -> Identity t) -> s -> t Source #
Filter Nothing
values out of a structure, like
catMaybes
.
>>>
catMaybesOf withered [Just 1, Nothing, Just 2]
[1,2]
>>>
catMaybesOf (withered . _2) [("a", Just 1), ("b", Nothing), ("c", Just 2)]
[("a",1),("c",2)]
filterAOf :: Applicative f => ((a -> Withering f a) -> s -> f s) -> (a -> f Bool) -> s -> f s Source #
Remove elements matched by a specific Withering
context if they
don't match a predicate returning a result in an arbitrary
Applicative context.
>>>
filterAOf (withered . _1) (const readLn) [(1,2),(2,4),(3,6),(4,8)]
False True True False [(2,4),(3,6)]
filterOf :: ((a -> Withering Identity a) -> s -> Identity s) -> (a -> Bool) -> s -> s Source #
Remove elements matched by a specific Withering
context if they
don't match a predicate.
>>>
filterOf (withered . _1) even [(1,2),(2,4),(3,6),(4,8)]
[(2,4),(4,8)]
ordNubOf :: Ord a => ((a -> Withering (State (Set a)) a) -> s -> State (Set a) s) -> s -> s Source #
Removes duplicates from a structure based on the focused element.
>>>
ordNubOf (traverse . withered . _2) [[("z",3),("q",3)],[("apple", 1), ("bat", 1), ("cat", 2), ("dog", 2)]]
[[("z",3)],[("apple",1),("cat",2)]]
ordNubOnOf :: Ord b => ((a -> Withering (State (Set b)) a) -> s -> State (Set b) s) -> (a -> b) -> s -> s Source #
Removes duplicates from a structure based on applying a function to the focused element.
>>>
ordNubOnOf (withered . _2) (`div` 2) [("apple", 1), ("bat", 2), ("cat", 3), ("dog", 4)]
[("apple",1),("bat",2),("dog",4)]
hashNubOf :: (Eq a, Hashable a) => ((a -> Withering (State (HashSet a)) a) -> s -> State (HashSet a) s) -> s -> s Source #
hashNubOnOf :: (Eq b, Hashable b) => ((a -> Withering (State (HashSet b)) a) -> s -> State (HashSet b) s) -> (a -> b) -> s -> s Source #
Removes duplicates from a structure based on applying a function
to the focused element. Often will be more efficient than
ordNubOnOf
if your data type supports Hashable
>>>
hashNubOnOf (withered . _2) (`div` 2) [("apple", 1), ("bat", 2), ("cat", 3), ("dog", 4)]
[("apple",1),("bat",2),("dog",4)]