background preloader

Functional Programming in JS

Facebook Twitter

Trampolines in JavaScript. This description is precise and appropriate for a reference work, but it is nearly impossible to go out and read about thunks, continuation-passing style, and tail-recursion without learning about trampolining along the way.

Trampolines in JavaScript

So it is exactly how one ought to answer the question “define trampolining” on an examination, because it demonstrates that you’ve learned the subject thoroughly. But if asked to explain trampolining, a more tutorial-focused approach is called for. Let’s begin with a use case. recursion, see recursion You are asked to demonstrate that you know how to write a recursive function, perhaps in one of those job interviews where they like to take a small snippet of code and use it as an excuse to talk about programming philosophies. The challenge is to implement factorial in recursive style. You write: This creates two problems: First, we need space On for all those stack frames. Trying this in Node, I get: tail-call elimination Finally, we call factorial(0) and it returns 1. Functional-programming-in-javascript-equals-garbage. Functional Programming in Javascript === Garbage Functional Programming is great.

functional-programming-in-javascript-equals-garbage

It allows for simpler programs which are easy to test and reason about. Unfortunately, not all languages are created equal. I've been writing a bit of Clojure and really enjoying myself. As I've ported my learnings and approaches to Javascript, I've encountered a handful of issues which I'd like to talk about. Strong Preference for Immutable Data A core design goal for any Functional system is to avoid global mutable state. The solution is to not allow state to mutate (and to avoid global variables if possible). Clojure uses Persistent Data Structures, which avoids deep copying the whole structure and instead builds upon the previous data by creating a new structure that is the new data, plus the previous value.

ClojureScript, a version of Clojure which compiles to Javascript, includes Persistent Data Structures if you'd like to experiment. Recursion & Tail Call Elimination Okay, that was a long lead in. Read JavaScript Allongé. A Pull of the Lever: Prefaces “Café Allongé, also called Espresso Lungo, is a drink midway between an Espresso and Americano in strength.

Read JavaScript Allongé

There are two different ways to make it. The first, and the one I prefer, is to add a small amount of hot water to a double or quadruple Espresso Ristretto. Like adding a splash of water to whiskey, the small dilution releases more of the complex flavours in the mouth. “The second way is to pull an extra long double shot of Espresso. Foreword by Michael Fogus As a life-long bibliophile and long-time follower of Reg’s online work, I was excited when he started writing books. The act of writing is an iterative process with (very often) tight revision loops. In the case of JavaScript Allongé, you’ll find the Leanpub model a shining example of effectiveness. As a staunch advocate of functional programming, much of what Reg has written rings true to me. Enjoy. – Fogus, fogus.me Foreword by Matthew Knox –Matthew Knox, mattknox.com Why JavaScript Allongé?

Legend.