diff options
author | Ćukasz Hanuszczak <lukasz.hanuszczak@gmail.com> | 2015-07-19 18:16:11 +0200 |
---|---|---|
committer | Mateusz Kowalczyk <fuuzetsu@fuuzetsu.co.uk> | 2015-08-21 18:22:30 +0100 |
commit | d5700e1427f7171db0e0e9393aedb734b554459f (patch) | |
tree | df24abb0667948cfb831227d00653682d4a32976 /haddock-api | |
parent | 2c94f5a7804ecf84f818a64ba41ca5829621323c (diff) |
Add SYB-like utility function for performing stateful queries.
Diffstat (limited to 'haddock-api')
-rw-r--r-- | haddock-api/src/Haddock/Syb.hs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs index 2016b74c..4847e486 100644 --- a/haddock-api/src/Haddock/Syb.hs +++ b/haddock-api/src/Haddock/Syb.hs @@ -2,7 +2,7 @@ module Haddock.Syb - ( everything, everywhere + ( everything, everythingWithState, everywhere , mkT , combine ) where @@ -20,6 +20,20 @@ everything :: (r -> r -> r) -> (forall a. Data a => a -> r) -> (forall a. Data a => a -> r) everything k f x = foldl k (f x) (gmapQ (everything k f) x) + +-- | Perform a query with state on each level of a tree. +-- +-- This is the same as 'everything' but allows for stateful computations. In +-- SYB it is called @everythingWithContext@ but I find this name somewhat +-- nicer. +everythingWithState :: s -> (r -> r -> r) + -> (forall a. Data a => a -> s -> (r, s)) + -> (forall a. Data a => a -> r) +everythingWithState s k f x = + let (r, s') = f x s + in foldl k r (gmapQ (everythingWithState s' k f) x) + + -- | Apply transformation on each level of a tree. -- -- Just like 'everything', this is stolen from SYB package. |