12 ms·
Is your JavaScript function actually pure?
- automatwon 10y agoIf we want to be pedantic, there is no such thing as a pure JavaScript function. If the stack is full, the invocation of ANY function will overflow the stack. That's a side effect to the outside world. Pure functions exist under Plato's Theory of the Form conception of the world, but not on a fixed and finite physical computer. http://www.johndcook.com/blog/2010/05/18/pure-functions-have-side-effects/ http://www.johndcook.com/blog/2010/05/18/pure-functions-have... If we don't want to pedantic, then a fraction of the 74% of who took the function as 'pure' probably took this to mean being equivalent to referential transparency. That's good enough, and I believe the author would agree. I hope we can start shifting our discussions from 'is this pure?' to 'Does this function behave like a Mathematical Function in all the situations I will encounter in my code?' It's a good exercise to think about assumptions, but we shouldn't be so paranoid as to check if native JS methods, namely Array.isArray, have been overridden (unless security is a concern). When we speak about the Fibonacci sequence, it's nonsensical to talk about f("pie"). The domain being integers is implied in the semantics of the function. In the veins of "keep an open mind but not so open that your brain falls out", I propose "keep writing code with pure functions, but not so pure that your code becomes cluttered."
- wldcordeiro 10y agoI ran into this article a little while ago and I had a similar sentiment. I get the point staltz is making but it felt pedantic and heavy handed, hell you can ignore JavaScript entirely and the same is true for just about every programming language. Seems that there is a trend towards ideology in the JavaScript community lately that bothers me. People obsess over pure functions and FRP and talk so highly about these things like mutation and non-FRP are the worst things on the planet when it comes to code.
- oldmanjay 10y agoThe trouble with trying to apply words like "community" to millions upon millions of people with basically nothing but a surface connection (like they all have used javascript, for instance) is explained nicely in Vonnegut's "Cat's Cradle"
- philh 10y ago"The JavaScript community" need not include everyone who has used javascript. Why do you assume GP was intending it to do so?
- oldmanjay 10y agoI made only one assumption - that "the JavaScript community" is a granfalloon. Any further assumptions you assume I made are not valid.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- dustingetz 10y agoyes we run on imperative hardware, a pure functional language can run out of memory too (but I agree that Staltz's stance is unhelpful)
- wongarsu 10y ago>Pure functions exist under Plato's Theory of the Form conception of the world, but not on a fixed and finite physical computer In the same vein you can say that C is a Turing complete language (i.e. you can use C to simulate a Turing machine), so there can be no complete implementation of C on a computer because no computer can simulate a Turing machine (since real computers are only state machines, which are inferior to Turing machines). When talking about computer-science terms like "pure function" or "turing complete" things tend to stay a lot more sane if you just assume that even in the real world you will not run out of memory (just as a lot of programming stays more sane under that assumption).
- amelius 10y ago> If we want to be pedantic, there is no such thing as a pure JavaScript function. If the stack is full, ... Under this definition, Haskell has no pure functions either.
- jrockway 10y agoI think that's a fine assertion to make, because Haskell functions can die at runtime because of pattern match failure or an explicit evaluation of "bottom" (undefined). Prelude> undefined *** Exception: Prelude.undefined Prelude> let x 2 = 4 in x 1 *** Exception: <interactive>:7:5-11: Non-exhaustive patterns in function x Neither of these behaviors is indicated in the type definition: Prelude> :t undefined undefined :: t Prelude> :t let x 2 = 4 in x let x 2 = 4 in x :: (Eq a, Num a, Num a1) => a -> a1 I was promised a -> a but got a runtime exception instead.
- tiagobraw 10y agoisn't bottom part of any data type, including a?
- jrockway 10y agoIt is, yes, but people often proclaim "haskell has solved the 'null' problem", and it hasn't because bottom is the exact same thing as null.
- tome 10y ago> bottom is the exact same thing as null No it's not because you can't check for bottom (purely) and thus you can't use it as a flag or sentinel value.
- wyager 10y agoYou can get full pattern match checking via -fwarn-incomplete-patterns. Crashing on bottom is a concession to the fact that crashing is more useful than infinite loops. The semantics of the program exiting are the same as the semantics of the program infinite looping. So I'd say it's as pure as you can get on a finite state computer.
- dottedmag 10y agoAs a matter of fact, if your JS code is running on a pages where other pieces of code could be loaded, then it will regularly encounter native JS methods being overridden (Prototype.js is the worst offender so far).
- msl09 10y ago>When we speak about the Fibonacci sequence, it's nonsensical to talk about f("pie"). Indeed it is, but I think you are taking his example literally. That problem can appear in more subtle ways. Consider some other examples: f(string) Does f works for every string? What if it's unicode. What if it's an empty string? What if it's a sequence type(like an iterator over a stream) that returns the letters of a string? mean(numbers) What is the biggest number I can pass? If I pass a list of integers does it return a float or an integer? If I pass an empty list, does it return 0, -1, null, undefined? There are several situations on which you can encounter similar situations. I do not disagree that all that type information has a cost, your type signature can become more complicated, your cognitive load can be increased, your code can be less flexible, it can be harder to integrate with existing technologies, but since the problem still exists I think it's very reasonable that the author is trying to keep that discussion alive.
- klodolph 10y agoThe conventional definition of "pure" is that the result of the function is the same for the same input. You might include "throws an exception" as a possible "result" for a JavaScript function, or you might not (generally not). So the fact that your implementation of mean() returns a float when you expected an int (honestly, there are no ints in JavaScript, just floats which happen to have integer values, why would you expect that?) or returns undefined doesn't mean that the function is impure, it just means that the function doesn't do what you thought that it should do. For example, I can write the function: function addOne(x) { return x + 1; } I think there is no conceivable generally useful definition of "pure" under which this function is not pure, and yet... it could overflow the stack, it could run out of heap memory if you pass a string depending on how much heap memory is available... The generally useful definition of "pure" is just that if f is pure, I can save y = f(x) and use y instead of f(x), or vice versa, or I can eliminate f(x) completely if I don't need the result. The fact that extreme corner cases, like out-of-memory situations, can result in slightly different behavior is something that we like to gloss over.
- agrafix 10y agoIf you compare Haskell with JavaScript, all JavaScript functions would run in the `IO` monad because you can always (even implicitly as in the article) run an impure action even if everything else seems pure. In Haskell, if your function is not monadic, you can't "run" the monadic action. On the other hand, you don't see exceptions in Haskell's type signatures and you can throw them in pure code. Hm! ;-)
- bbcbasic 10y agoSorry... in Haskell there is performUnsafeIO. Nothing in Haskell is therefore guaranteed to be pure! Hello Downvoter: I know it is silly but the article is talking about equally silly JS by overriding valueOf.
- masklinn 10y agoThe question is asked providing the entire implementation of the respective sum functions in terms of primitives. There's no room for unsafePerformIO in the Haskell code (assuming prelude's `(+)` and `foldl`). There could be if the Haskell version was typed on `Num a`, but it's typed on `Int`, which is a standard type with a known pure Num instance.
- bbcbasic 10y agoOk sure but I was meaning in general. Anyway pass in (repeat 0) and you get a side effect. :-)
- masklinn 10y ago> Ok sure but I was meaning in general. So was I, the article denotes that JS functions can be impure because values carry a "purity stigma" (the value itself can embed impure operations in its manipulation or evaluation). > Anyway pass in (repeat 0) and you get a side effect. :-) That's a good point, laziness also carries a "purity stigma". You could actually sneak in unsafePerformIO with an unfold or a lazy list construction.
- tel 10y agoHaskell is well-known not to be pure in supporting non-terminating computation.
- lmm 10y agoSure, that's an issue with Haskell - much better to use Idris.
- yyhhsj0521 10y ago
- svalorzen 10y agoThe assumption in this article is that as long as a line of code as the same characters, its output will be the same. But this makes no sense. Consider: var arr = [0,1,2] sum(arr); arr = [1,2,3]; sum(arr); The two sums will have different results. What the article does is in essence calling sum([Math.Random(), Math.Random(), Math.Random()]); sum([Math.Random(), Math.Random(), Math.Random()]); I don't see why one would expect the output of such a call to be always the same, if you are in all effect changing the input. Even if the characters you typed are the same, if the values that are passed around change the result of a function call will change. "Pure" is a property with regards to the values passed to a function, not to the characters typed to call it.
- philplckthun 10y agoI believe as well that the article is imprecise, since it elaborates on a trick question and tries to just inform about the term "pure" being hard to define, especially in a dynamic language. To be referencing an exact quote of the article to make it very clear: var arr = [{}, {}, {}]; arr[0].valueOf = arr[1].valueOf = arr[2].valueOf = Math.random; Obviously this relies on multiple function calls, so the output of the original function `sum` is still predictable. I think predictability and the absence of side effects is still a much better definition for pure functions. It's also a necessary definition in JS. Saying that 'the same call of the function with similar enough arguments will always yield the same output' is not a sufficient definition and way too simplified.
- mthq 10y agoThe author uses the definition that a pure function must return identical outputs on identical inputs. I actually think a better definition is that the output of a function cannot depend on anything but it's inputs. If we use that definition then the first example he gives where the `sum` function violates pureness is actually not a violation. This is also justified by interpreting a `.valueOf = Math.random` field as a random variable, making the procedure `sum` a map from a random variable to a random variable.
- agd 10y agoI think it's commonly agreed that pure functions depend only on their inputs AND cannot have side effects. So when introducing 'Math.random' the function would be impure as it has side effects.
- grenoire 10y agoIs it then the variable (or the variable accessor) that is impure, or the function that is impure?
- 14113 10y agoThe variable accessor is a function of type Variable -> Field, which the function sum is calling. Because of that, the function sum is impure, as it's calling an impure function. I would argue that the object is just a wrapper around a function, and in essence making the sum function implicitly higher order, as you're passing it a function to retrieve a value, rather than an actual value.
- nbevans 10y agoMath.random doesn't necessarily have side affects. If it's storing a seed value, and incrementing that each time; then yes it stores mutable state which is a side affect because future invocations of that function will then depend upon that pre-existing state. If however Math.random is passively listening for background radiation through some sensor perhaps then it doesn't really have side affects does it? Nor does it have mutable state, for that matter. But it would still be impure according the true definition of what is a pure function, I believe.
- chriswarbo 10y agoReposted from https://news.ycombinator.com/item?id=12530652 https://news.ycombinator.com/item?id=12530652 I disagree with some of the claims, as I think these sorts of edge-cases mostly serve to highlight how imprecise the given pure/impure distinction is, and how we can't always take a semantic concept from one language (e.g. Haskell) and apply it in another (JS). For example, if you always get "TypeError" when calling "sum()", you can replace "sum()" with its result ("throw TypeError") and get the same behaviour; it doesn't break referential transparency, so is it "pure"? What do we even mean by "result" in a language with exception handling? Do thrown exceptions count, or only return values? Similarly, using "valueOf" doesn't show that the sum function is impure, it shows that the sum function is higher-order. If we mapped 'Math.random' over an array, does that make 'map' impure? What does it mean to be pure/impure when any variable can be replaced by a 'valueOf' function/procedure? If two expressions call the same procedures, the same number of times, in the same order, and return the same pure function of their outputs, are those two expressions the same? Would replacing one with the other maintain referential transparency, and does that imply "purity"? I think the answer is that such examples need more fine-grained notions than just "pure"/"impure". Otherwise it's just arguing over the semantics of English, rather than semantics of programs.
- jerf 10y agoDefining a pure function as "a function that is pure as long as you use it purely" isn't a very useful definition. (Note distinction between "not very useful" and "useless".) It isn't very useful to humans, who do not have the cognitive bandwidth to process the resulting graph of purity and impurity through any non-trivial code base, especially in dynamically-typed language like Javascript where the purity of a function's invocation can be changed at runtime with the full arbitrariness of a Turing-complete language. It isn't useful at all to compilers or JITs, who if they can not be assured a function is definitely, 100% always pure must always check in one way or another and thus lose optimization opportunities. It's sort of useful in terms of looking at leaf nodes of the structure of the program and determining that this call is pure (today, at least) and thus reasoning with it hyperlocally, but that's pretty much it, and that's not that great of an advantage over what programmers already do. And such reasoning is still quite weak given that it can be invalidated by changes to the local environment, like complicated objects getting passed in. I think purity isn't too confusing here, and it's not really a Haskell concept being used, it's general purpose. It's just that the answer given by the post is correct; it's essentially impossible to write pure Javascript code. It's also, for much the same reasons, impossible to write pure Python, Ruby, or Perl; there's so many ways to invoke something that runs arbitrary unconstrained code (properties, method overrides, class-level hacks, metaprogramming, redefinitions of symbols used by the function) that it's very difficult or impossible to write pure code. (I think you could write a pure id function in JS. But in light of the fact that x + 10 can be impure, that would seem to be about the limit.) And this fact is not just academic; it's part of the reason why it seems like JS engines have plateaued at around 8-10x slower than C. These issues may be academic to most JS programmers, but they are brutally, unavoidably practical to someone writing a JIT.
- philplckthun 10y agoObviously due to JavaScript being a scripting language and dynamically typed, we have to make certain assumptions so that we can even consider calling any function "pure". It's nice that André wants to warn us here about the fact that the term "pure" is more or less accurate depending on the language. In JS it can depend heavily on previous assumptions and the context, but I'd argue that this is often only due to the arguments that are being passed to a function. A non-safe function that isn't type will always be impure in some contexts. But for sanity reasons we must be able to make assumptions about the arguments.
- fmap 10y agoDefining what a pure function in JavaScript is seems to be the difficult problem here. Maybe we should call a function pure if "given identical, pure arguments, it returns identical, pure results". You would also need to define when an object is pure. That's already difficult, as the semantics of JavaScript objects are so weird. It's quite likely that we would have to replace "identical" by some weaker notion of relatedness which is merely preserved by "pure" functions... Anyway, this might be useful as a static analysis for a JavaScript vm. It depends on how much wild JavaScript code is actually "pure" in any reasonable sense of the word. Let's just sum this up and say that JavaScript is really not a nice language to analyze.
- cousin_it 10y agoGood article. Yes, the idea of purity becomes less useful in OO languages. That's not specific to JavaScript or even to dynamic typing. Try summing the elements of a Collection<Integer> in Java and your code will also be impure, because Collection is an interface whose implementation can do anything. I agree that JS goes too far in allowing you to impersonate numbers, but most real-world code uses interfaces in some way, so you can't make it pure without changing your language into Haskell.
- catnaroek 10y agoIs this function pure? function make_pair(x, y) { return { former: x, latter: y }; }
- thght 10y agoI would use my dynamic type checker/enforcer to make it pure: var types= require( 'types.js' ); function sum(arr) { arr = types.forceArray( arr ); var z = 0; for (var i = 0; i < arr.length; i++) { z += types.forceNumber( arr[i], 0 ); } return z; }
- chukye 10y agoI would also use some `demand` function or argument-validator in JavaScript to validate that, thats a good practice in anyway, like: `function sum(arr) { goodArrayOfNumbers(arr); ... }`. I think that; in JavaScript you NEED to validate your arguments with something like that, you really cant assume nothing. ("Dont assume it, prove it!").
- nicolast 10y agoTrivia: assuming the input to `sum` below is guaranteed to always be a non-trickery array of numbers, so it always returns the same result for the same input, is the function 'pure'? var x = 0; function sum(arr) { x += 1; return arr.reduce(function(a, b) { return a + b; }, 0); }
- eyelidlessness 10y agoI don't understand how this is ambiguous. `sum` is changing state outside its scope (a side-effect). There is no definition of "pure" I'm aware of that would include this implementation.
- ohitsdom 10y agoFor those of you that write in JS (and not TypeScript), do you use "void 0" instead of "undefined"? I use undefined and would hate to switch to void 0, since it's less clear and unnecessary thanks to tools like JSHint.
- v413 10y agoPrior to ES5 undefined was overwritable that is why void 0 guaranteed to produce undefined. With ES5 and later using undefined directly is safe.
- pygy_ 10y ago`undefined` is a variable that can (could? It may have changed with strict mode or ES6) be set globally to arbitrary values. You can use `undefined` safely with an IIFE wrapper: (function(undefined) { // you're safe here }());
- wybiral 10y agoIf you're using valueOf like that then you already did something wrong. Reevaluate your life choices.
- phyzome 10y agoI bet you can still break that last code block by having an isArray accessor that returns the "proper" function the first time and a broken one the second time. :-)
- pmarreck 10y agoGiven the emerging importance of separating "pure" code from "side-effecting" code, what frontend JS techs actually enforce this division, as opposed to just leaving it up to the fastidious developer? Elm? Haskell/Haste?
- ajuc 10y agosum(); // TypeError: Cannot read property 'length' of undefined That doesn't mean it's not pure. var arr = [{}, {}, {}]; arr[0].valueOf = arr[1].valueOf = arr[2].valueOf = Math.random; sum(arr); // 2.393660612848899 sum(arr); // 2.3418339292845998 sum(arr); // 2.15048094452324 Oh come on. By that logic there are no pure functions in C, because you can play with #define. Definitions are meant to be useful.
- dustingetz 10y agoNo (map println [1 2 3]) Map is RT even when println is not
- badthingfactory 10y agoDon't pure functions come with the disclaimer that no one does anything stupid like overriding the Date constructor or valueOf? Otherwise, we have to either come up with a different name for pure functions with a modified meaning, or write a bunch of absolutely ridiculous defensive code in every single function we want to truly be pure.
- caub 10y agoso var sum=(...a) => a.reduce((x,y)=>x+y) is pure?
- pwdisswordfish 10y agoIf we're being nitpicky about type checking... function f() { return Math.random() < 0.5; } f.toString = function () { return 'function toArray() { [native code] }' } Array.isArray = f; What, use Function.prototype.toString instead? What if someone tampered with that too?
- chadscira 10y agoI mean what happens when someone does Array.isArray = () => true; Array.isArray.toString = () => 'function isArray() { [native code] }'; :/
- lr4444lr 10y agoPremise is interesting, but by the end, JS fails the author's standard of purity not really because of its function implementation (or any variant thereof) but because of its dynamic-weak type system.
- dclowd9901 10y agoDear god this article is annoying. I could spend an hour nitpicking the bs-ey wonk that André is spewing here, but that would be pointless, so I'd like to make a more macro point: Can we please get away from pedantism and trivial analysis as a display of intelligence?
- yokohummer7 10y ago> Well, in an actual functional programming language like PureScript or Haskell we can: > > sum :: [Int] -> Int > sum = foldl (+) 0 > > The list cannot be undefined, cannot be null. Yes it can? main = putStr "The result is: " >> print (sum undefined) The result is: Program error: Prelude.undefined So by the same logic Haskell doesn't have pure functions, either?
- sebringj 10y agoI'm too stupid to see how this is either practical or relevant in a loosely typed language. The example was making assumptions that it is an array with integers. It was also making an assumption a hacker is not hijacking your code to put strange values into your array etc. This reminds of John Resig's super complicated tricky JavaScript examples only a very small number of people would even care to try to understand but slightly less purposeful. Probably good for that type of crowd.
- rictic 10y agoAlong the same lines as worrying about someone redefining `Array.isArray`, someone could compile your Haskell with its own prelude, including their own definition for `Int`. There's a point at which the pursuit of purity becomes the pursuit of sanctity, in the sense of Haidt's moral foundations (https://en.wikipedia.org/wiki/Moral_foundations_theory https://en.wikipedia.org/wiki/Moral_foundations_theory). It's not about a rational tradeoff at that point, it's about easing an anxiety and sense of uncleanliness. I say this because I'm prone to doing it too, and I've noticed that I can spend a lot more time and energy than is worthwhile "cleaning" code.
- newshorts 10y agoShould have been titled: "Is Your JavaScript Function Actually Pure? An impractical guide to Pedantic JavaScript."
- deleted 10y ago[deleted]
- kazinator 10y agoThrowing an error doesn't make a function impure. If the calculation is erroneous then the expression sum() has no value which could replace it. I.e. the definition of pure being that we can replace sum(a) by its value without changing the behavior of the program, simply doesn't apply. The exception is effectively equivalent to the situation of there not being a value. As for this: var a = {}; a.valueOf = Math.random; var fn = x => x * 10; fn(a); // 5.107926817373938 fn(a); // 3.4100775757245416 fn(a); // 5.1903831613695095 // Same input, different outputs! Calling that "same input" just because it has the same syntax "a" is just ... plain silly. It's obviously "a" that is impure not the "fn" multiplication function. "a" is behaving like a macro which denotes the retrieval of a pseudo-random number. It's simply not reasonable extend the definition of a functions' purity to include *the behavior of the expression(s) which are evaluated to provide its arguments. (I would say that it's still not reasonable even under non-strict evaluation.)