kaffeine icon indicating copy to clipboard operation
kaffeine copied to clipboard

for own key in { ... }

Open benekastah opened this issue 14 years ago • 15 comments

Are there plans to develop a method to easily generate a javascript for...in loop like:

var obj = {a: 'a', b: 'b'}
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    doSomething();
  }
}

I think a variant of CoffeeScript's solution would be elegant:

for own key in obj {
  doSomething();
}

For me, this is the only really crucial thing I find missing from kaffeine. I will probably still end up using (and contributing) to it even with this missing because of how awesome async programming will be with this tool.

benekastah avatar Jun 13 '11 17:06 benekastah

hmm good idea - needs a decent keyword ...

for key within obj {
   doSomething();
}

?

Love some help ..

On Mon, Jun 13, 2011 at 6:02 PM, benekastah < [email protected]>wrote:

Are there plans to develop a method to easily generate a javascript for...in loop like:

var obj = {a: 'a', b: 'b'}
for (var key in obj) {
 if (obj.hasOwnProperty(key)) {
   doSomething();
 }
}

I think a variant of CoffeeScript's solution would be elegant:

for own key in obj {
 doSomething();
}

For me, this is the only really crucial thing I find missing from kaffeine. I will probably still end up using (and contributing) to it even with this missing because of how awesome async programming will be with this tool.

Reply to this email directly or view it on GitHub: https://github.com/weepy/kaffeine/issues/32

weepy avatar Jun 13 '11 17:06 weepy

or

for key ownedby obj { ... }

Edit: sorry, accidentally closed the issue.

benekastah avatar Jun 13 '11 17:06 benekastah

inside ?

On Mon, Jun 13, 2011 at 6:23 PM, benekastah < [email protected]>wrote:

or

for key ownedby obj { ... }

Reply to this email directly or view it on GitHub: https://github.com/weepy/kaffeine/issues/32#issuecomment-1360399

weepy avatar Jun 13 '11 17:06 weepy

hmmm... I feel like I'm a little bit stuck on own. I think it's an intuitive word because it is in the native JavaScript method hasOwnProperty. What about:

forown key in obj

making forown a new keyword?

benekastah avatar Jun 13 '11 18:06 benekastah

How about :

for key in obj { if !obj.hasOwnProperty(key), continue doSomething(); }

^_^

Tapped on my fone

On 13 Jun 2011, at 18:02, benekastah < [email protected]> wrote:

for (var key in obj) { if (obj.hasOwnProperty(key)) { doSomething(); }

weepy avatar Jun 13 '11 18:06 weepy

Why do you need this feature so ? What's your use case. ?

Tapped on my fone

On 13 Jun 2011, at 19:36, benekastah [email protected] wrote:

hmmm... I feel like I'm a little bit stuck on own. I think it's an intuitive word because it is in the native JavaScript method hasOwnProperty. What about:

forown key in obj

making forown a new keyword?

Reply to this email directly or view it on GitHub: https://github.com/weepy/kaffeine/issues/32#issuecomment-1360908

weepy avatar Jun 13 '11 18:06 weepy

Fair enough, lol. Well, I will try to put something together to make this work tonight. I'll pick a keyword for you to veto :)

Edit: just saw your comment above.

I always loop that way by default, because otherwise I am looping through all the parent prototypes of the object. When I'm looping through an object, most often I only care about the direct members of that object (although this isn't always the case). It's an optimization, but it's also a security measure, to make sure that I only modify what I'm expecting to modify.

benekastah avatar Jun 13 '11 19:06 benekastah

for x from y { .. } ?

weepy avatar Jun 13 '11 20:06 weepy

while we're on the subject, it would be nice to be able to range between two variables. i.e. sugar for : for(var i=a ; i < b; i++)

could do

for i=1..3 { }

or

for i of [a..b]

weepy avatar Jun 13 '11 20:06 weepy

I really like for x from y {...}. That one feels good to me.

About the ranges, what if a range simply resolved to an array?

[1...5] // compiles to [1, 2, 3, 4, 5]

Then looping would be built in:

for i in [1...5]

Additionally, one could use the for i of [a..b] construct you mentioned to skip doing for i, j in [a..b] to get the value. It might open up other useful possibilities as well.

benekastah avatar Jun 13 '11 23:06 benekastah

yeah :) how about:

[a..b]  
goes to
__array(a,b)

function __array(x,y) {
  var a = [];
  for(var i=x; i

weepy avatar Jun 14 '11 10:06 weepy

I like it (I will infer the rest of that function :). Should we distinguish between inclusive and exclusive ranges?

[1..5] // => [2, 3, 4] or [1, 2, 3, 4] ?
[1...5] // => [1, 2, 3, 4, 5]

benekastah avatar Jun 14 '11 16:06 benekastah

[1..5] // => [1,2, 3, 4, 5] [1...5] // => [1, 2, 3, 4]

On Tue, Jun 14, 2011 at 5:48 PM, benekastah < [email protected]>wrote:

I like it (I will infer the rest of that function :). Should we distinguish between inclusive and exclusive ranges?

[1..5] // => [2, 3, 4] or [1, 2, 3, 4] ?
[1...5] // => [1, 2, 3, 4, 5]

Reply to this email directly or view it on GitHub: https://github.com/weepy/kaffeine/issues/32#issuecomment-1367371

weepy avatar Jun 14 '11 16:06 weepy

I am authoring a language like Kaffeine. My thoughts are to steal from LiveScript in this instance: http://livescript.net/

The keywords used there are much less ambiguous (do I use one dot? do I use two? let me check the manual...)

for key in [1 til 10] {
}
for key in [1 to 10] {
}

Also I would like to see hasOwnProperty be the default looping construct. If you want ancestor keys then add something like this:

for all key in obj {
    console.log key
}

cmwelsh avatar Jan 04 '13 23:01 cmwelsh

Awesome. Like the keywords.

What's your philosophy for the language ?

On Friday, January 4, 2013, Chris M. Welsh wrote:

I am authoring a language like Kaffeine. My thoughts are to steal from LiveScript in this instance: http://livescript.net/

The keywords used there are much less ambiguous (do I use one dot? do I use two? let me check the manual...)

Also I would like to see hasOwnProperty be the default looping construct. If you want ancestor keys then add something like this:

for all key in obj { console.log key }

— Reply to this email directly or view it on GitHubhttps://github.com/weepy/kaffeine/issues/32#issuecomment-11904448.

weepy avatar Jan 05 '13 10:01 weepy