Refactoring in Haskell: Adding an Argument

code,tips 10 December 2008 | 0 Comments

Just a small tip on this: When you add an argument to a function that already exists you should check the existing usage of the function. Say you have this:

f x y z = ...

… and you want:

f x y z w = ...

First of all you should check the contexts where f is used. For example, if f is quite often used as an argument to map (fmap, <$>):

elsewhere xs = map (f a b) xs

If you naïvely add w to the end of the argument list you’ll end up with this instead:

elsewhere xs = map (\x-> f a b x c) xs

On the other hand, if you take a look at the usage of f you can decide that because f is often used to map over things, you can insert the additional parameter before the last, and end up with nicer code:

elsewhere xs = map (f a b c) xs

You may even discover that the existing f is used in a lot of lambdas that could be avoided by rearranging its arguments!

Tagged in , , , , , ,

Leave a Reply