Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Syntax for object comprehension?

Reply
Thread Tools

Syntax for object comprehension?

 
 
G
Guest
Posts: n/a
 
      01-30-2010
Not an array comprehension.

let a = [key for each( key in getKeys() )];

An object comprehension...

let o = {key: getValue( key ) for each( key in getKeys() )}; //
invalid syntax
let o = [{key: getValue( key )} for each( key in getKeys() )]; //
valid, but an array of objects instead of an object

Is it possible to do an 'object comprehension' In JavaScript?

--
G
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-30-2010
G wrote:

> Not an array comprehension.
>
> let a = [key for each( key in getKeys() )];
>
> An object comprehension...
>
> let o = {key: getValue( key ) for each( key in getKeys() )}; //
> invalid syntax
> let o = [{key: getValue( key )} for each( key in getKeys() )]; //
> valid, but an array of objects instead of an object
>
> Is it possible to do an 'object comprehension' In JavaScript?


-v please


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$(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
G
Guest
Posts: n/a
 
      01-30-2010
By "-v please", are you asking which version?

Presumably

application/javascript;version=1.7

or

application/javascript;version=1.8

because 'object comprehensions' (if such a thing is possible) are
unlikely to predate array comprehensions.

--
G
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-30-2010
G <(E-Mail Removed)> writes:

> By "-v please", are you asking which version?


He probably meant --verbose, as in: Please give more information (or
perhaps: please Write in entire sentences).

Anyway, as far as I know, there is no Object comprehension in JavaScript
1.7 or 1.8. You might want to look at Generator Expressions, for some
kind of shorthand.

/L
P.S. But remember that it works it Mozilla browsers only.
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      01-30-2010
On Jan 30, 4:04*pm, G <(E-Mail Removed)> wrote:
> Not an array comprehension.
>
> * * * * let a = [key for each( key in getKeys() )];
>
> An object comprehension...
>
> * * * * let o = {key: getValue( key ) for each( key in getKeys() )}; //
> invalid syntax
> * * * * let o = [{key: getValue( key )} for each( key in getKeys() )]; //
> valid, but an array of objects instead of an object
>
> Is it possible to do an 'object comprehension' In JavaScript?
>


Nope, that's not possible. By the syntax rules {key: ...} will be key
'key' but not dynamic key on each iteration from the getKeys() object.

But using array comprehension, it's possible to make it in actions
(define first object, and then fill):

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;
}

Also simple iteration loop is acceptable.

/ds
 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      01-30-2010
On Jan 30, 8:49*pm, "Dmitry A. Soshnikov" <(E-Mail Removed)>
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
 
Reply With Quote
 
G
Guest
Posts: n/a
 
      01-30-2010
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
 
Reply With Quote
 
G
Guest
Posts: n/a
 
      01-30-2010
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-30-2010
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$(E-Mail Removed)>
 
Reply With Quote
 
G
Guest
Posts: n/a
 
      01-31-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments