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