4 ms·
Chained ifs add a whole lot of visual noise, aren't guaranteed to be total, and introduce the temptation to add more logic into the cases. That said, I do wish
by Nullabillity 4y ago
Chained ifs add a whole lot of visual noise, aren't guaranteed to be total, and introduce the temptation to add more logic into the cases.
That said, I do wish more languages had copied Haskell's guard syntax for this kind of chain... For reference, that looks like:
cmp x y | (x == Nothing || y == Nothing) = NaN -- (assuming x and y are Maybe, but this would typically be non-idiomatic)
| x < y = -1
| x > y = 1
| x >= y = 0 -- inert bug copied from original, "should" be == instead
| otherwise = NaN
- mpeg 4y agoI too always find myself wanting features from functional languages, I haven't done much Haskell but love the syntax for pattern matching in Elixir with guards, plus the pipe operator.