data-transform-0.1.0.0: Functions to transform data structures.

Copyright(c) 2014 Jonas Scholl
LicenseBSD3
Maintainerjonas.scholl@gmx.de
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Transform

Contents

Description

This module provides a simple way to transform parts of complex data structures.

Synopsis

Wrapper functions

mkItem :: Data a => (a -> a) -> EndoItem Source

Wrap a function as an EndoItem.

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

Same as transform but with a monadic function. Calls fail instead of error if a type-error is detected.

unsafeTransform :: (Transformation d, Data a) => d -> a -> a Source

Same as transform but omits any type checking (and hence does not call error).

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]