From d5700e1427f7171db0e0e9393aedb734b554459f Mon Sep 17 00:00:00 2001 From: Ɓukasz Hanuszczak Date: Sun, 19 Jul 2015 18:16:11 +0200 Subject: Add SYB-like utility function for performing stateful queries. --- haddock-api/src/Haddock/Syb.hs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'haddock-api/src') 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. -- cgit v1.2.3