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
1 vote
2 answers
324 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 ...
user avatar
  • 119
7 votes
3 answers
529 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 ...
user avatar
  • 179
1 vote
1 answer
161 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 ...
user avatar
4 votes
1 answer
448 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 ...
user avatar
1 vote
3 answers
324 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 ...
user avatar
9 votes
2 answers
665 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 ...
user avatar
5 votes
5 answers
483 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 ...
user avatar
  • 159
51 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). ...
user avatar
  • 10.2k
10 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 ...
user avatar
  • 3,307
17 votes
6 answers
2k 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 ...
user avatar
  • 3,307
-1 votes
1 answer
109 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 ...
user avatar
11 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 ...
user avatar
4 votes
1 answer
1k 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? ...
user avatar
4 votes
2 answers
710 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 ...
user avatar
  • 153
2 votes
2 answers
235 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) ...
user avatar
  • 327
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....
user avatar
  • 217
6 votes
2 answers
1k 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?)
user avatar
  • 171
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 ...
user avatar
4 votes
1 answer
219 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 ...
user avatar
5 votes
3 answers
846 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; ...
user avatar
2 votes
2 answers
502 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 #...
user avatar
2 votes
1 answer
310 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 ...
user avatar
  • 637
3 votes
1 answer
288 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... ...
user avatar
45 votes
7 answers
20k 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 ...
user avatar
  • 753
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 ...
user avatar
2 votes
1 answer
477 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 ...
user avatar
  • 123
-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 ...
user avatar
  • 621
17 votes
5 answers
3k 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 ...
user avatar
  • 571
6 votes
4 answers
4k 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 ...
user avatar
13 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 ...
user avatar
  • 1,098
0 votes
1 answer
374 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 ...
user avatar
  • 9,222
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 ...
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 ...
user avatar
  • 107k
19 votes
2 answers
10k 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 ...
user avatar
  • 4,095
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) ...
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 ...
user avatar
  • 917