Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: How would you prepare for javascripting interview? Date: Sat, 23 Jul 2016 16:25:57 +0200 Organization: PointedEars Software (PES) Lines: 91 Message-ID: <1875389.irdbgypaU6@PointedEars.de> References: <50e38667-93da-4c5d-bc02-a78394f0ca3b@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1469283959 30469 eJwFwQERACAIA8BKwmQecVBY/wj+B2h8ZzO4QyFivDP9zbPJNhV1ShQQm1nCqm74lF/cXh8uohHs (23 Jul 2016 14:25:59 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 23 Jul 2016 14:25:59 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNykkBwDAIBEBLXEtADoHgX0I774E6ex9zuGGxtpmTfGmK7GD4CYNCxrUPC4LKOvjGU5ii3ojLvwPbklMfNpsU9w== Cancel-Lock: sha1:5PHw3eungHhLWa0hoKKIUBH8ows= X-NNTP-Posting-Host: eJwNyMEBwCAIA8CVSiChjKNY9h/B3vPoMnWGqOBwFlL0XXrJyDimP70g7DrjjlHXanx8TCxcAq0QJw== Xref: csiph.com comp.lang.javascript:30966 Stefan Ram wrote: > Scott Sauyet writes: >>through discussion: "It's a somewhat surprising fact that in Javascript, >>Math.min() > Math.max(). Can you think of why this might be so?" > > A varargs operator (that is obtained from a binary operator) > over the empty set always gives the identity element of the > operation, because this is the most natural and useful definition. > > For example, we can say that the sum of a list of numbers > a, b, c, can be written as > > 0 + a + b + c > > where there is a substring »+ x« for every member of the list. > > So, we have to rule to append » + x« to »0« for each member »x«. > > The binary operator »+« is not defined for just one operand > or no operand, but our list operator (varargs operator) needs to be. > > When there is only one member a in the list, the rule given > above results in > > 0 + a > > , when the list is empty we just get > > 0 > > , the identity element. [What on Earth is the relation of this to the question at hand?] The reason for the aforementioned fact is that Math.min() returns Infinity and Math.max() returns -Infinity. And – need I spell it out? – Infinity > -Infinity. It makes sense for a minimum function to iterate over an argument list starting with the highest possible value, and for a maximum function with the lowest possible value. Were one to implement the methods (according to ECMAScript Ed. 3 and later), they would be (quickhack): if (Math.min(2, 3, 1) > 1) { Math.min = function () { var result = Infinity; for (var i = 0, len = arguments.length; i < len; ++i) { var arg = +arguments[i]; /* NaN is the only Number value not equal to itself */ if (arg != arg) return Number.NaN; if (arg < result) result = arg; } return result; }; Math.min.length = 2; } if (Math.max(1, 2, 3) < 3) { Math.max = function () { var result = -Infinity; for (var i = 0, len = arguments.length; i < len; ++i) { var arg = +arguments[i]; if (arg != arg) return Number.NaN; if (arg > result) result = arg; } return result; }; Math.max.length = 2; } BTDT (there have been implementations that did not support more than two arguments, and tail recursion is inefficient in this case). -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.