Questions tagged [pure-function]

A pure function is one that always evaluates to the same thing given the same arguments, and cannot change or depend on any external state.

A pure function has special meaning in programming. It is a function that:

  • Always evaluates to the same result given the same values
    • Does not depend on any external state of the function
  • Does not have any side effects
    • It doesn't change a mutable object or output to I/O devices

Examples of pure functions include math functions such as sin(x) which returns the sin of x.

Impure functions may return different results each time they are called. Classic examples of this include time() and random(). Additionally, functions that do IO are impure such as printf().

Further reading: Pure function at wikipedia

40 questions
51
votes
6 answers

Is a memoized pure function itself considered pure?

Let’s say fn(x) is a pure function that does something expensive, like returning a list of the prime factors of x. And let’s say we make a memoized version of the same function called memoizedFn(x). It always returns the same result for a given…
callum
  • 10,377
  • 9
  • 30
  • 33
46
votes
7 answers

What do you call a function where the same input will always return the same output, but also has side effects?

Say we have a normal pure function such as function add(a, b) { return a + b } And then we alter it such that it has a side effect function add(a, b) { writeToDatabase(Math.random()) return a + b; } It's not considered a pure function as far…
m0meni
  • 773
  • 1
  • 6
  • 12
28
votes
5 answers

Does catching/throwing exceptions render an otherwise pure method impure?

The following code examples provide context to my question. The Room class is initialized with a delegate. In the first implementation of the Room class, there are no guards against delegates that throw exceptions. Such exceptions will bubble up to…
27
votes
4 answers

Is there a good reason to make pure functions non-public?

I had a little debate going on with a coworker. Simply put, is there a good reason to hide/encapsulate functions that are pure? By "pure" I mean the wikipedia definition: Always returns the same results from the same input. (For the sake of this…
Telastyn
  • 108,850
  • 29
  • 239
  • 365
19
votes
2 answers

When to use [Pure] on a constructor?

I'm learning about code contracts in .NET, and I'm trying to understand the idea of pure constructors. The code contracts documentation states: All methods that are called within a contract must be pure; that is, they must not update any…
p.s.w.g
  • 4,135
  • 4
  • 28
  • 40
17
votes
6 answers

Pure functional vs tell, don't ask?

"The ideal number of arguments for a function is zero" is plain wrong. The ideal number of arguments is exactly the number needed to enable your function to be side-effect free. Less than that and you needlessly cause your functions to be impure…
gaazkam
  • 3,517
  • 3
  • 19
  • 35
17
votes
5 answers

Is a function immediately impure if it takes a function as a parameter?

Since the purity of an input parameter is an unknown until runtime, is a function immediately considered impure if it takes a function as an input parameter? Related: if a function applies a pure function that is defined outside of the function, but…
Dancrumb
  • 571
  • 5
  • 14
16
votes
5 answers

Is there a non-deterministic function without side effects?

By definition, a pure function is deterministic + no side effect. Is there any example for a function which has no side effects, but is non-deterministic? I.e., a function without side effects, but not pure. To me non-deterministic function comes…
Helin Wang
  • 271
  • 1
  • 5
15
votes
3 answers

Origin of "a method should return a value or have side-effects, but not both"

I read once that a method should either have a return value (and be referentially transparent), or have side-effect(s), but not both. I cannot find any references to this rule, but want to learn more about it. What is the origin of this advice? …
Wayne Conrad
  • 1,118
  • 10
  • 20
12
votes
4 answers

Why usage of assignment operator or loops discouraged in functional programming?

If my function meets the two requirements listed below, I believe that the function Sum returns the summation of the items in a list, where item evaluates as true for a given condition. Doesn't this mean that the function can be classified as…
11
votes
2 answers

How does functional programming handle the situation where the same object is referenced from multiple places?

I am reading and hearing that people (also on this site) routinely praise the functional programming paradigm, emphasising how good it is to have everything immutable. Notably, people propose this approach even in traditionally imperative OO…
gaazkam
  • 3,517
  • 3
  • 19
  • 35
11
votes
5 answers

Compute if a function is pure

As per Wikipedia: In computer programming, a function may be described as pure if both these statements about the function hold: The function always evaluates the same result value given the same argument value(s). The function result value…
Oni
  • 927
  • 1
  • 9
  • 13
10
votes
2 answers

Why are impure functions said to be non-composable?

I understand what pure functions are and when someone says pure functions are composable - I believe it means that the output of one function can be passed as an input to another function but same thing goes with impure functions as well, doesn't…
rahulaga-msft
  • 1,402
  • 1
  • 11
  • 24
10
votes
5 answers

Is this method pure?

I have the following extension method: public static IEnumerable Apply( [NotNull] this IEnumerable source, [NotNull] Action action) where T : class { source.CheckArgumentNull("source"); …
Thomas Levesque
  • 211
  • 2
  • 12
9
votes
4 answers

Is implicitly depending on pure functions bad (in particular, for testing)?

To extend a bit on the title, I'm trying to get to some conclusion about whether it is necessary or not to explicitly declare (i.e. inject) pure functions on which some other function or class depends. Is it any given piece of code less testable or…
DanielM
  • 217
  • 1
  • 5
1
2 3