0
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?

Jun
  • 21
  • 1
  • 7
  • 1
    This particular function is pure because it meets the two criteria: (1) the output is the same for the same input (i.e., it's deterministic), and (2) there are no side effects – Nick Jul 28 '21 at 12:51
  • You may find this interesting: https://stackoverflow.com/questions/39834458/why-this-implementation-of-a-pure-function-isnt-considered-to-have-external-dep/39834974 – Nick Jul 28 '21 at 12:52

2 Answers2

0

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.

-1

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.

Hitmands
  • 13,491
  • 4
  • 34
  • 69