Knowing the limitations of your runtime is important. My book is not a dogmatic application of functional programming in spite of JavaScript, but instead an exploration of the functional techniques that complement JavaScript. Tail calls make an appearance, but they are more broadly applicable than their use in the presence of TCO.
Basically you rewrite your recursive functions so that instead of calling another function they return a parameterless closure. Then you can iteratively call your first function, and the function it returns, and the function it returns etc until the return value is not a function.
Tail calls are not as much about "deep recursion" as they are about continuation-passing style, compositionality, and similar things.
It's like with lazy evaluation. It allows you to write novel kinds of algorithms and data structures, and you can emulate it with helper objects or structures, but it's tedious, muddles the essence of the code, and the implementation without them (the helper structures) would be simpler if the language provided you with lazy expressions.
In other words, repeat with me: loops don't compose. They're not first-class, they're not extensible, they're a syntactic construct.
Functional purity would be nice, but TCO not strictly necessary to be program very functionally. For instance, here TCO would be necessary to prevent the stack from being over-run:
lack of TCO doesn't mean it's a little slow, lack of TCO means that naive use of classic functional patterns blows the stack for big lists/structures/arguments.
By "classic functional patterns" you mean recursion. There are other ways to implement functions that are "classically functional" using techniques other than recursion, that do not blow the stack. I'll talk about each and their tradeoffs.
It's not just recursion, it's calls in general, I guess. (There are other ways of being space-inefficient in functional programs, see the odd versus even streams issue in Scheme.) Are you going to mention tail calls in ES6 in your book, BTW?
I realize that JS is becoming more popular for various tasks -- not always relating to the web -- but still, I kind of doubt that 99% of the JS out there is going to run up against these types of problems. Perhaps something like Uglify.js or similar might, but then again, if you're implementing a parser (or any other static code analyzer/tool) then you're probably going to avoid most libs to start with.
Yes, in particular, asking people to think about the input and output to a particular piece of code is really important.
Pulling apart the iteration over a collection of elements and behavior performed to each element is one of those insights that a functional style is good at communicating, and leads to better more testable code.
I think that because TCO is all but inevitable in future engines/standards, starting to use functional patterns now for the programming benefits is worthwhile.
Seeing as the page's title has "fun.js", I thought this was yet another 'functional JS' lib... but I was pleasantly surprised to see that this is actually a book that covers different aspects of FP within JS.
Now I'm looking forward to reading it!
HN policy used to be that you could give items customized/more appropriate titles if the page you were linking to had a bad title.
In the last year, this guideline was removed from the guidelines page and mods have been renaming items "back" to the actual title of pages being linked to. So this item should be "fun.js" and now the useful title it is now. I'm surprised it hasn't happened yet because there have been a ton of examples in the past several months.
(If you couldn't guess, I think the change in policy is ridiculous.)
I have mixed feelings about it; on one hand, if an author titles a piece of work, then in the interest of honoring their creative expression (e.g. their work) then it'd be best to use their titles. Except, of course, when it's a poor title. On the other hand, authors aren't always the best about naming articles, and/or there's context that's present on their site that might produce a title that's out of context when lifted wholesale for use on another site.
For this article, I think "fun.js" is particularly poor, as it implies that it's about some coding project. Of course, the ecosystem has been tacking ".js" onto anything even remotely related to JS, and so it's not all the author's fault, but as a HN title it'd be misleading. IMO the 'best' title here would be akin to a reference, such as:
"Functional Javascript: Introducing Functional Programming with Underscore.js" by Michael Fogus (O'Reilly)
I like the idea of separating a programming paradigm from actual programming languages. Obviously, certain languages lend themselves to certain paradigms, but it's nice to see a book explaining the concepts of functional programming in a language that doesn't enforce all of the tenants of that paradigm by default.
For the second edition, I think focusing on ES5 wherever something is actually included in JavaScript would be a huge improvement. Underscore.JS duplicates huge parts of ES5's own work for the benefit of older ES3 environments like IE8, Firefox 3 etc.
Though I agree, there are only a handful of Array.prototype methods, and I believe Underscore falls back to them when they are available. I imagine you could read fogus's work, then substitute where appropriate if you're already well-versed in JS. "I know I can use [].forEach instead of _.each."
In all of this, let us never forget: "Functional Programming" is a great lens through which to look at a large class of problems... but it is never the goal. Code re-use is the goal. FP and OOP are just means to that end.
If you want to be precise about it, "the goal" is specific to each human being. I would hope it's something like "Make the world a better place" though.
"Code re-use is the goal. FP and OOP are just means to that end."
I think that both of you are right, to some extent. Code reuse is a goal, because it's better than writing everything anew every time you need some functionality. But it's not a long term goal, because sooner or later, it turns out that the code you have is insufficiently abstract, and you have to replace it with something more generic. I don't think that the level of genericity and abstraction available in mainstream languages is up to the task of making long-time code reuse an appropriate goal. VPRI-style "'runnable math' specifications" seem like the way to go, but how many people program that way?
Code reuse isn't the point of OOP. It may be a goal you personally have when you're writing code in OOP style, but AFAIK it was not Alan Kay's overriding goal when he coined the term. It was more about promoting clean, well-factored program designs. Reuse just happens to fall out of that more readily than bad program designs.
I edited my post to make it clear that that was a quote. :-) I also think that in case of OOP, it's an incidental outcome. (Efficient code reuse clearly needs more than just to have your program have a message-passing based architecture.)
I've been reading up a lot on functional programming, and by know I think I get most of the principles and techniques.
What I'm mostly struggling with is how to structure my code within this paradigm. While this struggle is not limited to FP, I'd be very curious to find resources that don't just show code snippets, but explain the greater architecture of FP implementation.
I find these topics to be fun, and I want to use them. However, I can't help but feel that if I use these design patterns, they might unintentionally obfuscate my intentions to my peers.
I have a code base, and I think my priority should be to remains as consistent with that code base as possible.
Am I wrong? Is this book just for entertainment? There's nothing wrong with that of course.
I ended up writing a blog post to answer your excellent question. Please let me know if you feel I shouldn't have quoted you or if the post could be improved in any way:
It covers first class and higher order functions, currying, partial application, composing functions, using operators as functions, comprehensions, and immutability.
This may be too political (as well as OT) a question for you to respond, but I see that this is an O'Reilly book. Does this mean that O'Reilly... um, "has some life left in them"?
I guess I'm hoping so, and looking for some encouraging words from your own experience doing this book with them. Or criticism, if it's justified and you are free to say. (There used to be some O'Reilly people on here, I've noticed, so keep that in mind...)
I like Tim and like the older books. Some more recent issues have given me pause.
P.S. Oh, I see the book isn't out, yet. Maybe I should ask this later.
Also, I didn't initially consider that this could start another "O'Reilly" conversation, in depth. Rather, I'm interested in first-hand experience.