[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-13 Thread Bruno Jouhier
yield is only valid inside a function*. This is why galaxy has a 'Star' 
variant for all the array methods that take a callback. So, it should work 
if you replace forEach by forEachStar

Bruno

On Friday, July 12, 2013 11:47:28 PM UTC+2, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new feature.  I 
> found two libraries that should help use existing node callback-based code 
> with generators (suspend and galaxy).  The code I'm trying to run was 
> similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id, 
> resume);
> });
> })();
>
> (using galaxy)
>
> var galaxy = require('galaxy');
> var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request'));
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id);
> });
> };
>
> galaxy.unstar(main)(function(err, result) {
> console.log('done');
> });
>
> Using either library, when node tries to execute the get request ("var 
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield 
> request.get..."  I'm guessing that the creation of the new function scope 
> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
> working.  
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-13 Thread Bruno Jouhier
I responded a bit too quickly. To make it work with galaxy you need to 
change the following:

* forEach -> forEachStar
* function(id) -> function*(id) for the callback
* add a yield to the forEachStar call (this is a function* so you need to 
yield on it)

This will get you started but you will hit a few problems with the request 
module. The first one is that request is a function. So the 
galaxy.star(request) call will wrap the request function instead of 
wrapping request.get. You can get around it by calling 
galaxy.star(request.get) but then you'll run into another problem, which is 
that the get callback receives 3 arguments instead of 2. So you need a 
little wrapper around it. An alternative is to use galaxy's stream module 
(see the tutorial). 

Here is a revised version that works:

var galaxy = require('galaxy');
var fs = galaxy.star(require('fs'));

var getWrapper = function(url, callback) {
return require('request').get(url, function(err, response, body) {
callback(err, { response: response, body: body });
})
}
var get = galaxy.star(getWrapper);

function* main() {
var contents = yield fs.readFile('idList.json', 'utf8');
yield contents.split('\n').forEachStar(function*(id) {
var result = yield get('http://www.example.com?id='+id);
console.log("body=" + result.body);
});
};

galaxy.unstar(main)(function(err, result) {
if (err) throw err;
console.log('done');
});

 

On Saturday, July 13, 2013 10:48:43 AM UTC+2, Bruno Jouhier wrote:
>
> yield is only valid inside a function*. This is why galaxy has a 'Star' 
> variant for all the array methods that take a callback. So, it should work 
> if you replace forEach by forEachStar
>
> Bruno
>
> On Friday, July 12, 2013 11:47:28 PM UTC+2, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature. 
>>  I found two libraries that should help use existing node callback-based 
>> code with generators (suspend and galaxy).  The code I'm trying to run was 
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(function(id) {
>> var info = yield request.get('http://www.example.com?id='+id, 
>> resume);
>> });
>> })();
>>
>> (using galaxy)
>>
>> var galaxy = require('galaxy');
>> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request'));
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(function(id) {
>> var info = yield request.get('http://www.example.com?id='+id);
>> });
>> };
>>
>> galaxy.unstar(main)(function(err, result) {
>> console.log('done');
>> });
>>
>> Using either library, when node tries to execute the get request ("var 
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield 
>> request.get..."  I'm guessing that the creation of the new function scope 
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
>> working.  
>>
>>
>>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-13 Thread Bruno Jouhier
I just published a new version (0.1.3) that makes it easier to deal with 
mulitple results callback. You don't need to write a wrapper any more; you 
just get the resutls as an array. So your example becomes:

var galaxy = require('galaxy');
var fs = galaxy.star(require('fs'));

var get = galaxy.star(require('request').get);

function* main() {
var contents = yield fs.readFile('idList.json', 'utf8');
yield contents.split('\n').forEachStar(function*(id) {
var result = yield get('http://www.example.com?id='+id);
console.log("body=" + result[1]);
});
};

galaxy.unstar(main)(function(err, result) {
if (err) throw err;
console.log('done');
});




-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread cpprototypes
Thanks for the replies, I have a better understanding now of how these 
libraries are using generators.  I hope that one of these types of 
libraries becomes as popular as the async library and "standard" within the 
node community.  This way of writing async code is much more elegant than 
the current way of using only callbacks and can help expand node.js usage 
into other areas.  For instance, the script example I wrote is part of a 
larger command line script I was writing to test some services.  Such 
scripting tends to follow a synchronous flow.  I got really frustrated 
while writing it in an async way (since the natural flow is synchronous, it 
maps very poorly to the current callback-only style, even libraries like 
async don't help much).  I eventually gave up and quickly wrote the script 
in python because I had to get the task done for the day.  But I want 
node.js to eventually take python's place in my tool set.  So I went back 
and tried it again with this new generators feature and it was going well 
until I ran into this issue.  Some may say that such use is beyond the 
scope of node.js, but I think JS and node.js have potential to be more than 
just network async programming.


On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new feature.  I 
> found two libraries that should help use existing node callback-based code 
> with generators (suspend and galaxy).  The code I'm trying to run was 
> similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id, 
> resume);
> });
> })();
>
> (using galaxy)
>
> var galaxy = require('galaxy');
> var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request'));
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id);
> });
> };
>
> galaxy.unstar(main)(function(err, result) {
> console.log('done');
> });
>
> Using either library, when node tries to execute the get request ("var 
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield 
> request.get..."  I'm guessing that the creation of the new function scope 
> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
> working.  
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread jmar777
Hey - 

Just realized there's another simple solution to your issue (that should 
work with any of the libraries mentioned in this thread) - just avoid 
nested functions:

suspend(function* (resume) {
var contents = yield fs.readFile('idList.json', 'utf8', resume),
ids = contents.split('\n');

for (var i = 0, len = ids.length; i < len; i++) {
var info = yield request.get('http://www.example.com?id=' + ids[i], 
resume);
}
})();

Main caveat to the above is that all the requests will happen in serial, 
rather than parallel.  Like I said earlier, suspend will soon be including 
some helper functions to make these scenarios easier (I'll happily accept 
any API suggestions around that, too :)).

On Friday, July 12, 2013 5:47:28 PM UTC-4, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new feature.  I 
> found two libraries that should help use existing node callback-based code 
> with generators (suspend and galaxy).  The code I'm trying to run was 
> similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id, 
> resume);
> });
> })();
>
> (using galaxy)
>
> var galaxy = require('galaxy');
> var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request'));
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id);
> });
> };
>
> galaxy.unstar(main)(function(err, result) {
> console.log('done');
> });
>
> Using either library, when node tries to execute the get request ("var 
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield 
> request.get..."  I'm guessing that the creation of the new function scope 
> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
> working.  
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread cpprototypes
The code sample I posted was a simplified example to demonstrate the 
issue.  The actual command line script I was working on uses the response 
from the get request for other things.  That for...of syntax is very nice, 
is it an official part of ECMAScript 6?


On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new feature.  I 
> found two libraries that should help use existing node callback-based code 
> with generators (suspend and galaxy).  The code I'm trying to run was 
> similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id, 
> resume);
> });
> })();
>
> (using galaxy)
>
> var galaxy = require('galaxy');
> var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request'));
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(function(id) {
> var info = yield request.get('http://www.example.com?id='+id);
> });
> };
>
> galaxy.unstar(main)(function(err, result) {
> console.log('done');
> });
>
> Using either library, when node tries to execute the get request ("var 
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield 
> request.get..."  I'm guessing that the creation of the new function scope 
> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
> working.  
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-16 Thread jmar777
> That for...of syntax is very nice, is it an official part of ECMAScript 6?

Yes, although it's not implemented in V8 just yet.  Once for-of and 
generator-expressions are implemented, a lot of these operations will 
become a lot nicer at the language level (rather than relying on cludgy 
`suspend.map()` or whatever helpers).

Here's an example from the ES6 Wiki [1]:

(xhrGet(url) for (url of getURLs())) // hawt

[1] http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions

On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:
>
> The code sample I posted was a simplified example to demonstrate the 
> issue.  The actual command line script I was working on uses the response 
> from the get request for other things.  That for...of syntax is very nice, 
> is it an official part of ECMAScript 6?
>
>
> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature. 
>>  I found two libraries that should help use existing node callback-based 
>> code with generators (suspend and galaxy).  The code I'm trying to run was 
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(function(id) {
>> var info = yield request.get('http://www.example.com?id='+id, 
>> resume);
>> });
>> })();
>>
>> (using galaxy)
>>
>> var galaxy = require('galaxy');
>> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request'));
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(function(id) {
>> var info = yield request.get('http://www.example.com?id='+id);
>> });
>> };
>>
>> galaxy.unstar(main)(function(err, result) {
>> console.log('done');
>> });
>>
>> Using either library, when node tries to execute the get request ("var 
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield 
>> request.get..."  I'm guessing that the creation of the new function scope 
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
>> working.  
>>
>>
>>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-16 Thread jmar777
> Yes, although it's not implemented in V8 just yet. 

I was wrong (and happy about it)!  This is working in the latest unstable 
version of node/v8:

$ node -e 'console.log(process.versions.v8)'
3.20.2
$ node --harmony-iteration --harmony-generators
> function* gen() { yield 'foo'; yield 'bar'; }
undefined
> for (var item of gen()) { console.log(item); }
foo
bar

Awesome! Not sure how I missed that this was added...

On Tuesday, July 16, 2013 9:13:26 AM UTC-4, jmar777 wrote:
>
> > That for...of syntax is very nice, is it an official part of ECMAScript 
> 6?
>
> Yes, although it's not implemented in V8 just yet.  Once for-of and 
> generator-expressions are implemented, a lot of these operations will 
> become a lot nicer at the language level (rather than relying on cludgy 
> `suspend.map()` or whatever helpers).
>
> Here's an example from the ES6 Wiki [1]:
>
> (xhrGet(url) for (url of getURLs())) // hawt
>
> [1] http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions
>
> On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:
>>
>> The code sample I posted was a simplified example to demonstrate the 
>> issue.  The actual command line script I was working on uses the response 
>> from the get request for other things.  That for...of syntax is very nice, 
>> is it an official part of ECMAScript 6?
>>
>>
>> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>>
>>> I'm using node 0.11.3 with --harmony-generators to try the new feature. 
>>>  I found two libraries that should help use existing node callback-based 
>>> code with generators (suspend and galaxy).  The code I'm trying to run was 
>>> similar to the following:
>>>
>>> (using suspend)
>>>
>>> var fs = require('fs');
>>> var request = require('request');
>>>
>>> suspend(function* (resume) {
>>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>>> contents.split('\n').forEach(function(id) {
>>> var info = yield request.get('http://www.example.com?id='+id, 
>>> resume);
>>> });
>>> })();
>>>
>>> (using galaxy)
>>>
>>> var galaxy = require('galaxy');
>>> var fs = galaxy.star(require('fs'));
>>> var request = galaxy.star(require('request'));
>>>
>>> function* main() {
>>> var contents = yield fs.readFile('idList.json', 'utf8');
>>> contents.split('\n').forEach(function(id) {
>>> var info = yield request.get('http://www.example.com?id='+id);
>>> });
>>> };
>>>
>>> galaxy.unstar(main)(function(err, result) {
>>> console.log('done');
>>> });
>>>
>>> Using either library, when node tries to execute the get request ("var 
>>> info = yield request.get...") it exits with the following error:
>>>
>>> SyntaxError: Unexpected identifier
>>>
>>> And the error highlights the "request" part in "var info = yield 
>>> request.get..."  I'm guessing that the creation of the new function scope 
>>> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
>>> working.  
>>>
>>>
>>>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread Tim Caswell
I'm so glad to see more interest in using generators in node.js.  I've
experimented with this style of programming a *lot*.  I even went so far as
to re-implement node.js in the lua language (luvit.io) and attempted to
implement it on top of SpiderMonkey (luvmonkey) because both VMs already
have co-routines/generators.

I know this question has already been answered, but I wanted to share my
solution as I ran into the same issue using generators for some js-git
related examples.
https://github.com/creationix/git-repo/blob/master/example/create.js

The relevant part of that file is where I loop over nested objects and
create git history from them.  This involves I/O (and thus yielding) at all
levels of the nested loops.

run(function* main() {

  // Configure the repo API to work from a local clone.
  var repo = yield gitRepo({ fs: fs("./test-repo.git"), bare: true,
init: true });
  console.log("Git database Initialized");


  var parent;
  yield* each(commits, function* (message, files) {
// Start building a tree object.
var tree = {};
yield* each(files, function* (name, contents) {
  // Save the file in the database and store the hash in the tree data.
  tree[name] = {
mode: 0100644,
hash: yield repo.save({
  blob: {
length: contents.length,
source: create(contents)
  }
})
  };
});
var commit = {
  tree: yield repo.save({tree: tree}),
  parent: parent,
  author: author + " " + gitDate(new Date),
  committer: committer + " " + gitDate(new Date),
  message: message
};
if (!parent) delete commit.parent;
parent = yield repo.save({commit: commit});
yield repo.updateHead(parent);
  });

});

// Generator friendly forEach for objects
function* each(object, callback) {
  var keys = Object.keys(object);
  for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
yield* callback(key, object[key]);
  }
}


As you can see, I had to create a simple "each" helper that worked like
forEach, but worked in terms of yielding generators.

P.S.  I know you were hoping for the community to standardize on one helper
soon, but I feel it's far to premature for that.  Generators don't work in
any stable release of node and even the unstable releases require a flag to
use it.  There is more to explore in this area, especially generator based
streams once they land in a stable release and not behind a flag.

P.P.S I too wrote a simple generator helper based on the gist I wrote that
galaxy is inspired from.  Mine is called "gen-run"
https://github.com/creationix/gen-run. Another one that seems popular is
TJ's "co" https://github.com/visionmedia/co

-Tim Caswell




On Mon, Jul 15, 2013 at 2:11 AM, cpprototypes wrote:

> Thanks for the replies, I have a better understanding now of how these
> libraries are using generators.  I hope that one of these types of
> libraries becomes as popular as the async library and "standard" within the
> node community.  This way of writing async code is much more elegant than
> the current way of using only callbacks and can help expand node.js usage
> into other areas.  For instance, the script example I wrote is part of a
> larger command line script I was writing to test some services.  Such
> scripting tends to follow a synchronous flow.  I got really frustrated
> while writing it in an async way (since the natural flow is synchronous, it
> maps very poorly to the current callback-only style, even libraries like
> async don't help much).  I eventually gave up and quickly wrote the script
> in python because I had to get the task done for the day.  But I want
> node.js to eventually take python's place in my tool set.  So I went back
> and tried it again with this new generators feature and it was going well
> until I ran into this issue.  Some may say that such use is beyond the
> scope of node.js, but I think JS and node.js have potential to be more than
> just network async programming.
>
>
>
> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>  I found two libraries that should help use existing node callback-based
>> code with generators (suspend and galaxy).  The code I'm trying to run was
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request')**);
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.sp

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread Jake Verbaten
The fact that you can't trivially do deep yields in callbacks is a good
thing.

For example

```
contents.split('\n').forEach(function(id) {
var info = yield
request.get('http://www.example.com?id='+id in python because I had to get the task done for the day.  But I want
> node.js to eventually take python's place in my tool set.  So I went back
> and tried it again with this new generators feature and it was going well
> until I ran into this issue.  Some may say that such use is beyond the
> scope of node.js, but I think JS and node.js have potential to be more than
> just network async programming.
>
>
>
> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>  I found two libraries that should help use existing node callback-based
>> code with generators (suspend and galaxy).  The code I'm trying to run was
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request')**);
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> });
>>
>> Using either library, when node tries to execute the get request ("var
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield
>> request.get..."  I'm guessing that the creation of the new function scope
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not
>> working.
>>
>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscri

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread jmar777
This is probably true (although I'm currently arguing for them anyway in 
es-discuss :)).

Regarding the suspend example, I keep running into this same problem 
myself, and the nested generators are just too clunky to work with for such 
common interactions.  Expect some helper functions in the near future 
(which, granted, will still be nested generators... just sugared up).


On Monday, July 15, 2013 2:30:10 PM UTC-4, Raynos wrote:
>
> The fact that you can't trivially do deep yields in callbacks is a good 
> thing.
>
> For example
>
> ```
> contents.split('\n').forEach(function(id) {
> var info = yield 
> request.get('http://www.example.com?id='+id return request.get.bind(null, 'http://www.examples/com?id='+id)
> }))
> ```
>
> Which is yielding a single thing, where that single thing is do N get 
> requests in parallel.
>
> If we make yielding too easy you will write sequential programs when you 
> really want to write parallel programs.
>
>
> On Mon, Jul 15, 2013 at 12:11 AM, cpprototypes 
> 
> > wrote:
>
>> Thanks for the replies, I have a better understanding now of how these 
>> libraries are using generators.  I hope that one of these types of 
>> libraries becomes as popular as the async library and "standard" within the 
>> node community.  This way of writing async code is much more elegant than 
>> the current way of using only callbacks and can help expand node.js usage 
>> into other areas.  For instance, the script example I wrote is part of a 
>> larger command line script I was writing to test some services.  Such 
>> scripting tends to follow a synchronous flow.  I got really frustrated 
>> while writing it in an async way (since the natural flow is synchronous, it 
>> maps very poorly to the current callback-only style, even libraries like 
>> async don't help much).  I eventually gave up and quickly wrote the script 
>> in python because I had to get the task done for the day.  But I want 
>> node.js to eventually take python's place in my tool set.  So I went back 
>> and tried it again with this new generators feature and it was going well 
>> until I ran into this issue.  Some may say that such use is beyond the 
>> scope of node.js, but I think JS and node.js have potential to be more than 
>> just network async programming.
>>
>>
>>
>> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>>
>>> I'm using node 0.11.3 with --harmony-generators to try the new feature. 
>>>  I found two libraries that should help use existing node callback-based 
>>> code with generators (suspend and galaxy).  The code I'm trying to run was 
>>> similar to the following:
>>>
>>> (using suspend)
>>>
>>> var fs = require('fs');
>>> var request = require('request');
>>>
>>> suspend(function* (resume) {
>>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> var fs = galaxy.star(require('fs'));
>>> var request = galaxy.star(require('request')**);
>>>
>>> function* main() {
>>> var contents = yield fs.readFile('idList.json', 'utf8');
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> });
>>>
>>> Using either library, when node tries to execute the get request ("var 
>>> info = yield request.get...") it exits with the following error:
>>>
>>> SyntaxError: Unexpected identifier
>>>
>>> And the error highlights the "request" part in "var info = yield 
>>> request.get..."  I'm guessing that the creation of the new function scope 
>>> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
>>> working.  
>>>
>>>
>>>  -- 
>> -- 
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nod...@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+un...@googlegroups.com 
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>  
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to nodejs+un...@googlegroups.com .
>> For more 

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread Bruno Jouhier
Parallelizing is not very difficult. Galaxy gives you two ways to do it.

The first one is to call galaxy.spin. This gives you another generator 
function on which you can yield later to get the result. This is very much 
like a future.

var future = galaxy.spin(generatorFunction(args));
// do some awesome stuff while future can run every time you yield
// you can also pass future around, etc.

// somewhere down the line
var result = yield future(); // will throw here if generatorFunction failed.

The second one is to pass an extra argument to forEachStar:

// run 8 loop iterations in parallel (pass -1 if you don't want to limit 
parallelism)
yield contents.split('\n').forEachStar(8, function* (id) {
var info = yield 
request.get('http://www.example.com?id='+id, 
resume);
});

Bruno

On Monday, July 15, 2013 8:30:10 PM UTC+2, Raynos wrote:
>
> The fact that you can't trivially do deep yields in callbacks is a good 
> thing.
>
> For example
>
> ```
> contents.split('\n').forEach(function(id) {
> var info = yield 
> request.get('http://www.example.com?id='+id return request.get.bind(null, 'http://www.examples/com?id='+id)
> }))
> ```
>
> Which is yielding a single thing, where that single thing is do N get 
> requests in parallel.
>
> If we make yielding too easy you will write sequential programs when you 
> really want to write parallel programs.
>
>
> On Mon, Jul 15, 2013 at 12:11 AM, cpprototypes 
> 
> > wrote:
>
>> Thanks for the replies, I have a better understanding now of how these 
>> libraries are using generators.  I hope that one of these types of 
>> libraries becomes as popular as the async library and "standard" within the 
>> node community.  This way of writing async code is much more elegant than 
>> the current way of using only callbacks and can help expand node.js usage 
>> into other areas.  For instance, the script example I wrote is part of a 
>> larger command line script I was writing to test some services.  Such 
>> scripting tends to follow a synchronous flow.  I got really frustrated 
>> while writing it in an async way (since the natural flow is synchronous, it 
>> maps very poorly to the current callback-only style, even libraries like 
>> async don't help much).  I eventually gave up and quickly wrote the script 
>> in python because I had to get the task done for the day.  But I want 
>> node.js to eventually take python's place in my tool set.  So I went back 
>> and tried it again with this new generators feature and it was going well 
>> until I ran into this issue.  Some may say that such use is beyond the 
>> scope of node.js, but I think JS and node.js have potential to be more than 
>> just network async programming.
>>
>>
>>
>> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>>
>>> I'm using node 0.11.3 with --harmony-generators to try the new feature. 
>>>  I found two libraries that should help use existing node callback-based 
>>> code with generators (suspend and galaxy).  The code I'm trying to run was 
>>> similar to the following:
>>>
>>> (using suspend)
>>>
>>> var fs = require('fs');
>>> var request = require('request');
>>>
>>> suspend(function* (resume) {
>>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> var fs = galaxy.star(require('fs'));
>>> var request = galaxy.star(require('request')**);
>>>
>>> function* main() {
>>> var contents = yield fs.readFile('idList.json', 'utf8');
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> });
>>>
>>> Using either library, when node tries to execute the get request ("var 
>>> info = yield request.get...") it exits with the following error:
>>>
>>> SyntaxError: Unexpected identifier
>>>
>>> And the error highlights the "request" part in "var info = yield 
>>> request.get..."  I'm guessing that the creation of the new function scope 
>>> in the forEach is somehow causing an issue.  But I'm not sure why it's not 
>>> working.  
>>>
>>>
>>>  -- 
>> -- 
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send e

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-15 Thread Rick Waldron
On Mon, Jul 15, 2013 at 7:17 PM, jmar777  wrote:

> Hey -
>
> Just realized there's another simple solution to your issue (that should
> work with any of the libraries mentioned in this thread) - just avoid
> nested functions:
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume),
> ids = contents.split('\n');
>
> for (var i = 0, len = ids.length; i < len; i++) {
> var info = yield request.get('http://www.example.com?id=' +
> ids[i], resume);
> }
> })();
>

I've had a for-of example waiting in my drafts, seems like now is a good
time to share (updated for relevance):

suspend(function* (resume) {
  var contents = yield fs.readFile('idList.json', 'utf8', resume),
ids = contents.split('\n');

  for (var id of ids) {
var info = yield request.get('http://www.example.com?id=' + id, resume);
  }
})();


But I can't help it... what the hell is "info" for? Nothing happens with
the yielded return from request.get...?

Rick






>
> Main caveat to the above is that all the requests will happen in serial,
> rather than parallel.  Like I said earlier, suspend will soon be including
> some helper functions to make these scenarios easier (I'll happily accept
> any API suggestions around that, too :)).
>
> On Friday, July 12, 2013 5:47:28 PM UTC-4, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>  I found two libraries that should help use existing node callback-based
>> code with generators (suspend and galaxy).  The code I'm trying to run was
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request')**);
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> });
>>
>> Using either library, when node tries to execute the get request ("var
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield
>> request.get..."  I'm guessing that the creation of the new function scope
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not
>> working.
>>
>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-16 Thread Rick Waldron
On Tue, Jul 16, 2013 at 2:36 AM, cpprototypes wrote:

> The code sample I posted was a simplified example to demonstrate the
> issue.  The actual command line script I was working on uses the response
> from the get request for other things.  That for...of syntax is very nice,
> is it an official part of ECMAScript 6?


Big time. It's already implemented in Firefox ;)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/ECMAScript_6_support_in_Mozilla


Rick


>
>
>
> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>  I found two libraries that should help use existing node callback-based
>> code with generators (suspend and galaxy).  The code I'm trying to run was
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request')**);
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(**function(id) {
>> var info = yield 
>> request.get('http://www.**example.com?id='+id> });
>>
>> Using either library, when node tries to execute the get request ("var
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield
>> request.get..."  I'm guessing that the creation of the new function scope
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not
>> working.
>>
>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-16 Thread Rick Waldron
On Tue, Jul 16, 2013 at 9:13 AM, jmar777  wrote:

> > That for...of syntax is very nice, is it an official part of ECMAScript
> 6?
>
> Yes, although it's not implemented in V8 just yet.  Once for-of and
> generator-expressions are implemented, a lot of these operations will
> become a lot nicer at the language level (rather than relying on cludgy
> `suspend.map()` or whatever helpers).
>
> Here's an example from the ES6 Wiki [1]:
>
> (xhrGet(url) for (url of getURLs())) // hawt
>
> [1] http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions
>

Caution!!!

*"This proposal has progressed to the Draft ECMAScript 6 Specification,
which is available for review here: specification_drafts. Any new issues
relating to them should be filed as bugs at http://bugs.ecmascript.org. The
content on this page is for historic record only and may no longer reflect
the current state of the feature described within"*

I added this to every wiki proposal that is now in the spec—in this case,
the generator expression syntax is backwards!

In the spec draft...

GeneratorComprehension :
  ( Comprehension )


And Comprehension is defined in ArrayComprehension

Comprehension :
  ComprehensionFor ComprehensionQualifierTail

ComprehensionQualifierTail :
  AssignmentExpression
  ComprehensionQualifier ComprehensionQualifierTail

ComprehensionQualifier :
  ComprehensionFor
  ComprehensionIf

ComprehensionFor :
  for (ForBinding of AssignmentExpression )

ComprehensionIf :
  if ( AssignmentExpression )


ForBinding :
  BindingIdentifier
  BindingPattern


Here are the notes from the meeting when this was discussed:
https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#comprehensionsgenerator-syntax


Rick




> On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:
>>
>> The code sample I posted was a simplified example to demonstrate the
>> issue.  The actual command line script I was working on uses the response
>> from the get request for other things.  That for...of syntax is very nice,
>> is it an official part of ECMAScript 6?
>>
>>
>> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>>
>>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>>  I found two libraries that should help use existing node callback-based
>>> code with generators (suspend and galaxy).  The code I'm trying to run was
>>> similar to the following:
>>>
>>> (using suspend)
>>>
>>> var fs = require('fs');
>>> var request = require('request');
>>>
>>> suspend(function* (resume) {
>>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> var fs = galaxy.star(require('fs'));
>>> var request = galaxy.star(require('request')**);
>>>
>>> function* main() {
>>> var contents = yield fs.readFile('idList.json', 'utf8');
>>> contents.split('\n').forEach(**function(id) {
>>> var info = yield 
>>> request.get('http://www.**example.com?id='+id>> });
>>>
>>> Using either library, when node tries to execute the get request ("var
>>> info = yield request.get...") it exits with the following error:
>>>
>>> SyntaxError: Unexpected identifier
>>>
>>> And the error highlights the "request" part in "var info = yield
>>> request.get..."  I'm guessing that the creation of the new function scope
>>> in the forEach is somehow causing an issue.  But I'm not sure why it's not
>>> working.
>>>
>>>
>>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-17 Thread jmar777
> Here are the notes from the meeting when this was discussed: 
https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#comprehensionsgenerator-syntax

Ahh, thanks for the clarification.  So, hopefully correct this time, the 
previous example should have really been:

[for (url of getURLs()) xhrGet(url)]

?

On Tuesday, July 16, 2013 7:38:56 PM UTC-4, Rick Waldron wrote:
>
>
>
>
> On Tue, Jul 16, 2013 at 9:13 AM, jmar777  >wrote:
>
>> > That for...of syntax is very nice, is it an official part of ECMAScript 
>> 6?
>>
>> Yes, although it's not implemented in V8 just yet.  Once for-of and 
>> generator-expressions are implemented, a lot of these operations will 
>> become a lot nicer at the language level (rather than relying on cludgy 
>> `suspend.map()` or whatever helpers).
>>
>> Here's an example from the ES6 Wiki [1]:
>>
>> (xhrGet(url) for (url of getURLs())) // hawt
>>
>> [1] http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions
>>
>
> Caution!!!
>
> *"This proposal has progressed to the Draft ECMAScript 6 Specification, 
> which is available for review here: specification_drafts. Any new issues 
> relating to them should be filed as bugs at http://bugs.ecmascript.org. 
> The content on this page is for historic record only and may no longer 
> reflect the current state of the feature described within"*
>  
> I added this to every wiki proposal that is now in the spec—in this case, 
> the generator expression syntax is backwards!
>
> In the spec draft...
>
> GeneratorComprehension : 
>   ( Comprehension )
>
>
> And Comprehension is defined in ArrayComprehension
>
> Comprehension : 
>   ComprehensionFor ComprehensionQualifierTail
>
> ComprehensionQualifierTail : 
>   AssignmentExpression
>   ComprehensionQualifier ComprehensionQualifierTail
>
> ComprehensionQualifier : 
>   ComprehensionFor
>   ComprehensionIf 
>
> ComprehensionFor :
>   for (ForBinding of AssignmentExpression ) 
>
> ComprehensionIf :
>   if ( AssignmentExpression )
>
>  
> ForBinding : 
>   BindingIdentifier
>   BindingPattern
>
>
> Here are the notes from the meeting when this was discussed: 
> https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#comprehensionsgenerator-syntax
>
>
> Rick
>
>
>
>
>> On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:
>>>
>>> The code sample I posted was a simplified example to demonstrate the 
>>> issue.  The actual command line script I was working on uses the response 
>>> from the get request for other things.  That for...of syntax is very nice, 
>>> is it an official part of ECMAScript 6?
>>>
>>>
>>> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:

 I'm using node 0.11.3 with --harmony-generators to try the new feature. 
  I found two libraries that should help use existing node callback-based 
 code with generators (suspend and galaxy).  The code I'm trying to run was 
 similar to the following:

 (using suspend)

 var fs = require('fs');
 var request = require('request');

 suspend(function* (resume) {
 var contents = yield fs.readFile('idList.json', 'utf8', resume);
 contents.split('\n').forEach(**function(id) {
 var info = yield 
 request.get('http://www.**example.com?id='+id
 );
 });
 };

 galaxy.unstar(main)(function(**err, result) {
 console.log('done');
 });

 Using either library, when node tries to execute the get request ("var 
 info = yield request.get...") it exits with the following error:

 SyntaxError: Unexpected identifier

 And the error highlights the "request" part in "var info = yield 
 request.get..."  I'm guessing that the creation of the new function scope 
 in the forEach is somehow causing an issue.  But I'm not sure why it's not 
 working.  


  -- 
>> -- 
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nod...@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+un...@googlegroups.com 
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>  
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "nodejs" group.
>> To un

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-17 Thread jmar777
Also, FWIW, the LTR version reads much more naturally to me. Happy about 
that change.

On Wednesday, July 17, 2013 8:42:40 AM UTC-4, jmar777 wrote:
>
> > Here are the notes from the meeting when this was discussed: 
> https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#comprehensionsgenerator-syntax
>
> Ahh, thanks for the clarification.  So, hopefully correct this time, the 
> previous example should have really been:
>
> [for (url of getURLs()) xhrGet(url)]
>
> ?
>
> On Tuesday, July 16, 2013 7:38:56 PM UTC-4, Rick Waldron wrote:
>>
>>
>>
>>
>> On Tue, Jul 16, 2013 at 9:13 AM, jmar777  wrote:
>>
>>> > That for...of syntax is very nice, is it an official part of 
>>> ECMAScript 6?
>>>
>>> Yes, although it's not implemented in V8 just yet.  Once for-of and 
>>> generator-expressions are implemented, a lot of these operations will 
>>> become a lot nicer at the language level (rather than relying on cludgy 
>>> `suspend.map()` or whatever helpers).
>>>
>>> Here's an example from the ES6 Wiki [1]:
>>>
>>> (xhrGet(url) for (url of getURLs())) // hawt
>>>
>>> [1] http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions
>>>
>>
>> Caution!!!
>>
>> *"This proposal has progressed to the Draft ECMAScript 6 Specification, 
>> which is available for review here: specification_drafts. Any new issues 
>> relating to them should be filed as bugs at http://bugs.ecmascript.org. 
>> The content on this page is for historic record only and may no longer 
>> reflect the current state of the feature described within"*
>>  
>> I added this to every wiki proposal that is now in the spec—in this case, 
>> the generator expression syntax is backwards!
>>
>> In the spec draft...
>>
>> GeneratorComprehension : 
>>   ( Comprehension )
>>
>>
>> And Comprehension is defined in ArrayComprehension
>>
>> Comprehension : 
>>   ComprehensionFor ComprehensionQualifierTail
>>
>> ComprehensionQualifierTail : 
>>   AssignmentExpression
>>   ComprehensionQualifier ComprehensionQualifierTail
>>
>> ComprehensionQualifier : 
>>   ComprehensionFor
>>   ComprehensionIf 
>>
>> ComprehensionFor :
>>   for (ForBinding of AssignmentExpression ) 
>>
>> ComprehensionIf :
>>   if ( AssignmentExpression )
>>
>>  
>> ForBinding : 
>>   BindingIdentifier
>>   BindingPattern
>>
>>
>> Here are the notes from the meeting when this was discussed: 
>> https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#comprehensionsgenerator-syntax
>>
>>
>> Rick
>>
>>
>>
>>
>>> On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:

 The code sample I posted was a simplified example to demonstrate the 
 issue.  The actual command line script I was working on uses the response 
 from the get request for other things.  That for...of syntax is very nice, 
 is it an official part of ECMAScript 6?


 On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new 
> feature.  I found two libraries that should help use existing node 
> callback-based code with generators (suspend and galaxy).  The code I'm 
> trying to run was similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(**function(id) {
> var info = yield 
> request.get('http://www.**example.com?id='+id var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request')**);
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(**function(id) {
> var info = yield 
> request.get('http://www.**example.com?id='+id });
>
> Using either library, when node tries to execute the get request ("var 
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield 
> request.get..."  I'm guessing that the creation of the new function scope 
> in the forEach is somehow causing an issue.  But I'm not sure why it's 
> not 
> working.  
>
>
>  -- 
>>> -- 
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines: 
>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to nod.

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-17 Thread Rick Waldron
On Wed, Jul 17, 2013 at 8:42 AM, jmar777  wrote:

> > Here are the notes from the meeting when this was discussed:
> https://github.com/**rwldrn/tc39-notes/blob/master/**
> es6/2013-01/jan-31.md#**comprehensionsgenerator-syntax
>
> Ahh, thanks for the clarification.  So, hopefully correct this time, the
> previous example should have really been:
>
> [for (url of getURLs()) xhrGet(url)]
>
>
For a generator comprehension, you had the parens right, but yes, the order
is now correct :)

(for (url of getURLs()) xhrGet(url))

Rick



> ?
>
> On Tuesday, July 16, 2013 7:38:56 PM UTC-4, Rick Waldron wrote:
>>
>>
>>
>>
>> On Tue, Jul 16, 2013 at 9:13 AM, jmar777  wrote:
>>
>>> > That for...of syntax is very nice, is it an official part of
>>> ECMAScript 6?
>>>
>>> Yes, although it's not implemented in V8 just yet.  Once for-of and
>>> generator-expressions are implemented, a lot of these operations will
>>> become a lot nicer at the language level (rather than relying on cludgy
>>> `suspend.map()` or whatever helpers).
>>>
>>> Here's an example from the ES6 Wiki [1]:
>>>
>>> (xhrGet(url) for (url of getURLs())) // hawt
>>>
>>> [1] http://wiki.ecmascript.org/**doku.php?id=harmony:generator_**
>>> expressions
>>>
>>
>> Caution!!!
>>
>> *"This proposal has progressed to the Draft ECMAScript 6 Specification,
>> which is available for review here: specification_drafts. Any new issues
>> relating to them should be filed as bugs at http://bugs.ecmascript.org.
>> The content on this page is for historic record only and may no longer
>> reflect the current state of the feature described within"*
>>
>> I added this to every wiki proposal that is now in the spec—in this case,
>> the generator expression syntax is backwards!
>>
>> In the spec draft...
>>
>> GeneratorComprehension :
>>   ( Comprehension )
>>
>>
>> And Comprehension is defined in ArrayComprehension
>>
>> Comprehension :
>>   ComprehensionFor ComprehensionQualifierTail
>>
>> ComprehensionQualifierTail :
>>   AssignmentExpression
>>   ComprehensionQualifier ComprehensionQualifierTail
>>
>> ComprehensionQualifier :
>>   ComprehensionFor
>>   ComprehensionIf
>>
>> ComprehensionFor :
>>   for (ForBinding of AssignmentExpression )
>>
>> ComprehensionIf :
>>   if ( AssignmentExpression )
>>
>>
>> ForBinding :
>>   BindingIdentifier
>>   BindingPattern
>>
>>
>> Here are the notes from the meeting when this was discussed:
>> https://github.com/**rwldrn/tc39-notes/blob/master/**
>> es6/2013-01/jan-31.md#**comprehensionsgenerator-syntax
>>
>>
>> Rick
>>
>>
>>
>>
>>> On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:

 The code sample I posted was a simplified example to demonstrate the
 issue.  The actual command line script I was working on uses the response
 from the get request for other things.  That for...of syntax is very nice,
 is it an official part of ECMAScript 6?


 On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>
> I'm using node 0.11.3 with --harmony-generators to try the new
> feature.  I found two libraries that should help use existing node
> callback-based code with generators (suspend and galaxy).  The code I'm
> trying to run was similar to the following:
>
> (using suspend)
>
> var fs = require('fs');
> var request = require('request');
>
> suspend(function* (resume) {
> var contents = yield fs.readFile('idList.json', 'utf8', resume);
> contents.split('\n').forEach(**f**unction(id) {
> var info = yield request.get('http://www.**exampl**
> e.com?id='+id  var fs = galaxy.star(require('fs'));
> var request = galaxy.star(require('request'));
>
> function* main() {
> var contents = yield fs.readFile('idList.json', 'utf8');
> contents.split('\n').forEach(**f**unction(id) {
> var info = yield request.get('http://www.**exampl**
> e.com?id='+id  });
>
> Using either library, when node tries to execute the get request ("var
> info = yield request.get...") it exits with the following error:
>
> SyntaxError: Unexpected identifier
>
> And the error highlights the "request" part in "var info = yield
> request.get..."  I'm guessing that the creation of the new function scope
> in the forEach is somehow causing an issue.  But I'm not sure why it's not
> working.
>
>
>

Re: [nodejs] Re: Weird error with generators (using suspend or galaxy)

2013-07-17 Thread Rick Waldron
On Wed, Jul 17, 2013 at 8:43 AM, jmar777  wrote:

> Also, FWIW, the LTR version reads much more naturally to me. Happy about
> that change.


Same here :)


>
>
> On Wednesday, July 17, 2013 8:42:40 AM UTC-4, jmar777 wrote:
>>
>> > Here are the notes from the meeting when this was discussed:
>> https://github.com/**rwldrn/tc39-notes/blob/master/**
>> es6/2013-01/jan-31.md#**comprehensionsgenerator-syntax
>>
>> Ahh, thanks for the clarification.  So, hopefully correct this time, the
>> previous example should have really been:
>>
>> [for (url of getURLs()) xhrGet(url)]
>>
>> ?
>>
>> On Tuesday, July 16, 2013 7:38:56 PM UTC-4, Rick Waldron wrote:
>>>
>>>
>>>
>>>
>>> On Tue, Jul 16, 2013 at 9:13 AM, jmar777  wrote:
>>>
 > That for...of syntax is very nice, is it an official part of
 ECMAScript 6?

 Yes, although it's not implemented in V8 just yet.  Once for-of and
 generator-expressions are implemented, a lot of these operations will
 become a lot nicer at the language level (rather than relying on cludgy
 `suspend.map()` or whatever helpers).

 Here's an example from the ES6 Wiki [1]:

 (xhrGet(url) for (url of getURLs())) // hawt

 [1] http://wiki.ecmascript.org/**doku.php?id=harmony:generator_**
 expressions

>>>
>>> Caution!!!
>>>
>>> *"This proposal has progressed to the Draft ECMAScript 6 Specification,
>>> which is available for review here: specification_drafts. Any new issues
>>> relating to them should be filed as bugs at http://bugs.ecmascript.org.
>>> The content on this page is for historic record only and may no longer
>>> reflect the current state of the feature described within"*
>>>
>>> I added this to every wiki proposal that is now in the spec—in this
>>> case, the generator expression syntax is backwards!
>>>
>>> In the spec draft...
>>>
>>> GeneratorComprehension :
>>>   ( Comprehension )
>>>
>>>
>>> And Comprehension is defined in ArrayComprehension
>>>
>>> Comprehension :
>>>   ComprehensionFor ComprehensionQualifierTail
>>>
>>> ComprehensionQualifierTail :
>>>   AssignmentExpression
>>>   ComprehensionQualifier ComprehensionQualifierTail
>>>
>>> ComprehensionQualifier :
>>>   ComprehensionFor
>>>   ComprehensionIf
>>>
>>> ComprehensionFor :
>>>   for (ForBinding of AssignmentExpression )
>>>
>>> ComprehensionIf :
>>>   if ( AssignmentExpression )
>>>
>>>
>>> ForBinding :
>>>   BindingIdentifier
>>>   BindingPattern
>>>
>>>
>>> Here are the notes from the meeting when this was discussed:
>>> https://github.com/**rwldrn/tc39-notes/blob/master/**
>>> es6/2013-01/jan-31.md#**comprehensionsgenerator-syntax
>>>
>>>
>>> Rick
>>>
>>>
>>>
>>>
 On Tuesday, July 16, 2013 2:36:44 AM UTC-4, cpprototypes wrote:
>
> The code sample I posted was a simplified example to demonstrate the
> issue.  The actual command line script I was working on uses the response
> from the get request for other things.  That for...of syntax is very nice,
> is it an official part of ECMAScript 6?
>
>
> On Friday, July 12, 2013 2:47:28 PM UTC-7, cpprototypes wrote:
>>
>> I'm using node 0.11.3 with --harmony-generators to try the new
>> feature.  I found two libraries that should help use existing node
>> callback-based code with generators (suspend and galaxy).  The code I'm
>> trying to run was similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>> var contents = yield fs.readFile('idList.json', 'utf8', resume);
>> contents.split('\n').forEach(**f**unction(id) {
>> var info = yield request.get('http://www.**exampl**
>> e.com?id='+id > var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request'));
>>
>> function* main() {
>> var contents = yield fs.readFile('idList.json', 'utf8');
>> contents.split('\n').forEach(**f**unction(id) {
>> var info = yield request.get('http://www.**exampl**
>> e.com?id='+id > });
>>
>> Using either library, when node tries to execute the get request
>> ("var info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield
>> request.ge