3 ms·
As long as you don't mix your functional code with imperative code you can stay pure.
by aerique 16y ago
As long as you don't mix your functional code with imperative code you can stay pure.
- perlgeek 16y agoTo me, multi-paradigm always was about mixing the styles, so this sounds a like quite a radical restriction, and a bit weird.
- dkersten 16y agoWell, inside a function, you can have as much imperative code as you like, with mutable local variables and what-not, the function overall is still pure as long as it does not cause side-effects. Eg, functional code could looks something like this: foo a b = if a < b then quux (map bar a) else b while a pure imperative function could look something like this: int foo (string a, int b) { // since a is passed by value, mutating it below will not modify the original if (a < b) { for (int idx=0; idx<a.length; ++idx) { a[idx] = bar(a[idx]); } return quux(a); } else { return b; } } Obviously this example is contrived and dumb, but both do the same thing and, besides the syntax difference, the first is written in a functional style, the second in an imperative style, but both are still pure (as long as bar and quux are also pure). So you can mix functional and imperative without making functions impure.
- aerique 16y agoYes, but you don't have to mix them each and every time. It's just that you have the option to do so and aren't constrained to a single language designer's favourite style.