You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove unsafeCoerce, use only coerce on GHC 7.8 and later.
Also, move the conditional compilation to a local where definition.
On my GHC 7.6.3, there is no heap allocation in the cmm in fromFunction
for the (Elem . f) closure, so there is no penalty of not using `coerce`.
Nevertheless, GHC 7.8.3 and GHC-head (15 Dec 2014) do heap-allocate
trivial closure for (Elem . f), so `coerce` helps.
Back to GHC 7.6.3, I found that the following does not allocate in
GHC 7.6.3:
newtype Elem a = Elem a
elemMap :: Int -> (Int -> b) -> [Elem b]
elemMap s f = go (Elem . f) 0
where go :: (Int -> b) -> Int -> [b]
go f i | i >= s = []
| otherwise = f i : go f (i+1)
Nevertheless, the following does heap-allocate trivial closure for f:
newtype Elem a = Elem a
elemMap :: [Int] -> (Int -> b) -> [Elem b]
elemMap xs f = go (Elem . f) xs
where go :: (Int -> b) -> [Int] -> [b]
go f [] = []
go f (x:xs) = f x : go f xs
I am not sure what the difference is, but the current fromFunction
does not allocate too (on 7.6.3).
0 commit comments