On Nov 28, 2012, at 1:11 PM, David Bruant <[email protected]> wrote:
> Le 28/11/2012 21:35, Oliver Hunt a écrit : >> >> On Nov 28, 2012, at 12:25 PM, Waldemar Horwat <[email protected]> wrote: >> >>> On Wed, Nov 28, 2012 at 5:39 AM, Marius Gundersen <[email protected]> >>> wrote: >>> On Wed, Nov 28, 2012 at 1:20 PM, Andreas Rossberg <[email protected]> >>> 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; >> } > I don't even... Reading this makes me understand why "return a+b" didn't work > and... oh well... JavaScript is quite a language... my favourite bit in all of this was var undefined; ... return +undefined; Although i realise that 0/0 is possibly going to be a more efficient way to get NaN :D > > David
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

