
Introducing "Functional JavaScript" - jashkenas
http://blog.fogus.me/2013/03/20/fun-js/
======
bsimpson
Is it prudent to utilize the functional style in a language that isn't tail
call optimized? Doesn't that create a lot of garbage for the GC?

~~~
alexfarran
You can use a trampoline like the one the author has written in this library
[https://github.com/fogus/lemonad/blob/master/lib/lemonad.js#...](https://github.com/fogus/lemonad/blob/master/lib/lemonad.js#L1089)

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.

~~~
snappy173
or rather than creating an iterator yourself, use setTimeout(..., 0) to put
the next call on the event loop.

~~~
fogus
I talk about that too. :-)

------
k3n
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!

~~~
petercooper
Give it an hour and this link will be retitled "fun.js" by the mods..

~~~
k3n
Been around here awhile, but mostly on the fringes, so I'm not sure what you
mean by that...?

~~~
petercooper
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.)

~~~
k3n
I see, thank you for the explanation.

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)

------
jimbokun
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.

------
nailer
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.

~~~
bsimpson
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."

------
weareconvo
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.

~~~
cousin_it
Code reuse also seems like an intermediate goal to me...

~~~
gngeal
_"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?

~~~
chc
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.

~~~
weareconvo
A good point, well-made. I would edit my post, but I don't have much to add to
your reply.

------
kailuowang
What I really miss when doing "functional JS" is pattern matching. Is there
any good solution for that?

~~~
coldtea
You might try this one:

<http://www.bramstein.com/projects/funcy/>

It implements pattern matching in JS, with objects and closures. Not as
convenient as first class support, but might be worth it.

~~~
bsimpson
That link is overloaded. Try this one:

<https://github.com/bramstein/funcy>

------
hilko
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.

------
GhotiFish
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.

~~~
raganwald
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:

<https://news.ycombinator.com/item?id=5409626>

~~~
GhotiFish
That was a good analysis of the problem. I'll give it some thought.

------
GeZe
For those who don't want to wait for the book, I've written a brief article
covering some concepts of functional programming in JavaScript:
[http://livescript.net/blog/functional-programming-in-
javascr...](http://livescript.net/blog/functional-programming-in-javascript-
using-livescript-and-prelude-ls.html)

It covers first class and higher order functions, currying, partial
application, composing functions, using operators as functions,
comprehensions, and immutability.

------
pepijndevos
For the German speakers amongst you, a friend of mine also just submitted her
book about functional Javascript for review at O'Reilly:
[http://www.amazon.de/Das-Curry-Buch-Funktional-
programmieren...](http://www.amazon.de/Das-Curry-Buch-Funktional-
programmieren-JavaScript/dp/386899369X/)

------
pasbesoin
fogus,

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.

------
gosukiwi
Where can I suscribe so I know when it's released?

