Skip to content

Commit 1a33149

Browse files
committed
Use Fuuzetsu's Array foldMap
1 parent 70d7825 commit 1a33149

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

Data/HashMap/Array.hs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -451,30 +451,18 @@ foldl f = \ z0 ary0 -> go ary0 (length ary0 - 1) z0
451451
{-# INLINE foldl #-}
452452

453453
-- We go to a bit of trouble here to avoid appending an extra mempty.
454+
-- The below implementation is by Mateusz Kowalczyk, who indicates that
455+
-- benchmarks show it to be faster than one that avoids lifting out
456+
-- lst.
454457
foldMap :: Monoid m => (a -> m) -> Array a -> m
455-
foldMap f = \ary0 ->
456-
let len = length ary0
457-
in if len == 0
458-
then mempty
459-
else go ary0 (len - 1) 0
460-
where
461-
go ary !lst i
462-
| (# x #) <- index# ary i
463-
, let fx = f x
464-
= if i == lst
465-
then fx
466-
else fx `mappend` go ary lst (i + 1)
467-
468-
{-
469-
case index# ary0 lst of
470-
(# xn #) -> go ary0 lst 0 (f xn)
471-
where
472-
go ary lst i z
473-
| i == lst = z
474-
| otherwise
475-
= case index# ary i of
476-
(# x #) -> f x `mappend` go ary lst (i + 1) z
477-
-}
458+
foldMap f = \ary0 -> case length ary0 of
459+
0 -> mempty
460+
len ->
461+
let !lst = len - 1
462+
go i | (# x #) <- index# ary0 i, let fx = f x =
463+
if i == lst then fx else fx `mappend` go (i + 1)
464+
in go 0
465+
{-# INLINE foldMap #-}
478466

479467
undefinedElem :: a
480468
undefinedElem = error "Data.HashMap.Array: Undefined element"

0 commit comments

Comments
 (0)