Prev: func.apply() throws Error -2147467259
Next: subject
From: Dmitry A. Soshnikov on 31 Jan 2010 10:07 On Jan 31, 5:21 pm, "Dmitry A. Soshnikov" <dmitry.soshni...(a)gmail.com> wrote: [...] > dict([('a', 1), ('b', 2)]) # {'a': 1, 'b': 2} > > You can create absolutely the same object-builder from passed array of > arrays as argument in JavaScript function and reuse it then. > Small addition: although it's possible to do it in this way (and also in JS), there will be additional full iteration inside the `dict' function (callable class), first to get array from array comprehension, the second one - to create the dictionary from the array parameter: d = dict([(v, k) for v, k in {'a': 1, 'b': 2}.items()]) Meanwhile in JS will be only one iteration for that: [o[key] = value for ([key, value] in Iterator({a: 1, b: 2}))]; /ds
From: G on 31 Jan 2010 10:29 It's comforting to know that USENET hasn't changed much after all these years. :) -- G
From: G on 31 Jan 2010 11:00 Would caching allow multiple interleaved uses, without conflicts? I was assuming the present implementation of getLocalStorageKeyValues() would allow for multiple interleaved clients, with different filtering criteria. For example, I was hoping to use Worker instances and localStorage together (assuming Worker instances can access localStorage, I haven't experimented with Worker yet). Hmmm, maybe Worker scope is isolated, including any potential getLocalStorageKeyValues() states... In that other language; I should have used the example from the Python docs (thought it wasn't as obvious what was going on, perhaps it is actually clearer). dict( (x, sin( x * pi / 180 )) for x in range( 0, 91 ) ) That's a generator expression being passed to dict(), a tuple at a time. I assume that minimizes the intermediates involved. I do like the o = {}; [o[k] = v ...] trick (for JavaScript). Without running some benchmarks, I don't know which would be most performant (I'd guess for loops, due to the lack of function calls). -- G
From: Dmitry A. Soshnikov on 31 Jan 2010 12:12
On Jan 31, 7:00 pm, G <culturea...(a)gmail.com> wrote: > Would caching allow multiple interleaved uses, without conflicts? I > was assuming the present implementation of getLocalStorageKeyValues() > would allow for multiple interleaved clients, with different filtering > criteria. For example, I was hoping to use Worker instances and > localStorage together (assuming Worker instances can access > localStorage, I haven't experimented with Worker yet). Hmmm, maybe > Worker scope is isolated, including any potential > getLocalStorageKeyValues() states... > Sure if some different objects should use it with own criteria (and especially with Workers) it's not acceptable. But in this case would be better to put iterator function into the prototype (which means, all instances will have their own state, but will share one __iterator__ function): function getLocalStorageKeyValues(criteriaFn) { ... this.criteriaFn = criteriaFn; ... } getLocalStorageKeyValues.prototype.__iterator__ = function () { if (this.criteriaFn(...)) { ... } }; var first = new getLocalStorageKeyValues(function () {...}); var second = new getLocalStorageKeyValues(function () {...}); But, that's just a suggestion, do in most comfortable way. /ds |