Pure functions are the atomic building blocks in functional programming. They are adored for their simplicity and testability. This post covers a quick checklist to tell if a function’s pure or not. The ChecklistA function must pass two tests to be considered “pure”:
Let’s zoom in on each one. 1. Same Input => Same OutputCompare this:
To this:
Pure Functions = Consistent ResultsThe first example returns a value based on the given parameters, regardless of where/when you call it. If you pass Nothing else affects the output. Impure Functions = Inconsistent ResultsThe second example returns nothing. It relies on shared state to do its job by incrementing a variable outside of its own scope. This pattern is a developer’s nightmare fuel. Shared state introduces a time dependency. You get different results depending on when you called the function. The first time results in Which Version’s Easier to Reason About?Which one’s less likely to breed bugs that happen only under certain conditions? Which one’s more likely to succeed in a multi-threaded environment where time dependencies can break the system? Definitely the first one. 2. No Side-EffectsThis test itself is a checklist. A few examples of side-effects are
Basically any work a function performs that isn’t related to calculating the final output. Here’s an impure function with a side-effect. Not So Bad
This, however, may cause a problem. “Impurely” Changing an Object
The variable, Shared state means
We can purify Purifying It Up
Now You could even do the following and remain pure: Another Pure Way
Mutating your input can be dangerous, but mutating a copy of it is no problem. Our end result is still a testable, predictable function that works no matter where/when you call it. The mutation’s limited to that small scope and you’re still returning a value. Deep-Cloning ObjectsHeads up! Using the spread operator Thank you Rodrigo Fernández Díaz for bringing this to my attention! Unsafe Nested Mutation
Both Safe Nested MutationTo safely mutate nested properties, we need a deep clone.
Now you’re guaranteed safety because they’re truly two separate entities! Summary
My Free CourseThis tutorial was from my completely free course on Educative.io, Functional Programming Patterns With RamdaJS! Please consider taking/sharing it if you enjoyed this content. It’s full of lessons, graphics, exercises, and runnable code samples to teach you a basic functional programming style using RamdaJS. Thanks for reading! Until next time. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What is a function called when its return value is always the same for the same arguments?A function that always returns the same result when called with the same arguments, and does not change any global data, is referred to as a pure function.
Can the same input have the same output?A pure function is a function that will always give the same output with the same input and has no side effects. A function that takes some arguments will always return the same value and not be affected by variables outside the function, nor will it change variables outside of it after it's done executing.
Can a function have the same input and output?Each input has only one output. Each input has only one output, and the fact that it is the same output (4) does not matter. This relation is a function. Remember that in a function, the input value must have one and only one value for the output.
...
. What type of function produces the same output value every time it is called using the same parameters?In computer programming, a pure function is a function that has the following properties: the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and.
|