const pureFunc = (x) => x + 1
const isPureFunc = (x) => {
return pureFunc(x) + 1
}
Even though a function uses a const declared pure function without dependency, is the function a pure function?
const pureFunc = (x) => x + 1
const isPureFunc = (x) => {
return pureFunc(x) + 1
}
Even though a function uses a const declared pure function without dependency, is the function a pure function?
Yes, because the result is depend on its own args, not from the other. The result of this is not changed from the other function but changed if the x argument will change.
So it is pure function.
I would say that this function isn't pure,
its return value depends on a global function that cannot be controlled... The referential transparency principle states that, given the same input, a function must yield the same output. However, the dependency on pureFunc
constitutes a violation (it could change, or be non-pure itself).
to be pure, or "by-the-book-pure", it should be refactored this way:
const isPureFunc = (fn, x) => {
return fn(x) + 1
}
const pureFunc = (x) => x + 1;
const result = isPureFunc(pureFunc, 5);
With this said, of course, we don't need to be "that" academic, and accept this overall program as pure program since the pureFunc
function is "actually" under control.