[ANN] control-block

Hello!

I have released version 0.0.1 of control-block. Those of you who enjoy -XBlockArguments and -XLambdaCase may find this library useful: it provides variants of frequently-used functions (fmap, filter, foldl', …) with different argument orders: the function argument is passed last, so that complex functions can be passed anonymously, and case splits for Maybe and [] can be written even more consisely.

Its only dependencies are indexed-traversable (imap, ifoldMap, …) and witherable (filter, mapMaybe, …) to provide switched versions of those functions as well.

7 Likes

How does usage look like in practice? I imagine it’s something like what @jackdk hinted at: Just 3 & foldMap \m -> Sum (m * 2)

But it would be interesting to see a side-by-side comparison.

Usage with (&) is still subject to precedence rules, and may be parsed as part of the body of the function you’d be using. I mentioned -XLambdaCase because it allows immediate definition of the function body without wrapping in parentheses or requiring a let or where block to name it. Consider the following contrived example:

example = reduce [1 .. 100] \case
  n | n `mod` 25 == 0 -> "."
    | n `mod` 37 == 0 -> "#"
    | otherwise -> " "
> example
"                        .           #            .                       #.                        ."

c.f.

example = foldMap f [1 .. 100]
 where
  f n
    | n `mod` 25 == 0 = "."
    | n `mod` 37 == 0 = "#"
    | otherwise = " "

In expressions where the type inside the Foldable has many constructors, this becomes more convenient.