[commit: ghc] master: Util.count: Implement as a left-fold instead of a right-fold (9aa5d87)

git at git.haskell.org git at git.haskell.org
Sat Aug 6 01:39:54 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/9aa5d87a28fbc328660829ec2e4841ea1a7a1440/ghc

>---------------------------------------------------------------

commit 9aa5d87a28fbc328660829ec2e4841ea1a7a1440
Author: Joachim Breitner <mail at joachim-breitner.de>
Date:   Fri Aug 5 21:39:07 2016 -0400

    Util.count: Implement as a left-fold instead of a right-fold


>---------------------------------------------------------------

9aa5d87a28fbc328660829ec2e4841ea1a7a1440
 compiler/utils/Util.hs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/compiler/utils/Util.hs b/compiler/utils/Util.hs
index d20a604..121fdbb 100644
--- a/compiler/utils/Util.hs
+++ b/compiler/utils/Util.hs
@@ -1,6 +1,6 @@
 -- (c) The University of Glasgow 2006
 
-{-# LANGUAGE CPP #-}
+{-# LANGUAGE CPP, BangPatterns #-}
 
 -- | Highly random utility functions
 --
@@ -619,9 +619,10 @@ all2 _ _      _      = False
 -- Count the number of times a predicate is true
 
 count :: (a -> Bool) -> [a] -> Int
-count _ [] = 0
-count p (x:xs) | p x       = 1 + count p xs
-               | otherwise = count p xs
+count p = go 0
+  where go !n [] = n
+        go !n (x:xs) | p x       = go (n+1) xs
+                     | otherwise = go n xs
 
 {-
 @splitAt@, @take@, and @drop@ but with length of another



More information about the ghc-commits mailing list