4 ms·
Maybe this is a bit off-topic but I have to say: I don't _get_ FP languages. I get the idea of having functions with no side-effects. I think that's a great thi
by serbuvlad 2y ago
Maybe this is a bit off-topic but I have to say: I don't _get_ FP languages. I get the idea of having functions with no side-effects. I think that's a great thing. But this can be achieved in every language.
Sure, the statements in the function will have side-effects on local variables, but as long as the function isn't too long, and you can comprehend all of it at a time, that's not a problem.
Your examples of "clean code" aren't any cleaner than what can be found in any imperative language.
Using C++ (with std::optional), for reasons of familiarity.
(person.getAge() >= 18) ? "beer" : {};
UUID::fromWords(getRandom(), getRandom(), getRandom(), getRandom());
cache.has_value() ? cache : (cache = computeFresh(seed));
for (auto& elem : collection) {
modify(elem);
}
- Tainnor 2y agoOther languages can't help you track that a function that transitively calls 100 other functions is side-effect free, Haskell can. > UUID::fromWords(getRandom(), getRandom(), getRandom(), getRandom()) This ignores that getRandom() is a side-effecting function (it has to be because it returns something different every time). And that's not just a "local variable". UUID.fromWords val1 val2 val3 val4 works in Haskell too, the "extra syntax" is to be able to all that in IO (i.e. with side effects). If you want it in a form that is more recognisable, you could write it as getUuid :: IO UUID getUuid = do val1 <- getRandom val2 <- getRandom val3 <- getRandom val4 <- getRandom return UUID.fromWords val1 val2 val3 val4 but that's unnecessarily verbose if you know the idiom. Your cache example is similarly side effecting.