Search code examples
functionhaskellchain

Haskell chain functions based on separate conditions


Is there an efficient/easy way, when chaining functions applied to one argument, to only apply functions that fulfill their requirements in order to be applied? E.g.:

I have 1 argument 'Obj' and 3 functions: func1, func2, func3. Each have their own requirements which need to be fulfilled in order for the function to be applied to the argument as well. E.g. pseudo-code:

(if condition1 then func1) . (if condition2 then func2) . (if condition3 then func3) Obj

So if all conditions are qualified, all 3 functions would be applied to Obj.

Is there any way I can do this properly?


Solution

  • First, you have a missing $ before Obj. I think in answer to your question, you just need to supply an else clause:

    (if c0 then f0 else id) $ (if c1 then f1 else id) $ (if c2 then f2 else id) $ arg