Skip to content

Commit 402b05d

Browse files
committed
Rewrite IntMap map so it can inline; define <$
Previously, mapping a constant function would fill an `IntMap` with thunks.
1 parent b2280fc commit 402b05d

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Data/IntMap/Base.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,11 +1330,11 @@ isSubmapOfBy _ Nil _ = True
13301330
-- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
13311331

13321332
map :: (a -> b) -> IntMap a -> IntMap b
1333-
map f t
1334-
= case t of
1335-
Bin p m l r -> Bin p m (map f l) (map f r)
1336-
Tip k x -> Tip k (f x)
1337-
Nil -> Nil
1333+
map f = go
1334+
where
1335+
go (Bin p m l r) = Bin p m (go l) (go r)
1336+
go (Tip k x) = Tip k (f x)
1337+
go Nil = Nil
13381338

13391339
#ifdef __GLASGOW_HASKELL__
13401340
{-# NOINLINE [1] map #-}
@@ -2096,6 +2096,12 @@ instance Ord a => Ord (IntMap a) where
20962096
instance Functor IntMap where
20972097
fmap = map
20982098

2099+
#ifdef __GLASGOW_HASKELL__
2100+
a <$ Bin p m l r = Bin p m (a <$ l) (a <$ r)
2101+
a <$ Tip k _ = Tip k a
2102+
a <$ Nil = Nil
2103+
#endif
2104+
20992105
{--------------------------------------------------------------------
21002106
Show
21012107
--------------------------------------------------------------------}

Data/IntMap/Strict.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,11 @@ updateMin f = updateMinWithKey (const f)
767767
-- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
768768

769769
map :: (a -> b) -> IntMap a -> IntMap b
770-
map f t
771-
= case t of
772-
Bin p m l r -> Bin p m (map f l) (map f r)
773-
Tip k x -> Tip k $! f x
774-
Nil -> Nil
770+
map f = go
771+
where
772+
go (Bin p m l r) = Bin p m (go l) (go r)
773+
go (Tip k x) = Tip k $! f x
774+
go Nil = Nil
775775

776776
#ifdef __GLASGOW_HASKELL__
777777
{-# NOINLINE [1] map #-}

0 commit comments

Comments
 (0)