Safe Haskell | Safe-Inferred |
---|
Provides a convenient and fast alternative to the common
forM_ [1..n]
idiom, which in many cases GHC cannot fuse to efficient
code.
Notes on fast iteration:
- For
Int
,(+1)
is almost twice as fast assucc
becausesucc
does an overflow check. - For
Int
, you can get around that while still usingEnum
usingtoEnum . (+ 1) . fromEnum
. - However,
toEnum . (+ 1) . fromEnum
is slower thansucc
forWord32
on 64-bit machines sincetoEnum
has to check if the givenInt
exceeds 32 bits. - Using
(+1)
fromNum
is always the fastest way, but it gives no overflow checking. - Using
forLoop
you can flexibly pick the way of increasing the value that best fits your needs. - The currently recommended replacement for
forM_ [1..n]
isforLoop 1 (<= n) (+1)
.