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
2
votes
4 answers
Using parameters that don't change after being read
I'm learning Haskell and writing a program to solve a toy problem. The program uses a parameter k that doesn't change while it is running, after the parameter has been read from a file. I'm very new to using pure functions, and I would like to write…

beardc
- 20,283
- 17
- 76
- 94
2
votes
2 answers
Escaping Safety with Debug Statements
I know of
debug writeln("Some good debug message")
in pure functions but what about functions that I have carefully tagged as @safe or @trusted? DMD currently doesn't allow debug writeln's in those because writeln and similar are currently…

Nordlöw
- 11,838
- 10
- 52
- 99
1
vote
2 answers
How pure should a function be when writing in React?
In functional programming, a pure function returns the same value for the same arguments.
I'd like to hear tips for writing in React sometime. I definitely do a lot of thinking at some point.
For example, the onClickHandler in the code below is not…

Lucas
- 128
- 7
1
vote
0 answers
Functions that have no effect on global state
We have the notion of a pure function, whose return value depends only and entirely on its parameters, and which does not change global state.
Example:
def f(a, b):
return a+b
What would be the correct term for a function that only meets the…

rwallace
- 31,405
- 40
- 123
- 242
1
vote
1 answer
gcc 12 suggesting to add the "pure" attribute
I have written a container class very similar to std::vector.
It has a size() member function, which I declared noexcept, const and constexpr.
class my_vector {
...
constexpr auto size() const noexcept -> size_type {
assert(stride_…

alfC
- 14,261
- 4
- 67
- 118
1
vote
1 answer
Would using a global function like Object.keys() inside a pure function be reasonably considered an impurity?
I'm currently going through my React project looking at where I can convert impure functions into pure functions in order to have less side effects, tidier and more understandable code.
I believe/hope converting to pure functions where possible will…

daniel blythe
- 946
- 2
- 16
- 44
1
vote
1 answer
How can I validate this in more elegant way?
Im trying to do login/register module in my project. This is my login function. I would like to have one function that will validate all things for me so I dont have to use so many "if" statements. I was trying to do with pure function but…

Pawel
- 27
- 4
1
vote
0 answers
Is noop a pure function?
In JavaScript, we can express a simple noop function like so (It's the same in Lodash):
const noop = () => {};
It might be a frivolous distinction, but does it count as a pure function?
My initial guess was that it is because:
Referentially…

sshh
- 5,964
- 4
- 17
- 20
1
vote
1 answer
Why delaying evaluation can transform impure functions into pure ones?
I know that a pure function is a function that doesn't rely on system's state, doesn't have side effects, and its output only depends on its inputs.
Making an http call is considered to be a side effect. So, the following is an example of an impure…

Nacho
- 21
- 1
1
vote
0 answers
Change Variable Defined In a Constructor of a class with a Pure Function
I have an ES6 Singleton pattern class with its constructor which has got a variable named name in.
class Sample {
constructor (){
this.name = ''
}
setName = (name)=> {
this.name = name
}
getName = () => {
return this.name
…

2019
- 81
- 1
- 1
- 9
1
vote
1 answer
How to insert API response into HTML
I used pure function to create wrapper for multiple HTML elements and don't understand how to insert JSON response API data into it.
Can you help to create one more pure function that insert API data into HTML likes a text or use exists function…

Maxim Gordiyenko
- 97
- 1
- 10
1
vote
0 answers
How to accommodate System.out.println() in Functional Programming in Java?
I want to ask the user for inputs using System.out.println(). Like, System.out.println("Enter name"). But, that would produce side-effects. Is there any way to bypass that?
System.out.println("Enter name: ");
String customerName =…

awesomemypro
- 531
- 1
- 11
- 32
1
vote
2 answers
Use closure to purify a function which builds an object through recursion — JavaScript
I've made a promise based function which crawls up a hierarchy until it reaches the top, and resolves with an object containing the structure. My only gripe with the code is that I modify variables outside the function body, meaning that it is not a…

Audun Olsen
- 597
- 6
- 17
1
vote
0 answers
Pure Asynchronous Tasks in Javascript
Combining Ramda and Folktale functors
_fetchLists is a function that performs an async operation, it takes the following arguments:
fetchAlllists: an async function that resolves with Result functor of an array of list objects
listIds: an array of…

Nizar
- 21
- 1
- 1
- 3
1
vote
2 answers
Creating a composed functions with pure functions
I am trying to get my head around both composed functions and pure functions.
I have an object with a mixture of data. On some values I need to:
remove the value's units
parse string to integer
Convert to value decimal
I have written three…

Stretch0
- 8,362
- 13
- 71
- 133