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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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....
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,...
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&...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
functional-programming × 737haskell × 99
object-oriented × 92
programming-languages × 60
javascript × 47
design × 43
c# × 38
scala × 37
java × 36
design-patterns × 35
f# × 32
python × 29
functions × 29
immutability × 29
paradigms × 26
algorithms × 24
pure-function × 23
clojure × 22
monad × 21
data-structures × 20
side-effect × 20
imperative-programming × 20
object-oriented-design × 19
c++ × 18
programming-practices × 18