Pure functions in EcmaScript

Marius Gundersen gundersen at gmail.com
Wed Nov 28 05:39:52 PST 2012

On Wed, Nov 28, 2012 at 1:20 PM, Andreas Rossberg <rossberg at google.com>wrote:

> On 28 November 2012 12:50, Marius Gundersen <gundersen at gmail.com> wrote:
> > Has there been any work done on pure functions in EcmaScript? The way I
> > imagine it, there would be a way to indicate that a function should be
> pure
> > (by using a symbol or a new keyword, although I understand new keywords
> > aren't terribly popular). The pure function is not allowed to access any
> > variable outside its own scope. Any access to a variable outside the
> scope
> > of the function would result in a Reference Error, with an indication
> that
> > the reference attempt was made from a pure function. This also applies to
> > any function called from within the pure function. The entire stack of a
> > pure function must be pure. This also means the pure function cannot
> access
> > the [this] object. Only the parameters  passed to the function can be
> used
> > in the calculation.
> >
> > The syntax could be something like this (the @ indicates that it is
> pure):
> >
> > function sum@(a, b){
> >   return a+b;
> > }
> >
> > var sum = function@(a, b){
> >   return a+b;
> > }
> A couple of comments.
> First, your definition of "pure" is not quite correct. Any function
> that even _returns_ locally created state in some form (i.e., a new
> object), is impure.

Fair enough. A better name would probably be side effect free functions.

Second, due to the extremely impure nature of JavaScript, there aren't
> many useful pure functions you could even write. For example, your
> 'sum' function is not pure, because the implicit conversions required
> by + can cause arbitrary side effects.

Functions passed to the array methods map, reduce, filter, etc would be
good candidates for pure/side-effect-free functions. These functions
shouldn't alter any state; they should only return a new value based on the
parameter they were sent.

Last, short of a static type-and-effect system, I don't see how the
> necessary checks could be implemented without imposing significant
> overhead on almost every primitive operation -- because every
> function, and hence almost any piece of code, might potentially end up
> with a "pure" function in its call chain, and would need to check for
> that.

I'm not an implementer of EcmaScript, so I don't have deep knowledge of how
this could be implemented. I would imagine that a subset of a true pure
function, where the only restriction would be that only variables passed as
arguments to the function exist in the scope, would be relatively easy to
implement. Wouldn't this be a faster implementation than todays functions,
which have to keep track of scope? These side-effect-free functions would
only need to contain the variables passed as parameters in their scope.
Accessing anything outside the scope would result in a reference error.

As you see this is obviously a subset of the pure function as it usually is
defined. The function is allowed to modify any object passed to is an
argument (since, as you point out, it would be difficult to protect against
this), and it is allowed to return new state.

> /Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121128/a2c4dda9/attachment.html>

More information about the es-discuss mailing list