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

42 questions
53
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,467
48
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…
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
  • 110,259
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,215
18
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
  • 4,529
18
votes
4 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? …
17
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…
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
  • 570
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
4 answers

Is it OK for a function to both do-something and return-something?

Is it OK for a function to both do-something and return-something? I try to avoid it for the most part, but there are situations where trying to avoid it would lead to worse/less clean code. Ex: if ( false == read_from_file( file, dest_buffer,…
Newline
  • 221
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
  • 4,529
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
  • 957
10
votes
4 answers

Functional architecture with lots of I/O

I'm learning about "Functional Core, Imperative Shell" as espoused by Gary Bernhardt in his talk about "Boundaries". In reality, it seems like these ideas have been known for a long time, and put into practice with languages like Elm or Haskell.…
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…
1
2 3