Pure functions in EcmaScript

Marius Gundersen gundersen at gmail.com
Wed Nov 28 05:47:56 PST 2012

On Wed, Nov 28, 2012 at 1:21 PM, David Bruant <bruant.d at gmail.com> wrote:

>  Hi Marius,
> I won't say the idea is bad, but what would be the benefit of this new
> type of function?
> From experience on this list, if a new idea cannot prove to make a major
> difference with what currently exists, it is not considered to be added to
> the ES6 spec.
> The major difference can be in performance, security, language
> extensibility, programming idioms/conveniences, etc.
> Do you have reasons to think pure functions as you propose them make that
> big of an improvement as opposed to JS as it is?

With many new functional programming possibilities (like map, reduce,
filter, lambdas) there are many scenarios where the implementer should use
pure (or as I renamed it in another reply, side-effect-free) functions.
Library implementers are likely interested in making functions that can
take lambdas as parameters, and these lambdas (in many cases) should not
alter any outside state, they should only return a value. A function with
this restriction could run faster and take up less memory since it only
needs the values passed to it as arguments in its own scope.

Mostly I feel this would introduce better coding practises with a focus on
functional programming rather than object oriented programming. Using
functions with limited scope would reduce the number of variables written
to the global scope, and would reduce the amount of state in an
application. Seeing as FP is a bit of a trend today (due, in part, to the
popularity of JavaScript), it seems to me like a good idea to implementing
features which help promote good FP patterns in a language that allows for
both FP and OOP.

> Le 28/11/2012 12:50, Marius Gundersen a écrit :
> 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;
> }
> Marius Gundersen
> _______________________________________________
> es-discuss mailing listes-discuss at mozilla.orghttps://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121128/d26c6710/attachment.html>

More information about the es-discuss mailing list