Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
145 views

Function objects with no state shouldn't be object oriented? [closed]

My question relates to this topic here: Are classes with only a single (public) method a problem? There I read in the comments often something like that: It is no longer object oriented. Because ...
Robin Kreuzer's user avatar
1 vote
5 answers
544 views

Should I repeat calculations twice to follow "return a value or have side-effects, but not both"?

According to Origin of "a method should return a value or have side-effects, but not both", a method should either return a value or have side-effect, but not both. However, I often see some ...
wcminipgasker2023's user avatar
0 votes
1 answer
383 views

Making side effects explicit even in non-pure functions

I try to have as many pure functions as possible, but if I can't, I at least try to make the side effects as explicit as possible. Here is an example (in Go) type State struct { count int } func (...
AndreaL's user avatar
  • 119
16 votes
5 answers
6k views

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 ...
Helin Wang's user avatar
3 votes
4 answers
714 views

Is the combination of state machine and pure function a programming style?

As a university student who just have been learning programming for a year. After I learned about the concept of state machine and the pure function in functional programming, I suddenly got an idea ...
Equescript's user avatar
1 vote
2 answers
748 views

What's the difference between a pure function that expects a complex object of a particular type and object oriented programming?

What's the difference between writing OO code that depends on internal state and writing a pure function that expects an argument that is a data structure of a specific type (and thus has internal ...
Andrew's user avatar
  • 119
8 votes
3 answers
1k views

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 ...
Maletor's user avatar
  • 189
2 votes
1 answer
322 views

Can I enforce "functional core, imperative" shell with a framework?

The design pattern known as "functional core, imperative shell" is about separating side-effects from pure calculations, where business logic is supposed to be pure and then coordinated by ...
Olle Härstedt's user avatar
4 votes
1 answer
519 views

Is the "Substring" method pure (in C#)? If yes, why isn't it annotated as [Pure]?

The Substring method of C#'s string class (which is also available in every other .Net language) does not seem to alter state and will always return the same value, given the same arguments. So is it ...
user85128's user avatar
1 vote
3 answers
385 views

what it means when someone says - "statements/instructions are not composable"?

I have been using c# and trying to learn FP. In context of FP I often hear that usage of basic assignment or return statements are not considered composable, hence their usage is not advised in FP ...
rahulaga-msft's user avatar
10 votes
2 answers
833 views

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 ...
rahulaga-msft's user avatar
5 votes
5 answers
565 views

Implicit reading/writing of state in OOP hurts readability, maintainability, and testability. Good way of mitigating this damage?

OOP makes state reads and writes implicit. For instance, in Python: class Foo: def bar(self): # This method may read and/or write any number of self.attributes. # There is no way ...
Dun Peal's user avatar
  • 159
52 votes
6 answers
7k views

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). ...
callum's user avatar
  • 10.4k
11 votes
2 answers
2k views

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 ...
gaazkam's user avatar
  • 4,459
18 votes
6 answers
3k views

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 ...
gaazkam's user avatar
  • 4,459
-1 votes
1 answer
130 views

Why are ReactiveX Operators considered functional?

I'm struggling to understand how a ReactiveX operator can be considered functional. Operators are implemented as functions, but but with the exception of simple operators like map and reduce many of ...
Brian Flynn's user avatar
12 votes
4 answers
2k views

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 ...
rahulaga-msft's user avatar
4 votes
1 answer
2k views

Naming conventions for pure functions

Are there 'conventions' / best practices on naming pure functions? For example: adding numbers: add or sum? calculating the square root: calcSqrt or sqrt? reversing a list: reverse or reversed? ...
Willem-Aart's user avatar
4 votes
2 answers
748 views

This functional programming example and what it means

I've been reading Out of the Tar Pit by Ben Moseley and Peter Marks and in section 5.2.3 they discuss state in functional languages compared to procedural languages. The procedural example is as ...
Karl Uibo's user avatar
  • 153
2 votes
2 answers
245 views

Functional programming: does using a generic make a function impure?

public static Func<string, Task<T>> MyMethod<T>( UserCredentials credentials, Func<string, string, string, Task<T>> func ) => async (value) ...
Yatrix's user avatar
  • 337
9 votes
4 answers
2k views

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....
DanielM's user avatar
  • 217
8 votes
2 answers
2k views

Are all deterministic functions free of side-effects (and vice versa)?

I'm reading about pure-functions in functional programming and am wondering, whether a function being deterministic implies that the function is also side-effect free? (and vice versa?)
user66875's user avatar
  • 191
28 votes
5 answers
4k views

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 ...
Rock Anthony Johnson's user avatar
4 votes
1 answer
228 views

Is anything gained by making dependencies explicit via function argument lists when implementing pure methods?

This question is followup to this question. Is there any benefit in avoiding the 'this' operator when implementing pure methods? That is, are there any advantages to making all dependencies explicit ...
Rock Anthony Johnson's user avatar
5 votes
3 answers
989 views

Can a function be pure if it depends on an immutable instance field?

In the following c# code example, the instance field [name] is readonly, and therefore is immutable after class construction. public sealed class Example { public readonly string name; ...
Rock Anthony Johnson's user avatar
2 votes
2 answers
604 views

Simulated functional programming in C -- passing the entire program state as a function argument

I have a struct called State which holds all the variables for the program. Rather than being modified by functions directly, it is the value returned. Here is some code: #define USERNAME_LENGTH 20 #...
ridthyself's user avatar
2 votes
1 answer
336 views

Pure functions and the outer scope [duplicate]

Excuse my ignorance, I come from the C family of languages but zero exposure to functional languages. I've read that pure functions only generate output based on a given input. Same input gives the ...
Joseph's user avatar
  • 657
3 votes
2 answers
414 views

Programatically determine that some functions are pure

I saw this question that shows it is impossible to programatically determine if a javascript function is pure, but is it sometimes possible to affirm that a function is pure - so something like... ...
Billy Moon's user avatar
47 votes
7 answers
25k views

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 ...
m0meni's user avatar
  • 793
2 votes
4 answers
3k views

What is Big-O notation for purely functional languages?

Is it still relevant? Instead of var result = new List<int>(); for (var i = 0; i < prev.Count; ++i) { result.Add(prev[i] * 2); } where the result.Add, prev[i], and * 2 instructions are ...
Joshua Grosso's user avatar
2 votes
1 answer
542 views

Enumerating the primitive recursive functions

How can I enumerate (by expression tree size, for example) all of the primitive recursive functions that map natural numbers to natural numbers in a traditional programming language like C? For ...
user76284's user avatar
  • 123
-1 votes
1 answer
165 views

Does pure functional programming become agility impediment? [closed]

While I like benefits of strong typing system, there is one thing that worries me the most. I think of strong type system as means of forcing design choices. If a team builds a system unaware of its ...
sevo's user avatar
  • 621
17 votes
5 answers
4k views

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 ...
Dancrumb's user avatar
  • 571
6 votes
4 answers
5k views

Why shouldn't pure functions always be static and public? [duplicate]

What are the reasons, technical or otherwise, that pure functions (functions which do not rely on external state and have no side effects) should not always be public and static? When a static and ...
PandemoniumSyndicate's user avatar
17 votes
4 answers
4k views

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 ...
Wayne Conrad's user avatar
  • 1,138
0 votes
1 answer
417 views

How to encourage to write pure functions in imperative environments? [closed]

In functional programming languages, a developer is always cautious about writing pure functions. Some functional languages simply do not allow impure functions, and some other functional languages ...
Gulshan's user avatar
  • 9,452
8 votes
2 answers
2k views

Alternative to language purity

Purity One of the interesting concepts in Haskell is the purity. However, I am wondering what the pragmatic reasons behind this is - let me explain a bit more before you reject my question. My main ...
nilu's user avatar
  • 1,024
27 votes
4 answers
3k views

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 ...
Telastyn's user avatar
  • 109k
19 votes
2 answers
11k views

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 ...
p.s.w.g's user avatar
  • 4,175
10 votes
5 answers
5k views

Is this method pure?

I have the following extension method: public static IEnumerable<T> Apply<T>( [NotNull] this IEnumerable<T> source, [NotNull] Action<T> action) ...
Thomas Levesque's user avatar
11 votes
5 answers
3k views

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 ...
Oni's user avatar
  • 957