[Haskell-cafe] ANN: loop - fast for loops

wren romano winterkoninkje at gmail.com
Sun Jul 20 05:26:03 UTC 2014


On Wed, Apr 30, 2014 at 11:21 AM, Niklas Hambüchen <mail at nh2.me> wrote:
> Announcing the first version of the loop package:
>
> http://hackage.haskell.org/package/loop
>
> This is for iterating over numeric or otherwise iteratable things in a
> fast fashion. Looks like C, runs like C.
>
> It's trivial, but very robust performance-wise and more independent of
> GHC smartness or non-smartness in regards to optimising alternatives
> like `forM_ [1..n]`.
>
> It comes with a lot of benchmarks.
>
> Contributions are very welcome.

How do you feel about bikeshedding contributions? The following seems
more idiomatic to me:

    -- Formerly @forLoopFold at . This is the primitive notion, so it
should have the simplest name.
    loop  ::  a -> (a -> Bool) -> (a -> a) -> b -> (b -> a -> b) -> b

    -- This one is new. We want to implement it directly instead of
using 'loop' for the same reasons that we want to implement 'loopM_'
directly.
    loopM :: Monad m => a -> (a -> Bool) -> (a -> a) -> b -> (b -> a
-> m b) -> m b

    -- Formerly @forLoop@
    loopM_ :: Monad m => a -> (a -> Bool) -> (a -> a) -> (a -> m ()) -> m ()

    loopFromTo :: (Eq a, Enum a) => a -> a -> b -> (b -> a -> b) -> b
    loopFromTo start stop = loop start (stop ==) succ

    loopNumFromTo :: (Eq a, Num a) => a -> a -> b -> (b -> a -> b) -> b
    loopNumFromTo start stop = loop start (stop ==) (1+)

    loopFromToM :: (Eq a, Enum a) => a -> a -> b -> (b -> a -> m b) -> m b
    loopFromToM start stop = loopM start (stop ==) succ

    loopNumFromToM :: (Eq a, Num a) => a -> a -> b -> (b -> a -> m b) -> m b
    loopNumFromToM start stop = loopM start (stop ==) (1+)

    loopFromToM_ :: (Eq a, Enum a) => a -> a -> (a -> m ()) -> m ()
    loopFromToM_ start stop = loopM_ start (stop ==) succ

    loopNumFromToM_ :: (Eq a, Num a) => a -> a -> (a -> m ()) -> m ()
    loopNumFromToM_ start stop = loopM_ start (stop ==) (1+)

the loopNum* names are pretty arbitrary/crappy, but using the *FromTo,
*M, and *M_ names is far more idiomatic.

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list