A function that always evaluates to the same result value given the same argument value(s) and that does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
Questions tagged [pure-function]
124 questions
115
votes
9 answers
Is a function that calls Math.random() pure?
Is the following a pure function?
function test(min,max) {
return Math.random() * (max - min) + min;
}
My understanding is that a pure function follows these conditions:
It returns a value computed from the parameters
It doesn't do any work…

Kiwi Rupela
- 2,238
- 5
- 24
- 44
91
votes
6 answers
Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"?
The two conditions that define a function as pure are as follows:
No side effects (i.e. only changes to local scope are allowed)
Always return the same output, given the same input
If the first condition is always true, are there any times the…

Magnus
- 6,791
- 8
- 53
- 84
74
votes
6 answers
Are idempotent functions the same as pure functions?
I read Wikipedia's explanation of idempotence.
I know it means a function's output is determined by it's input.
But I remember that I heard a very similar concept: pure function.
I Google them but can't find their difference...
Are they equivalent?

Lai Yu-Hsuan
- 27,509
- 28
- 97
- 164
41
votes
5 answers
Why are "pure" functions called "pure"?
A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same…

Tyler
- 21,762
- 11
- 61
- 90
21
votes
3 answers
Higher order function returns pure function
Here's an example of an higher order function called functionA that has customValue as input and returns a function that gets an input and uses the custom value to elaborate a result:
let functionA = (customValue) => {
let value = customValue ||…

Francesco Meli
- 2,484
- 2
- 21
- 52
21
votes
1 answer
Can I restrict a function to be pure in TypeScript?
Is there a way to allow a function to be pure only (thus not accepting the function to be non pure) in TypeScript? If yes, which?

gsamaras
- 71,951
- 46
- 188
- 305
17
votes
6 answers
What is an example of an impure function in JavaScript
Having seen a lot of pure functions and how they have no side effects, what would be an example of an impure function, which is always been antagonized as unstable and major source of error?

user2167582
- 5,986
- 13
- 64
- 121
15
votes
2 answers
Why props in React are read only?
The React documentation says:
React is pretty flexible but it has a single strict rule: all React components must act like pure functions with respect to their props.
Why is that?
I guess that if you change directly the value of the props, the…

L. Pier Roberto
- 513
- 1
- 6
- 20
15
votes
5 answers
Pure functional using F#
Is it possible to force F# to behave like a pure functional language like Haskell? Maybe using some compiler directives?
PS: since I come from a C/C++ background, I want to force myself to learn functional programming without learning Haskell :)

r00kie
- 843
- 1
- 11
- 12
15
votes
3 answers
Can a pure function have free variables?
For example, a referentially transparent function with no free variables:
g op x y = x `op` y
A now now a function with the free (from the point-of-view of f) variables op and x:
x = 1
op = (+)
f y = x `op` y
f is also referentially transparent. …

Matt Fenwick
- 48,199
- 22
- 128
- 192
13
votes
2 answers
Why are pure reducers so important in redux?
Pure reducers have no side effects and enable things like time-travelling. They make reasoning about application behavior easier.
This is intuitive to me. But I cannot articulate WHY pure reducers lead to these positive non-functional attributes.…

Ben Aston
- 53,718
- 65
- 205
- 331
12
votes
3 answers
Can a pure function call external function?
Can a pure function call an external method?
for example:
class Dog {
function jump(name) {
return "a dog named " + name + " jumped!"
}
function jumpTwice(names) {
var result = [];
for (var i = 0; i < 2; i++) {
…

bbnn
- 3,505
- 10
- 50
- 68
11
votes
3 answers
How to tell if an F# function is pure?
Suppose I have these two F# functions:
let sq x = x*x
let tm = DateTime.Now
Clearly sq is pure in that it will always return the same value for a given input while tm is impure because it will return a different value each time it is called.
In…

JonnyBoats
- 5,177
- 1
- 36
- 60
10
votes
5 answers
Why is println considered an impure function?
I'm reading the book programming in scala, and it is said :
... in this case , its side effect is printing to the standard output stream.
and I don't see where is the side effect, since, for the same input, println will print the same output (I…

aName
- 2,751
- 3
- 32
- 61
10
votes
5 answers
How to use pure in D 2.0
While playing around with D 2.0 I found the following problem:
Example 1:
pure string[] run1()
{
string[] msg;
msg ~= "Test";
msg ~= "this.";
return msg;
}
This compiles and works as expected.
When I try to wrap the string array in a…

Jeroen Dirks
- 7,705
- 12
- 50
- 70