Prev: FAQ Topic - How can I see in javascript if a web browser acceptscookies? (2010-06-16)
Next: FAQ Topic - How can I see in javascript if a web browser accepts cookies? (2010-06-16)
From: Thomas 'PointedEars' Lahn on 17 Jun 2010 11:05 Dmitry A. Soshnikov wrote: > On 17.06.2010 4:53, Thomas 'PointedEars' Lahn wrote: >> Lasse Reichstein Nielsen wrote: >>> "Dmitry A. Soshnikov" writes: >>>> Lasse Reichstein Nielsen wrote: >>>>> The pattern >>>>> [x,y] = [e1,e2]; >>>>> should be detectable at compile time, so the introduction of the >>>>> intermediate array can be optimized away. >>>> Moreover, it can be done syntactically without brackets, as in Python: >> No, it can't. Check your assumptions. > > Did I say that I assume something? It sure looked so: "... can be done ..., [the same] as in Python". > I confirmed. OK, that was easy to misunderstand. >> the feature was designed for extracting elements from Arrays instead. > > Possibly, it's from what is called pattern-matching in more older > languages (such as e.g. Erlang). There, if you want to extract elements > from "something" where "something", one can use that pattern matching: > > [A, B] = [1, 2] > > A and B will have 1 and 2 respectively. The same: > > {Data, {OtherData, AndOther}} = {10, {"test", [1, 2, 3]}} to bind three > variables. Thanks for the example. It led me to find out that var [data, [otherData, another]] = [10, ["test", [1, 2, 3]]]; works in JavaScript 1.8.2 (and probably all 1.7+), too (that is, data === 10 otherData === "test" another === [1, 2, 3] afterwards.) PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
From: Dmitry A. Soshnikov on 17 Jun 2010 11:19 On 17.06.2010 19:05, Thomas 'PointedEars' Lahn wrote: <snip> > > var [data, [otherData, another]] = [10, ["test", [1, 2, 3]]]; > > works in JavaScript 1.8.2 (and probably all 1.7+), too (that is, > > data === 10 > otherData === "test" > another === [1, 2, 3] > Yeah, moreover, pattern matching for objects also works since 1.7 as I see: let {a: n} = {a: 10}; Thus, `n' becomes 10. It can be useful for shorthands in enumeration of objects: for (let {longPropertyName: name, otherProperty: other} in object) { print(name, other); } Dmitry.
From: Dmitry A. Soshnikov on 17 Jun 2010 14:25 On 17.06.2010 18:16, Richard Cornford wrote: > On Jun 17, 1:51 pm, Dmitry A. Soshnikov wrote: >> On 17.06.2010 0:41, Lasse Reichstein Nielsen wrote: > <snip> >>>> Swapping without "third" can be done with simple arithmetic >>>> operations: >> >>>> A = A - B >>>> B = A + B >>>> A = B - A >> >>> Try that for strings :) >> >> Yeah, I know sure. Just mentioned thinking about numbers at that >> time (with numbers also can be overflow, by the way). > <snip> > > With javascript's double precision floating point numbers overflows > are possible, but loss of precision seems (much) more likely. Consider > what will happen if A is a number at the upper range of possible > number representations, say 1.0e308 and B is a number close to the > smallest (positive) number, say 5.0e-324. But the numbers don't have > to be as extreme as those:- > > var A = 1.0e9; > var B = 1.0e-9; > > A = A - B > B = A + B > A = B - A > > // A is now zero > // B is now 1000000000 > > I have a vague recollection of once being shown a 'swapping' algorithm > for signed integers (with no third variable) that used only bitwise > operations to swap the values, avoiding any risk of overflows. > Obviously in javascript that would only be viable for 32 bit integers, > and adding two of those will not overflow the range of integers that > can be represented by an IEEE double precision floating point number. Yeah, and XOR algorithm won't help also in this case: A ^= B; B ^= A; A ^= B; // the same // A is now zero // B is now 1000000000 Dmitry.
From: Dmitry A. Soshnikov on 17 Jun 2010 14:30 On 17.06.2010 19:19, Dmitry A. Soshnikov wrote: <snip> > for (let {longPropertyName: name, otherProperty: other} in object) { > print(name, other); > } Err (forgot the correct syntax). Should be "for each" and this approach is better use to iterate an array of objects: var array = [ {a: 10, b: 20}, {a: 30, b: 40, c: 50} ]; for each (let {a: x, b: y} in array) { print(x, y); } Result: 10, 20 30, 40 Dmitry.
From: Dr J R Stockton on 18 Jun 2010 16:50
In comp.lang.javascript message <timstreater-150A94.10555217062010(a)news. individual.net>, Thu, 17 Jun 2010 10:55:52, Tim Streater <timstreater(a)waitrose.com> posted: > >Was it not "swap bytes" on an operand in the PDP-11? So you might say: > > swab R3 or swab (SP) > >for example to swap the bytes in register 3 or the top item on the stack. > Probably, since it is on page 10-13 of my copy of a handbook for the LSI-11 series. Did you ever consider 014747, which is MOV -(PC),-(PC) ? -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Command-prompt MiniTrue is useful for viewing/searching/altering files. Free, DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>. |