19
loading...
This website collects cookies to deliver better user experience
TL;DR In programmers analogy a function maps the argument to the return value (also called as main effect).
f: x -> y
. Now, if the function is denoted by f then the relation(that associates) is denoted by y = f(x)
read as "f of x", where x,y are elements of domain and co-domain respectively. x is also often called as argument/input of the function f and y is the value, the output or the image of f(x)./*
* domain: number
* co-domain: number
* square: x -> x * x
*/
function square(x: number): number {
return x * x;
}
square
is the function that maps the elements of the domain (inputs/arguments) with the element of the co-domain output. This function as said above completely yields the value based on its inputs and nothing else matters to it.However the term pure functions doesn't come from mathematics and neither by imperative programming languages, but rather came from languages that allowed side-effects(impure functional programming languages).
💡 A pure function is always isolated.
💡 A pure function has no side-effects
function doubleNum(num:number): number {
return 2 * num;
}
const x = doubleNum(3); //6
// should be same as
const x = 6;
// then doubleNum(num) is said to referentially transparent
you'll can refer this Stackoverflow answer for a detailed explanation.
💡 A Pure functions is referentially transparent.
A unit test is testing a way to test unit -a small piece of code that can be logically separated/isolated.