Hacker Newsnew | past | comments | ask | show | jobs | submit | mthq's commentslogin


The 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.


I 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.


Is it then the variable (or the variable accessor) that is impure, or the function that is impure?


The 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.


Math.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.


I thought it was a bit pedantic as well, at first. But looking at Haskell, I can see why the author made this point. Haskell functions won't accept random values as input, since they're monadic values.


Are there two identical inputs to Math.random that yield different results? If the answer to that question is yes, the function isn't pure.


Replace Math.random with this, then:

    ((j) => () => j++)(0)


That's what I thought. It boils down to the question of whether:

    f => f()
is a pure function? If so, then so is the author's example. If not, then I don't see how any JS function that takes an argument (and evaluates it) can be pure. Under that definition, I'm not sure it makes sense to talk about.


Is Haskell's `(++) :: [a] -> [a] -> [a]` pure? What if I pass `unsafePerformIO` to it?

For theoretical purposes it might make sense to say pure functions cannot call impure functions, but I think a more practical definition includes functions that are provided as arguments.


Haven't the faintest. I don't speak Haskell, 's why I asked.

I'd thought pureness was a property of function definitions, irrespective of what they get passed, but living in Javascript land I've never had occasion to be concerned with the distinction. I kind of suspect it's not really something that's useful to talk about.


Not asking you directly, I agree with you and was trying to rephrase the notion.


If you introduce unsafe into a program, it is not pure. You can run a linter or something to check if your program uses unsafe anywhere.

Purity is a spectrum, not a binary, The more impurities you add, the more likely your program will not behave reasonably (as in, not in accordance with the simple logical rules of referential transparency).


> If not, then I don't see how any JS function that takes an argument (and evaluates it) can be pure.

A JS function could take an argument, use it but not evaluate it (or not evaluate it in all cases). `typeof` doesn't evaluate its argument for instance.


Certainly it could. I don't know what that has to do with my comment though.


The definition of a pure function is pretty well defined https://en.wikipedia.org/wiki/Pure_function. Math.random depends on hidden state, so (generally) any function using it isn't pure. This doesn't matter much in a language like Javascript, but other languages depend upon this definition (like the lazy function evaluation in Haskell).


Is that definition of pure closer to mthq's definition or the one in the article? I'm not sure.

It says "same argument value(s)", but how does that apply when the argument is a function? The article seems to be saying that if the same argument is passed in every time, then the function is only pure if it returns the same value every time, regardless of whether the argument is a pure function or not.


A pure function can't call a non pure function. So you can pass in a non pure function so long as you don't call it.


It's not so well-defined. In particular, "side effect" is hard to pin down. For instance, a good definition ought to consider potential non-termination a side effect. In that way, many functions are impure.


It's a mathematical definition, of course it will break down a bit when you introduce it into the real world. I guess a more pragmatic definition would be "The ability for a runtime to delay, or repeat the execution of a function (with its arguments), and always get the same result".


I mean that it's not even so well defined mathematically. Starting from a weak place and then moving it to the "real world" makes things quite tricky.


> I mean that it's not even so well defined mathematically.

Which part of it isn't? The distinction between "potentially non-terminating" and its negation is exactly the distinction between partial and total functions.


The definition of pure function (or, dually, side effect) is difficult to pin down. I've heard a number of definitions, but the best ones are far more technical than you'd see in casual conversation and the casual ones often admit holes.


> I've heard a number of definitions, but the best ones are far more technical than you'd see in casual conversation

To be sure, but there's a big difference between casual definitions being subtly wrong and:

> I mean that it's not even so well defined mathematically.

Most mathematical definitions, at least the interesting ones, aren't really suitable for casual conversation!


Right, so I think where we're disagreeing is that the casual ones are subtly wrong. I think they're often pretty significantly wrong and that this post was essentially poking into that notion. When purity gets on the table lots of divergent ideas tend to be tossed out to nail it down and a lot of confusion results.

The best casual definition I know of is "a pure function, f, is one such that all variation in the function's result arises from variation in its input and that all information from the function's call is contained in its result: throwing the result away is equivalent to having never called the function"

A bit of a mouthful, but it's intuitive and arises from a formal definition that works out pretty nicely. Otoh, it's complex enough that it's rarely used in its complete form. Finally, it's still subtle enough that it can lead to questions about what side effects are exactly (non-termination is an immediate example).

Anyway, I just want to continue to suggest that understanding pure functions, really, is a more difficult task than people make it out to be.


Pretty well defined?

> This article needs additional citations for verification ... (July 2014)


Looks nice! But I wonder, what is the reason to use this over something like bootstrap?


I initially thought, hey great that's something you simply include and have a base layout that's a great starting point for any type of project, but after looking through the document I realized that it comes with as many "modules" as bootstrap or other full-blown "css-frameworks". Wish they just made a better and more consize and proven {normalize.css reset.css base.css}

Some of the styles are nice and ready to copy though, ie. the table styles, but not sure what the intention of this project is.

--

→ What lightweight css-starter-kit would you choose for any of your upcoming projects? Other than reset.css or normalize.css of course :)

I don't like bootstrap and similar, because it's just too large and most people end up using all of the classes and the whole javascript instead of only the stuff they need. Bootstrap is absolutely great for projects that quickly need a whole "UI Theme" fix.


Skeleton (http://getskeleton.com) is pretty nice, as lightweight CSS frameworks go.


If you're looking for a simple, lightweight alternative to bootstrap, check out PureCSS by Yahoo:

http://purecss.io/


I like Nicolas Gallagher's SUIT CSS style foundation, utilities and components. https://suitcss.github.io/


That's completely new to me, but it looks nice. Thank you. I only knew about stylus, http://learnboost.github.io/stylus/ but that's just a preprocessor.



Although impressive, this quiz's content seems cherry picked for situations where bots can do well.

On the one hand we have highly informational content such as the one about the earthquake, earnings report and sports game. Reporting on this kind of information can be done in a context free way. Sentences and paragraphs can be self contained and need not reference each other or greater contexts. I only missed the sports game in part because I'm not an American and was not familiar with the game involved. In hindsight the earnings reports stands out from the others since much broader connections are made then just reporting the raw information itself.

On the other hand we have poetry, which is often so hard to parse that nonsense and genius are hard to tell apart, especially for short excerpts like these. I missed the poetry app one because although it read like nonsense for me, as a non native speaker much of these Shakespearean poems do on first sight. I don't know exactly why but "True Love" and "Absurdistan" immidetly stuck me as fake and real respectivly. For example I thought it was weird to be that her nerves where strained as two tight strings, and that someone would make her drink hot wine. Also the "True Love" excerpt seems very factual and deseperate where "Absurdistan" paints more of an atmosphere.


It could report on small events that are normally not covered to. Further it could customize articles based on readers interests. Have a favorite player? He is now highlighted in your articles.

It could also do vanity articles. Go back to your college days and write about your games, making you the highlight.

Worse it could be influenced to do personalized advertisements in the middle of the article.


> For example I thought it was weird [...] that someone would make her drink hot wine.

Hot wine is very much a thing (and it's delicious)! http://en.wikipedia.org/wiki/Mulled_wine


Row types are a way to encode type information of OOP object in a hindley milner type system. It allows you to type check the kind of ducked objects that are often featured in dynamic OOP languages like ruby and javascript. So you may specify a function on a object containing specific fields or methods without specifying a concrete type:

  f :: {left: Number, right: Number} -> Number
  f object = object.a + object.b
All type checked of course.

See here for more info: https://www.cs.cmu.edu/~neelk/rows.pdf


I assume you meant to write `object.left + object.right` instead of `a` and `b`, to match the type signature.


The site owner thinks he is being DDOS'ed: http://bit-player.org/2014/net-pests/


I'm the site owner. It's open again, temporarily. Somebody's script has multiple machines (many are EC2 instances) downloading the same file many times per second. That's not something I want to encourage.


Interesting. I suppose an automated DDoS of everything on the hackernews front page isn't impossible.

Are there other explanations? Scribd definitely got it automatically at least once, maybe they do it too many times? How many HN users use EC2-based proxies?



Well that would break back and forward history navigation as browsers require you te explicitly resend POST parameters.


Also the possibility to bookmark and to send a link to somebody else.

Also after all, you want to GET the search result, not POST anything.


Looking at the source[1], it does actually seem to be written in javascript.

[1] https://github.com/larryng/coffeescript-repl/blob/master/js/...


you are right, does not look like coffee-compiled-js. schräg. ("schräg" is german for "strange, baffling")


Well, one of the reasons why I wrote the REPL was to learn CoffeeScript. However, I soon realized my JavaScript knowledge was lacking, so I decided to write the REPL in JavaScript to learn/practice.

Porting the REPL to CoffeeScript will be Phase 2.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: