Copyright | (c) 2014 Jonas Scholl |
---|---|
License | BSD3 |
Maintainer | jonas.scholl@gmx.de |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
This module provides a simple way to transform parts of complex data structures.
- mkItem :: Data a => (a -> a) -> EndoItem
- mkItemM :: (Monad m, Data a) => (a -> m a) -> EndoMItem m
- transform :: (Transformation d, Data a) => d -> a -> a
- transformM :: (MonadicTransformation d m, Data a) => d -> a -> m a
- unsafeTransform :: (Transformation d, Data a) => d -> a -> a
- unsafeTransformM :: (MonadicTransformation d m, Data a) => d -> a -> m a
- getSubterms :: (Data a, Data b, Monoid m) => (b -> m) -> a -> m
- getSubterms' :: (Data a, Data b) => a -> [b]
- getSubtermsBy :: (Data a, Data b) => (b -> Bool) -> a -> [b]
- getSubtermsWith :: (Data a, Data b, Monoid m) => (b -> Maybe m) -> a -> m
Wrapper functions
mkItemM :: (Monad m, Data a) => (a -> m a) -> EndoMItem m Source
Wrap a monadic function as an EndoMItem
.
Transformation functions
transform :: (Transformation d, Data a) => d -> a -> a Source
Transform some data structure by applying one or more endomorphisms to the data structure or any sub-term of it. Sub-terms are transformed before the terms containing them are transformed. If the given endomorphisms contain two or more endomorphisms working on the same type the latter endomorphisms will be applied to the result of the former endomorphisms
NOTE: This function attempts to check at runtime if all given endomorphisms
can be applied to at least one term in the given argument. If at least one
endomorphism can never be applied because of its type, error
is called.
If you don't want this behavior consider using unsafeTransform
instead.
Example:
>>>
transform (+1) (1, 4.0, (False, [4, 5, 6]))
(2, 4.0, (False, [5, 6, 7]))
>>>
transform [mkItem (+1), mkItem (sqrt :: Double -> Double), mkItem (*2)] (1, 4.0, (False, [4, 5, 6]))
(4, 2.0, (False, [10, 12, 14]))
>>>
transform (+1) False
*** Exception: Data.DataTraverse.transform: Could not find all needed types when mapping over a value of type Bool. Types of missing terms: [Integer]
transformM :: (MonadicTransformation d m, Data a) => d -> a -> m a Source
unsafeTransform :: (Transformation d, Data a) => d -> a -> a Source
unsafeTransformM :: (MonadicTransformation d m, Data a) => d -> a -> m a Source
Same as transformM
but omits any type checking (and hence does not call fail
).
Searching functions
getSubterms :: (Data a, Data b, Monoid m) => (b -> m) -> a -> m Source
Returns all sub-terms (intermediate and non intermediate) of some type of a
value transformed by the supplied function to some Monoid
.
NOTE: Calls error
if no sub-term which the needed type can exist.
Example:
>>>
getSubterms (\ x -> if x then [x] else []) (3, 4.0, True, 'c', (False, (True, 5, 6)))
[True, True]
getSubterms' :: (Data a, Data b) => a -> [b] Source
Returns all sub-terms (intermediate and non intermediate) of some type of a value as a list.
NOTE: Calls error
if no sub-term which the needed type can exist.
Example:
>>>
getSubterms' (3, 4.0, True, 'c', (False, (True, 5, 6))) :: [Integer]
[3, 5, 6]
getSubtermsBy :: (Data a, Data b) => (b -> Bool) -> a -> [b] Source
Returns all sub-terms (intermediate and non intermediate) of some type of a value which fulfill the predicate.
NOTE: Calls error
if no sub-term which the needed type can exist.
Example:
>>>
getSubtermsBy (<6) (3, 4.0, True, 'c', (False, (True, 5, 6)))
[3, 5]
getSubtermsWith :: (Data a, Data b, Monoid m) => (b -> Maybe m) -> a -> m Source
Returns all sub-terms (intermediate and non intermediate) of some type of a
value which could be transformed to some Monoid
.
NOTE: Calls error
if no sub-term which the needed type can exist.
Example:
>>>
getSubtermsWith (\ x -> guard (x < 6) >> return [x]) (3, 4.0, True, 'c', (False, (True, 5, 6)))
[3, 5]