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.
42 questions
11votes
4answers
1kviews
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 ( ...
0votes
1answer
227views
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 ...
1vote
5answers
833views
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 ...
0votes
1answer
536views
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 (...
17votes
5answers
6kviews
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 ...
3votes
4answers
1kviews
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 ...
1vote
2answers
819views
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 ...
10votes
4answers
1kviews
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 ...
2votes
1answer
402views
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 ...
4votes
1answer
534views
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 ...
1vote
3answers
403views
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 ...
10votes
2answers
857views
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 ...
5votes
5answers
597views
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 ...
53votes
6answers
7kviews
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). ...
11votes
2answers
2kviews
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 ...