Home > Enterprise >  Using composition to get ceiling of a division
Using composition to get ceiling of a division

Time:10-13

I'm learning haskell, and I'm trying to rewrite a function using composition only

Here's the function I'm trying to refactor:

ceilingDiv a b = ceiling (a / b)

So far I managed to make it work using curry and uncurry but it feels dirty:

ceilingDiv = curry $ ceiling . uncurry (/)

Is there any way to do this more cleanly? I was thinking ceiling . div, but it doesn't work because (/) returns a function and ceiling takes a Double as its argument.

CodePudding user response:

There's a fun site called https://pointfree.io - it provides a solution to your problem like this: ceilingDiv = (ceiling .) . (/). It looks ugly because point-free composition with multiple arguments is a pain. Sometimes it's achieved by using . sections like here, sometimes by using an Applicative instance of functions and the <*> operator.

I think your solution with curry-uncurry is nice. It deals with passing multiple arguments by wrapping them into one tuple.

CodePudding user response:

Compositions with multiple arguments are usually best just left in point-ful form. None of the alternatives is as clean and clear. Do make use of composition- and grouping operators, but don't golf away arguments just for the sake of it.

ceilingDiv a b = ceiling $ a/b

What you can certainly do is eta-reduce the b argument

ceilingDiv a = ceiling . (a/)

but I'd leave it at that. Even that is IMO less clean than the original 2-argument form, because the division operator needs to be sectioned.

When directly passing compositions to a higher-order function then it can make sense to aim a bit more agressively point-free, to avoid lambda bindings. The composition package has a whole bunch of operators for composing with more arguments. In this case, you could use the indeed quite concise

ceilingDiv = ceiling .: (/)
  • Related