Pure and Impure Functions

Pure and impure functions are concepts in functional programming that describe the behavior and characteristics of functions based on their interactions with external state and data.

Pure Functions:

  1. Deterministic: Given the same inputs, pure functions always produce the same output, regardless of any external factors. This property makes them predictable and reliable.
  2. No Side Effects: Pure functions do not modify the state of external variables, objects, or resources, nor do they perform any observable actions outside their scope, such as logging to the console or modifying files.
  3. Referential Transparency: Pure functions can be replaced with their return values without affecting the correctness of the program. This property facilitates reasoning about and optimizing code.
  4. Idempotent: Calling a pure function multiple times with the same inputs does not change the state of the program or produce different results.

Example of a Pure Function:

function add(a, b) {
  return a + b;
}

This add function takes two arguments and returns their sum. It produces the same result for the same inputs every time it’s called, and it doesn’t modify any external state.

Impure Functions:

  1. Side Effects: Impure functions can modify external state or perform actions that have observable effects beyond returning a value. These side effects may include modifying variables, objects, or resources outside the function’s scope.
  2. Non-Deterministic: Impure functions may produce different results for the same inputs depending on external factors such as the state of the program, the environment, or the passage of time.
  3. Dependencies on External State: Impure functions may rely on or interact with external variables, objects, or resources, making their behavior unpredictable and harder to reason about.
  4. Not Referentially Transparent: Impure functions cannot be replaced with their return values without potentially altering the behavior of the program or causing unintended side effects.

Example of an Impure Function:

let sum = 0;

function addToSum(value) {
  sum += value;
  return sum;
}

This addToSum function modifies the external variable sum every time it’s called, causing side effects. Additionally, its return value depends on the current state of sum, making it non-deterministic.

5 Functions Pure And Impure

To get a better understands of pure and impure functions within javacsript eco system, well look at the following javscript methods that are parts of javascript language and wrap our heads around why method is pure and why it’s impure.

Impure Functions

  1. Date.now(): Returns the current timestamp. Its return value depends on the current time, which can change between invocations.
  2. Math.random(): Generates a random number. Its return value is unpredictable and can vary between invocations.
  3. console.log(): Outputs data to the console. It has side effects and doesn’t return a value, making it impure.
  4. fetch(): Performs an asynchronous HTTP request. Its behavior depends on external factors such as network latency and server response time.
  5. fs.writeFile(): Writes data to a file on the filesystem. Its behavior depends on the state of the filesystem and can vary between invocations.

Pure Functions

  1. Math.max(): Returns the largest of zero or more numbers. Given the same input, it always returns the same output, without causing side effects.
  2. Array.map(): Creates a new array by applying a function to each element of an existing array. It doesn’t modify the original array and always returns a new array based on the input.
  3. String.toLowerCase(): Returns a new string with all alphabetic characters converted to lowercase. It doesn’t modify the original string and produces the same output for the same input.
  4. parseInt(): Parses a string argument and returns an integer. It always produces the same result for the same input string and doesn’t have side effects.
  5. Object.assign(): Copies the values of all enumerable own properties from one or more source objects to a target object. It creates a new object without modifying the source objects and returns the target object.

Leave a Reply

Your email address will not be published. Required fields are marked *

All rights reserved 2024 ©