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
8
votes
4 answers
Why ReactJS components must act like pure functions?
the documentation says
All React components must act like pure functions with respect to their props.
https://facebook.github.io/react/docs/components-and-props.html, but does not explain the real reason behind it, why is that?

nomadus
- 859
- 1
- 12
- 28
8
votes
2 answers
Why this implementation of a pure function isn't considered to have external dependencies?
I'm fine with the pure function concept on pretty simple examples like...
function addTwo(val){
return val + 2;
}
Given the same arguments, it yields the same result, leading to Referential Transparency and good deterministic code.
But then I've…

sminutoli
- 853
- 4
- 11
7
votes
3 answers
Is print in Haskell a pure function?
Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should.

bubu
- 179
- 2
- 13
7
votes
1 answer
How to test a tree of pure function calls in isolation?
In our team of JavaScript devs we have embraced redux/react style of writing pure functional code. However, we do seem to have trouble unit testing our code. Consider the following example:
function foo(data) {
return process({
value:…

VoY
- 5,479
- 2
- 37
- 45
6
votes
2 answers
Haskell: Trapped in IO monad
I am trying to parse a file using the parseFile function found in the the haskell-src-exts package.
I am trying to work with the output of parseFile which is of course IO, but I can't figure out how to get around the IO. I found a function liftIO…

user2548080
- 187
- 7
5
votes
2 answers
How is this pure function able to modify non-private state?
TDPL, p. 167:
as long as the mutable state in a function is entirely transitory (i.e., allocated on the stack) and private (i.e., not passed along by reference to functions that may taint it), then the function can be considered pure.
import…

Arlen
- 6,641
- 4
- 29
- 61
5
votes
1 answer
Must a React reducer be a pure function?
I wrote a UI element as a function component which uses React's userReducer hook and it seems to run without errors.
useReducer references a function I wrote (called, imaginatively, reducer):
const [state, dispatch] = React.useReducer(reducer,…

ChrisW
- 54,973
- 13
- 116
- 224
5
votes
0 answers
How can I safely sandbox a pure javascript function in Node?
I'd like to be able to safely execute third party javascript inside a Node app. The code they would provide must be entirely pure, which means I can happily isolate them from anything globally available.
They will need to call certain libraries but…

Dave Hunt
- 157
- 1
- 8
5
votes
1 answer
Effects of declaring a function as pure or const to GCC, when it isn't
GCC can suggest functions for attribute pure and attribute const with the flags -Wsuggest-attribute=pure and -Wsuggest-attribute=const.
The GCC documentation says:
Many functions have no effects except the return value and their return value…

Riot
- 15,723
- 4
- 60
- 67
4
votes
1 answer
Can pure functions exist in pure object-oriented programming languages?
For the purposes of this question, by 'pure object-oriented programming language' I mean one in which functions can only exist inside (static or non-static) objects, i.e. as methods.
Consider the following Java code:
import java.lang.Math;
class…

Jacob Archambault
- 642
- 9
- 16
4
votes
1 answer
How to test a function that returns functions?
Here I have a function generateBlocks that takes in an array blocks and a function onBlockClick. It returns an array of object where each object has two properties label and onClick.
function generateBlocks(blocks, onBlockClick){
return…

Frozen Crayon
- 5,172
- 8
- 36
- 71
3
votes
1 answer
Getting I/O in a functional program
So I know that you can't directly get input from a user in a functional program because it obviously wouldn't be pure. But does this still apply if the user had only like 4 options to choose from?
For instance, is it still impure if you're asking…

skiboy108
- 53
- 3
3
votes
2 answers
How to skip unnecessary IOs in pure functions?
Update
There is an extra limit of this question,
which is to avoid IO as much as possible.
This limit was originally placed at the end of my question,
which seems to be hard to be noticed.
I actually know how to achieve my goal in Haskell,
just as I…

CopperCash
- 563
- 5
- 15
3
votes
0 answers
Optimization for pure vs. const function
The source code I use in this post, is also available here: https://gcc.godbolt.org/z/dGvxnv
Given this C source code:
int pure_f(int a, int b) __attribute__((pure));
int const_f(int a, int b) __attribute__((const));
int my_f(int a, int b) {
…

Kevin Meier
- 2,339
- 3
- 25
- 52
3
votes
5 answers
How would you write a "pure" function that removes an element from a List?
In Java (or any similar language) how would you write a Pure function (or method) that removes an element from a list.
If the element is in the list, we simply return a new (ideally immutable) list containing all the elements of the input list,…

Chris
- 4,212
- 5
- 37
- 52