324 votes

Am I too 'clever' to be readable by Jr. devs? Too much functional programming in my JS?

In your code, you have made multiple changes: destructuring assignment to access fields in the pages is a good change. extracting the parseFoo() functions etc. is a possibly good change. introducing ...
amon's user avatar
  • 133k
227 votes

Am I too 'clever' to be readable by Jr. devs? Too much functional programming in my JS?

If you are in doubt, it probably is too clever! The second example introduces accidental complexity with expressions like foo ? parseFoo(foo) : x => x, and overall the code is more complex which ...
JacquesB's user avatar
  • 57.4k
175 votes

Why do Trampolines work?

Kevin succinctly points out how this particular code snippet works (along with why it's quite incomprehensible), but I wanted to add some information about how trampolines in general work. Without ...
Jack's user avatar
  • 4,449
152 votes

How functional programming achieves "No runtime exceptions"

How does a Function Programming, such as Elm, achieve "No runtime exceptions"? That's easy. You simply don't write functions that fail. That might sound simplistic, but that's the gist of ...
Jörg W Mittag's user avatar
105 votes

Should functions that take functions as parameters, also take parameters to those functions as parameters?

There is absolutely no reason to pass a function, and its parameters, only to then call it with those parameters. In fact, in your case you have no reason to pass a function at all. The caller might ...
user253751's user avatar
  • 4,863
105 votes
Accepted

Return considered harmful? Can code be functional without it?

If a function doesn't have any side effects and it doesn't return anything, then the function is useless. It is as simple as that. But I guess you can use some cheats if you want to follow the letter ...
JacquesB's user avatar
  • 57.4k
103 votes
Accepted

Is functional programming faster in multithreading because I write things differently or because things are compiled differently?

The reason people say functional languages are better for parallel processing is due to the fact that they usually avoid mutable state. Mutable state is the "root of all evil" in the context of ...
MichelHenrich's user avatar
92 votes
Accepted

Why do Trampolines work?

The reason your brain is rebelling against the function loopy() is that it is of an inconsistent type: function loopy(x){ if (x<10000000){ return function(){ // On this line it ...
Kevin's user avatar
  • 2,658
86 votes
Accepted

What do you call a function where the same input will always return the same output, but also has side effects?

I'm not sure about universal definitions of purity, but from the point of view of Haskell (a language where programmers tend to care about things such as purity and referential transparency), only the ...
Andres F.'s user avatar
  • 5,129
72 votes
Accepted

What is it about functional programming that makes it inherently adapted to parallel execution?

The main reason is that referential transparency (and even more so laziness) abstracts over the execution order. This makes it trivial to parallelize evaluation. For example, if both a, b, and || are ...
Jörg W Mittag's user avatar
63 votes

"Easy to reason about" - what does that mean?

To my mind, the phrase "easy to reason about", refers to code that is easy to "execute in your head". When looking at a piece of code, if it is short, clearly written, with good names and minimal ...
David Arno's user avatar
60 votes

Do you need to think about encapsulation if you can ensure immutability?

I hate how encapsulation is always framed as preventing unauthorized access. If this were the best way to think of it, immutability would indeed eliminate most of the need for encapsulation. In fact, ...
Karl Bielefeldt's user avatar
56 votes
Accepted

Why are lists the data structure of choice in functional languages?

Because lists are simpler than trees. (You can see this trivially by the fact that a list is a degenerate tree, where every node has only a single child.) The cons list is the simplest possible ...
Jörg W Mittag's user avatar
53 votes
Accepted

Do you need to think about encapsulation if you can ensure immutability?

The question Casting your question to real life: Is it okay for your doctor to post your private medical records publicly to Facebook, provided no one (other than you) is able to change it? Is it ...
Flater's user avatar
  • 45.5k
50 votes

"Easy to reason about" - what does that mean?

A mechanism or piece of code is easy to reason about when you need to take few things into account to predict what it will do, and the things you do need to take into account are easily available. ...
Michael Borgwardt's user avatar
49 votes
Accepted

Why are discriminate unions associated with functional programming?

Discriminated unions really shines in conjunction with pattern-matching, where you select different behavior depending on the cases. But this pattern is fundamentally antithetical to pure OO ...
JacquesB's user avatar
  • 57.4k
48 votes

A language based on limiting amount of arguments passed to functions

There are lots of languages which already work this way, e.g. Haskell. In Haskell, every function takes exactly one argument and returns exactly one value. It is always possible to replace a function ...
Jörg W Mittag's user avatar
46 votes

What are the functional equivalents of imperative break statements and other loop checks?

The closest equivalent to looping over an array in most functional languages is a fold function, i.e. a function that calls a user-specified function for each value of the array, passing an ...
Jules's user avatar
  • 17.6k
45 votes

Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?

No, immutable objects are quite useful in general. The first and most basic reason is that concurrency in a system doesn't require a multi-threaded application. Making say... a row in a database ...
Telastyn's user avatar
  • 109k
42 votes
Accepted

How would this be programmed in non-OO?

In FP style, Product would be an immutable class, product.setPrice would not mutate a Product object but return a new object instead, and the increasePrice function would be a "standalone" function. ...
Doc Brown's user avatar
  • 201k
41 votes
Accepted

A language based on limiting amount of arguments passed to functions

Robert C. Martin in his book "Clean Code" recommends heavily the use of functions with 0, 1 or 2 parameters at maximum, so at least there is one experienced book author who thinks code becomes cleaner ...
Doc Brown's user avatar
  • 201k
40 votes

Return considered harmful? Can code be functional without it?

Tell, Don't Ask comes with some fundamental assumptions: You're using objects. Your objects have state. The state of your objects affects their behavior. None of these things apply to pure functions....
Robert Harvey's user avatar
39 votes

What is the meaning of "doesn't compose"?

When people say "X doesn't compose", what they mean by "compose" really just means "put together", and what and how you put them together can be very different, depending on what exactly "X" is. Also,...
Jörg W Mittag's user avatar
39 votes
Accepted

Should functions that take functions as parameters, also take parameters to those functions as parameters?

If you do this long enough, you'll eventually find yourself writing this function over and over: public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3&...
Jack's user avatar
  • 4,449
38 votes

Why would I not need an ORM in a functional language like Scala?

Well, one thing that's important to do whenever we have a discussion like this is to clearly distinguish between object relational mappers ("ORM") and database abstraction layers. An ORM is a kind of ...
sacundim's user avatar
  • 4,748
38 votes
Accepted

"Remembering" values in functional programming

First of all, congratulations on "seeing the light". You've made the software world a better place by expanding your horizons. Second, there is honestly no way a professor who doesn't understand ...
gardenhead's user avatar
  • 4,729
38 votes

Is there a non-deterministic function without side effects?

Of course this depends on the definitions. Let's drop "pure" which has a definition in your question that clearly makes non-deterministic pure functions impossible as being deterministic is ...
Hans-Martin Mosner's user avatar
37 votes

Given a herd of horses, how do I find the average horn length of all unicorns?

You've pretty much covered all the options. If you have behavior that's dependent on a specific subtype, and it's mixed in with other types, your code has to be aware of that subtype; that's simple ...
Mason Wheeler's user avatar
37 votes
Accepted

What are the benefits of referential transparency to a programmer?

The benefit is that pure functions make your code easier to reason about. Or, in another words, side effects increase the complexity of your code. Take an example of computeProductPrice method. A ...
Arseni Mourzenko's user avatar
37 votes

Why are discriminate unions associated with functional programming?

Having programmed in Pascal and Ada before learning functional programming, I don't associate discriminated unions with functional programming. Discriminated unions are in some way the dual of ...
AProgrammer's user avatar
  • 10.4k

Only top scored, non community-wiki answers of a minimum length are eligible