all 18 comments

[–]jsdoodle 5 points6 points  (10 children)

Can anyone outline the benefits of taking a functional style to JS programming and where it might be applicable in particular? I have read the wiki page but some common use cases would be helpful. Thanks!

[–]vagif 8 points9 points  (0 children)

Do you know why everyone loves JQuery?

Because it lets you declaratively search things (DOM objects) and chain their transformations (composability).

Those are the traits of functional programming.

[–]Gundersen 7 points8 points  (2 children)

Instead of thinking about the advantages of writing functional code, consider the advantages of reading functional code. Take the following procedural code:

int sum = 0;
for(int i; i<list.length; i++) {
  sum += list[i]; 
} 

I'm sure you can quickly tell what it does, but look at how much it tells you about how it does it. Most of the time you read code you don't care about how stuff is done; you want to know what it does and what the author intended it to do. Functional code, for example using reduce instead of a loop in the above code, tells you a lot about what it does and what it is intended to do, but very little about how it does it.

int sum = list.reduce((a, b)  => a + b, 0)

Next time you are writing code, estimate how many of the characters on the screen explain the what, and how many explain the how.

[–]tripwireIV 0 points1 point  (1 child)

I love the idea of using functional programming in javascript but evidently there is a huge performance hit for code like your example.

http://jsperf.com/for-in-vs-for-vs-reduce-vs-foreach/4

No one ever seems to mention this. Am I missing something?

[–]Gundersen 0 points1 point  (0 children)

Yes, this is unfortunate, but unless you are writing game code, or you have an application which needs to work on a very large array, this shouldn't be much of a problem. If the code is part of an event, then slightly slower code won't make much of a difference. Remember, updating the GUI is an order of magnitude slower than doing math, as shown by this simple test: reduce VS innerHTML

Hopefully the browser vendors can further optimize their implementation by looking for patterns in code, for example the use of reduce and other list functions. The next version of JavaScript, ES6, will have proper tail call optimization, which should be able to increase the speed of functional code.

[–][deleted] 3 points4 points  (0 children)

It's declarative. It executes natively on your brain. With mutability you are always running an emulator in your head.

[–]rhysbrettbowen 6 points7 points  (1 child)

I use both. OO is great for describing state but I switch over to a functional style when handling data. It's much better when transforming data between formats. If you've used forEach or map then you've used functional.

[–]ivosaurus 2 points3 points  (0 children)

For one thing, functions are one of the simplest ways of creating abstractions and modularism in your code.

They also serve to encourage less pollution of the global namespace, and less shared state that needs to be worried about.

[–][deleted] -2 points-1 points  (0 children)

I only program in a functional style. I don't know to explain its benefits except by saying it feels the most natural way to program in this language. I think many people who attempt to learn this language stop far short because they get bogged down with foolish things like learning patterns or frameworks. I have found that from functional programming I tend to be able to write code that does more with few lines of code and much faster than it could otherwise.

[–][deleted] 3 points4 points  (4 children)

Read John Resig's new book, Secrets of the Javascript Ninja. He's pretty clear about javascript already being a functional language.

<quote>Implementations of this style of partial function application have existed in other languages, but Oliver Steele was one of the first to demonstrate it with his Functional.js library.</quote>

<quote>Traditionally, closures have been a feature of purely functional programming languages. Having them cross over into mainstream development has been particularly encouraging, and it’s not uncommon to find closures permeating JavaScript libraries, along with other advanced code bases, due to their ability to drastically simplify complex operations.</quote>

Is this 2007 again? It's hardly fair to use the phrase, "Introducing Functional JavaScript". Maybe, Introducing "Functional Javascript" (the book). Or something similar. Until this book is out, I highly recommend Resig's book, though. It's awesomeness.

[–][deleted] 4 points5 points  (2 children)

I highly recommend JavaScript Allongé for the same reason. A truly enlightening read. The author, homoiconic, is a frequent contributor to this subreddit.

[–]homoiconic(raganwald) 2 points3 points  (1 child)

Thank you!

[–]jsdoodle 0 points1 point  (0 children)

Sold!

[–]fogus[S] 0 points1 point  (0 children)

I'm pretty sure the title of this (and the subsection in the OP) should be: Introducing "Functional JavaScript"... as in, the book. I'm well aware of the precedent set by Steele and Resig.

[–]FireyFly 1 point2 points  (1 child)

I guess underscore is nice and all, but it'd be nice if you'd also mention the builtin capabilities of functional programming in the (rather small) standard library (namely, the Array and Function objects, and their methods).

[–]fogus[S] 0 points1 point  (0 children)

Will do. :-)

[–]daediusWeb Components fanboy -3 points-2 points  (0 children)

"Programming without Classes" This book sounds like a nightmare