Prev: func.apply() throws Error -2147467259
Next: subject
From: Dmitry A. Soshnikov on 30 Jan 2010 13:04 On Jan 30, 8:49 pm, "Dmitry A. Soshnikov" <dmitry.soshni...(a)gmail.com> wrote: [...] > var o = {}; > [o[key] = value for ([key, value] in Iterator({a: 1, b: 2}))]; > > Which actually isn't so different from simple iteration via: > > var o = {}; > for ([key, value] in Iterator({a: 1, b: 2})) { > o[key] = value; > > } Addition: isn't so different except that in first case additional "intermediate" array from array comprehension will be created and deleted after that. If you put this construction into the round brackets that will be generator and not array comprehension. This generator, which generates iteration result lazily should be activated via .next method: (o[key] = value for ([key, value] in Iterator({a: 1, b: 2}))).next(); /ds
From: G on 30 Jan 2010 14:36 D'oh, bitten by {"variablenotallowedhere": "foo"} yet again! I was hoping to refactor an 'old style' for loop but, as you point out, simple iteration is OK too (and clearer than the alternative (s)). Using a generator (with additional tricks, to drive the .next ()) would also clutter the code in question (see below), so I'll stick with the 'old style' for loop for now (and avoid the intermediate, as you also pointed out). Thank you for the suggestions! function getStorageDataKeys() { let retval = []; for( let loop = 0; loop < localStorage.length; ++loop ) { retval.push( localStorage.key( loop ) ); } return retval; } function getStorageData() { let retval = {}; /* for each( let key in getStorageDataKeys() ) { retval[key] = localStorage.getItem( key ); } */ [retval[key] = localStorage.getItem( key ) for each( key in getStorageDataKeys() )]; return retval; } // all that to enable for( let [key, val] in Iterator( getStorageData() ) ) { // ... -- G
From: G on 30 Jan 2010 15:21 Ah, wait, __iterator__() to the rescue. No key array needed! Of course getStorageData() doesn't really return {} items, but it behaves as if it did in for each loops. :) function getStorageData() { return { "__iterator__": function() { let loop = 0; while( loop < localStorage.length ) { let key = localStorage.key( loop ); yield [key, localStorage.getItem( key )]; ++loop; } throw StopIteration; } }; } -- G
From: Thomas 'PointedEars' Lahn on 30 Jan 2010 16:51 G wrote: > By "-v please", are you asking which version? No, I meant to enable your --verbose mode ;-) IOW, what do you expect the input and output of "Object comprehension" to be? PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: G on 31 Jan 2010 01:05
Sorry about that, my default is terse (influenced by Postel's conservative:send::liberal:receive, and Strunk/White, and BNF (anyway...)). I'd like to be able to define 'object comprehensions' like array comprehensions, in JavaScript, with key:values ({}) instead of values ([]). Maybe it's a Python itch? As pointed out; because literal keys are required, the object syntax might be tricky. While thinking about intermediates (like the key array as well as the entire {} being returned (basically a copy of localStorage)) and data sizes (localStorage might be 5M or more), using __iterator__() seemed like a viable approach (much more on demand, limiting data fluff) and is compatible with [key, value] <- Iterator( foo ) syntax (behaving like a {} value, on the 'client' side). In the __iterator__() code mentioned, I've moved the 'key' declaration outside the loop and wrapped the return value in an Iterator, which allows for code like for( let [key, val] in getLocalStorageKeyValues() ) {...} (the function name was also refactored)) on the 'client' side, and am happy now with the implementation (and 'client' API). If only localStorage permissions (in Firefox 3.6) weren't so quirky... ;) https://bugzilla.mozilla.org/show_bug.cgi?id=542730 -- G |