From d529d788464962e8b28c092aca08f1c023dcd6ff Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Fri, 21 Nov 2008 18:54:23 +0000 Subject: [PATCH] Added a slightly hacky fastListify function built on top of applyDepthM --- pass/Traversal.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pass/Traversal.hs b/pass/Traversal.hs index 58c7389..0ef6c7c 100644 --- a/pass/Traversal.hs +++ b/pass/Traversal.hs @@ -28,8 +28,10 @@ module Traversal ( , DescendM, Descend, makeDescend , applyDepthM, applyDepthSM, applyDepthM2 , checkDepthM, checkDepthM2 + , fastListify ) where +import Control.Monad.State import Data.Generics import qualified AST as A @@ -203,3 +205,10 @@ checkDepthM2 f1 f2 = makeRecurse ops ops = baseOp `extOp` makeCheck ops f1 `extOp` makeCheck ops f2 +-- | Lists all the items (in arbitrary order) that meet the given criteria. Just +-- like the Data.Generics listify function, only faster +fastListify :: forall s t. (Data s, Data t) => (t -> Bool) -> s -> [t] +fastListify f x = execState (applyDepthM f' x) [] + where + f' :: t -> State [t] t + f' y = when (f y) (modify (y:)) >> return y