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.

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
293 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
944 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
604 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
851 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
445 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
564 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
453 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
890 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
634 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,529

15 30 50 per page