r/functionalprogramming • u/Echoes1996 • Dec 02 '24
Question Is this function pure?
Consider a function f(x) that invokes an impure function g(), which is not referentially transparent, though it has no side effects. Then, function f decides to do nothing with the result returned by function g and goes on to return a value based on its argument x. My question is: is f pure?
Example:
global y
def g():
# Not referentially transparent, though it does not
# alter the "outside world".
return y
def f(x: int):
_ = g() # Invoke non-referentially transparent function g.
return x + 1 # Return result solely based on input x.The output of f is completely dependent on its input, and it has no side effects, as g has no side effects as either. So, is f pure or not?
7
Upvotes
View all comments
Show parent comment
1
u/boris_m Dec 29 '24
The definition of purity depends on what you care about, here I would say that f has no *observable* side effects, which is as good as pure in many contexts.