2 ms·
Here is a completely pure function in any imperative language: int add(int a, int b) { return a + b; }
by kaba0 4y ago
Here is a completely pure function in any imperative language:
int add(int a, int b) {
return a + b;
}
- CrimsonCape 4y agoYes, this is a great example of a pillar of functional programming, pure functions. What is concerning is that your code example, like other pure function examples out there, implements a math operation a first grader can perform. It's exceedingly difficult to find a pure functional example that operates on tangible domain-specific data of objects full of state.
- sidlls 4y agoIt's not at all difficult (https://pandoc.org/ https://pandoc.org/). However, I'd hazard a guess that pandoc written in Rust or C (with idioms appropriate to those languages) would be much more performant.
- kaba0 4y agoI’m not as sure, several optimizations are only possible if we know that they can’t/won’t have side effects, and while the compiler can see it after inlining, having it part of the signature does help. Haskell is quite impressive on the performance front.
- initplus 4y agoBut the purity is implicit. Nothing will stop you from refactoring add into an impure function. As a programmer you have to maintain this mentally by remembering which types are pass by value and which are pass by reference.