2 ms·
Most languages which allow for function head clauses also have case or switch statements. So you can use both. I like the pattern when there is a corner case th
by rdtsc 7y ago
Most languages which allow for function head clauses also have case or switch statements. So you can use both. I like the pattern when there is a corner case that needs quite a bit of logic but it's not very important for how the code usually works, and you won't want an indent level on it. For example:
function(SpecialCase ...) ->
...
handle_odd_special_case;
function(DefaultWhatever ...) ->
...
main_business_logic.
This is more cleaner _sometimes_ than say
function(...) ->
case Arg of
SpecialCase ->
...
handle_odd_special_case;
Default ->
...
main_business_logic
end
Now imagine another level of case statements and all of the sudden the main path of the algorithm is squashed to the right with only a 40 columns left.
- sullyj3 7y agoIt seems to me that the things people like about the haskell style syntax are 1. Don't have to name the parameter 2. less nesting. You can get the best of all worlds with lambdacase style constructs. You don't need to write out the name of the function a bunch of times. function = \case SpecialCase -> ... handle_odd_special_case; Default -> ... main_business_logic;