Skip to main content

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
11 votes
4 answers
1k views

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 ( ...
Newline's user avatar
  • 221
0 votes
1 answer
338 views

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
972 views

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
616 views

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
18 votes
5 answers
6k views

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
4 votes
4 answers
1k views

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
872 views

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
10 votes
4 answers
2k views

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
  • 209
2 votes
1 answer
452 views

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
574 views

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
480 views

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
912 views

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
669 views

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
53 votes
6 answers
7k views

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.5k
11 votes
2 answers
2k views

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,549
18 votes
6 answers
3k views

"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,549
-1 votes
1 answer
169 views

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
3k views

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
3k views

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
790 views

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
1 vote
2 answers
292 views

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

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

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

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
269 views

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
1k views

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
676 views

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
364 views

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
  • 667
3 votes
2 answers
496 views

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
48 votes
7 answers
28k views

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
  • 803
2 votes
4 answers
3k views

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 ...
Josh Grosso's user avatar
2 votes
1 answer
606 views

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
190 views

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

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
  • 570
6 votes
4 answers
5k views

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
18 votes
4 answers
5k views

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,148
0 votes
1 answer
467 views

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,552
8 votes
2 answers
2k views

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

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
  • 110k
19 votes
2 answers
12k views

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,215
10 votes
5 answers
6k views

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

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