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.
40
questions
1
vote
6
answers
396
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 ...
0
votes
1
answer
258
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 (...
16
votes
5
answers
5k
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 ...
1
vote
4
answers
442
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 ...
1
vote
2
answers
616
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 ...
7
votes
3
answers
866
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 ...
2
votes
1
answer
280
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 ...
4
votes
1
answer
500
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 ...
1
vote
3
answers
369
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 ...
10
votes
2
answers
788
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 ...
5
votes
5
answers
549
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 ...
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). ...
11
votes
2
answers
1k
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 ...
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 ...
-1
votes
1
answer
122
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 ...
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 ...
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?
...
4
votes
2
answers
743
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 ...
2
votes
2
answers
244
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) ...
9
votes
4
answers
1k
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....
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?)
28
votes
5
answers
3k
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 ...
4
votes
1
answer
222
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 ...
5
votes
3
answers
938
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;
...
2
votes
2
answers
568
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
#...
2
votes
1
answer
328
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 ...
3
votes
2
answers
400
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...
...
46
votes
7
answers
24k
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 ...
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 ...
2
votes
1
answer
524
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 ...
-1
votes
1
answer
163
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 ...
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 ...
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 ...
15
votes
3
answers
3k
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 ...
0
votes
1
answer
410
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 ...
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 ...
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 ...
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 ...
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)
...
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 ...