Pure functions in EcmaScript

Hudson, Rick rick.hudson at intel.com
Sun Dec 2 10:20:03 PST 2012


Function purity, while an interesting concept, probably isn't as important as the function being associative. Our work with Parallel EcmaScript (aka River Trail) http://wiki.ecmascript.org/doku.php?id=strawman:data_parallelism has shown that for a useful set of kernel functions the compiler can conservatively determine that a function does not modify non-local variables.  We have found no need for function annotation, instead if one passes a function to a ParallelArray method we trust but verify that it modifies only local variables. Beyond this check we do not attempt to prove that a function is associative.  The results are guaranteed only to be the result of some ordering and never return an "out of thin air" value. Floating point anomalies aside, if the function is associative the result will be deterministic.

-	Rick

-----Original Message-----
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Brendan Eich
Sent: Wednesday, November 28, 2012 4:48 PM
To: Axel Rauschmayer
Cc: es-discuss
Subject: Re: Pure functions in EcmaScript

Axel Rauschmayer wrote:
> Would returning NaN be impure (apart from running the risk of it 
> having been changed to something different, globally)?

You can't say "apart from...", that's exactly the risk. It could be replaced with a global getter that has effects.

Isolating a pure function to a sandbox or prepared global environment saves this, but then the function's "pure" annotation can't be enough to express what's required.

/be
>
> On Nov 28, 2012, at 21:35 , Oliver Hunt <oliver at apple.com 
> <mailto:oliver at apple.com>> wrote:
>
>>
>> On Nov 28, 2012, at 12:25 PM, Waldemar Horwat <waldemar at google.com 
>> <mailto:waldemar at google.com>> wrote:
>>
>>> On Wed, Nov 28, 2012 at 5:39 AM, Marius Gundersen 
>>> <gundersen at gmail.com <mailto:gundersen at gmail.com>> wrote:
>>>
>>>     On Wed, Nov 28, 2012 at 1:20 PM, Andreas Rossberg
>>>     <rossberg at google.com <mailto:rossberg at google.com>> wrote:
>>>
>>>         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.
>>>
>>>
>>> You haven't addressed Andreas's point: Almost any function you write 
>>> is nonpure, including your sum example. As a fun exercise, go ahead 
>>> and write a pure version of your sum example.
>>>
>>>     Waldemar
>> Here you go:
>>
>> function sum(a, b) {
>>    var undefined;
>>    switch (typeof a) {
>>    case "number":
>>    case "string":
>>        break;
>>    default:
>>        return +undefined;
>>    }
>> switch (typeof b) {
>>    case "number":
>>    case "string":
>>        break;
>>    default:
>>        return +undefined;
>>    }
>>    return a + b;
>> }
>>
>>
>>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org> 
>>> https://mail.mozilla.org/listinfo/es-discuss
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org> 
>> https://mail.mozilla.org/listinfo/es-discuss
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de <mailto:axel at rauschma.de>
>
> home: rauschma.de <http://rauschma.de>
> twitter: twitter.com/rauschma <http://twitter.com/rauschma>
> blog: 2ality.com <http://2ality.com>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list