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 ...
176
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 ...
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 ...
64
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 ...
54
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 ...
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. ...
40
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&...
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....
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
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 ...
37
votes
Accepted
How do compilers work in a language that doesn't allow recursion?
Recursion can only be programmed either by having a call to function A within the definition of A itself (direct), or by having function A call function B, and function B call function A (indirect). ...
35
votes
How do the type systems in functional languages differ from those in OO languages?
tl;dr summary: they are more powerful, more expressive, and most importantly, they don't lie.
[Aside: I would like to point out that there is a double false dichotomy in the OP's question.
FP and OO ...
34
votes
"Remembering" values in functional programming
This is really just an addendum to gardenhead's answer, but I'd like to point out there's a name for the pattern you're seeing: folding.
In functional programming, a fold is a way to combine a series ...
34
votes
Do compilers like Javac automatically detect pure functions and parallelize them?
are compilers such as Javac smart enough to detect when a method is a pure function.
It's not a question of "smart enough". This is called Purity Analysis and is provably impossible in the general ...
33
votes
Accepted
What is the benefit of having "no runtime errors", like Elm claims?
Exceptions have extremely limiting semantics. They must be handled exactly where they are thrown, or in the direct call stack upwards, and there is no indication to the programmer at compile time if ...
33
votes
What are the functional equivalents of imperative break statements and other loop checks?
You could easily convert it to recursion. And it has nice tail-optimized recursive call.
Pseudocode :
public int doSomeCalc(int[] array)
{
return doSomeCalcInner(array, 0);
}
public int ...
33
votes
Do you need to think about encapsulation if you can ensure immutability?
Encapsulation could mean that you hide the actual storage of immutable data.
E.g.:
class Color
{
private readonly uint argb;
public byte Blue => (byte)(argb & 0xFF);
public Color(byte ...
33
votes
Accepted
Definition of "functor"; Haskell vs. C++
The two meanings are unrelated.
The Haskell community (and really the Functional Programming community in general, and even the general programming community beyond FP) uses the term Functor in the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
functional-programming × 742haskell × 99
object-oriented × 93
programming-languages × 60
javascript × 50
design × 43
c# × 38
scala × 37
java × 36
design-patterns × 36
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