From: Dmitry A. Soshnikov on
On Jan 30, 11:21 pm, G <culturea...(a)gmail.com> wrote:
> 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;
>                 }
>         };
>
> }
>

Yeah, __iterator__ (generator-based in your case; also you can you
special iterator object with .next method) - is a good decision in
this case. Although, there's still array on exit and you need to
define first object and then fill it with __iterator__ yielding
result.

/ds
From: Dmitry A. Soshnikov on
On Jan 31, 9:05 am, G <culturea...(a)gmail.com> wrote:

[...]

>
> Maybe it's a Python itch?
>

Yes, array comprehension was borrowed to JavaScript(trade mark) from
Python, but in Python in difference from JS that's not possible to
fill some object iteration dictionary via array comprehension:

[(v, k) for v, k in {'a': 1, 'b': 2}.items()]

Result: [('a', '1'), ('b', 2)]

[a[v] = k for v, k in {'a': 1, 'b': 2}.items()] # syntax error

for v, k in {'a': 1, 'b': 2}.items(): # OK
a[k] = v

Also, there's no "object comprehension" in Python.

/ds
From: G on
True, there aren't 'object comprehensions' in Python, but they can be
faked. :)

import math
sintable = dict( ([degree, math.sin( degree )] for degree in range( 0,
360 )) )
print sintable

While it would be nice to have something similar in JavaScript; for
loops and __iterator__() definitions seem to suffice (I'm not familiar
with JavaScript internals, so I don't know if comprehensions could be
optimized). Anyway, thanks for the discussion everyone, it was
educational!

Meanwhile, back at the creature feep (in case anyone is interested)...

function getLocalStorageKeyValues( criteriafunction ) {
return { "__iterator__": function() {
let key = undefined;
let loop = 0;
while( loop < localStorage.length ) {
key = localStorage.key( loop );
if( key && (typeof criteriafunction != 'function' ||
criteriafunction( key )) ) {
yield [key, localStorage.getItem( key )];
}
++loop;
}
throw StopIteration;
}
};
}

for( let [key, val] in getLocalStorageKeyValues() ) { //...

for( let [key, val] in getLocalStorageKeyValues( function( key )
key.indexOf( 'someprefix' ) === 0 ) ) { //...

--
G
From: Thomas 'PointedEars' Lahn on
G wrote:

> Sorry about that,

About *what*? Learn to post.

<http://jibbering.com/faq/#posting>

> [...]
> I'd like to be able to define 'object comprehensions' like array
> comprehensions, in JavaScript, with key:values ({}) instead of values
> ([]).

{
key1: value1,
key2: value2
}

> Maybe it's a Python itch?

Maybe you don't know what you are talking about.

> As pointed out

You pointed out nothing. You posted some bogus code without saying what
you expect from it. Hence my asking.

> [TLDR]


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Dmitry A. Soshnikov on
On Jan 31, 4:48 pm, G <culturea...(a)gmail.com> wrote:
> True, there aren't 'object comprehensions' in Python, but they can be
> faked. :)
>
> import math
> sintable = dict( ([degree, math.sin( degree )] for degree in range( 0,
> 360 )) )
> print sintable
>

Hey, completely true, as I practice Python not so often (more
theoretically) forgot that `dict' callable class can accept array with
arrays (or tuples) for dictionary items. So result which you have from
array comprehension can be easily converted to dict-object:

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.


>
> Meanwhile, back at the creature feep (in case anyone is interested)...
>
> function getLocalStorageKeyValues( criteriafunction ) {
>         return { "__iterator__": function() {
>                         let key = undefined;
>                         let loop = 0;
>                         while( loop < localStorage.length ) {
>                                 key = localStorage.key( loop );
>                                 if( key && (typeof criteriafunction != 'function' ||
> criteriafunction( key )) ) {
>                                         yield [key, localStorage.getItem( key )];
>                                 }
>                                 ++loop;
>                         }
>                         throw StopIteration;
>                 }
>         };
>
> }
>
> for( let [key, val] in getLocalStorageKeyValues() ) { //...
>
> for( let [key, val] in getLocalStorageKeyValues( function( key )
> key.indexOf( 'someprefix' ) === 0 ) ) { //...
>

Yep, that's interesting implementation, although, all the iterator
object can be cached (as an optimization for do not create object each
time) in `getLocalStorageKeyValues' as a static property and reused
then:

function getLocalStorageKeyValues(criteriafunction) {

// set each time new criteriafunction
getLocalStorageKeyValues.criteriafunction = criteriafunction;

// but return always the same object
return getLocalStorageKeyValues.iteratorObject;
}

getLocalStorageKeyValues.iteratorObject = {__iterator__: ...};

and inside the getLocalStorageKeyValues.iteratorObject use:

if( key && (typeof getLocalStorageKeyValues.criteriafunction !=
'function' ||
getLocalStorageKeyValues.criteriafunction( key )) ) {

There can be difference in `this' value inside the call of
criteriafunction, but that doesn't heart your case.

/ds
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: func.apply() throws Error -2147467259
Next: subject