Update global array using list comprehension

51 views
Skip to first unread message

Zhemin Lin

unread,
Aug 20, 2013, 5:07:00 AM8/20/13
to lives...@googlegroups.com
Hi.
I'm trying to rewrite the samples from "node.js: up and running" into LiveScript as a practice.
However, I found array.splice somewhat ugly:

I'd like to write it somewhat python-like, say,

(Ugly:)
client.on \end !->
      clientList.splice clientList.indexOf(client), 1

 
14     client.on('end', function(){
 
15       clientList.splice(clientList.indexOf(client), 1);
 
16     });


(Better but doesn't work:)
client.on \end !->
      clientList = [i for i in clientList when i isnt client]

 
14     client.on('end', function(){
 
15       var clientList, res$, i$, len$, i;   // Wrong!  clientList should not be here
 
16       res$ = [];
 
17       for (i$ = 0, len$ = clientList.length; i$ < len$; ++i$) {
 
18         i = clientList[i$];
 
19         if (i !== client) {
 
20           res$.push(i);
 
21         }
 
22       }
 
23       clientList = res$;
 
24     });


How should I make it work?
Thanks!

Zhemin Lin

unread,
Aug 20, 2013, 5:22:38 AM8/20/13
to lives...@googlegroups.com
I made up a version with list comprehension.
However, I find ~> and @ somewhat confusing and the compiled code is not concise.

Zhemin Lin

unread,
Sep 6, 2013, 1:22:10 PM9/6/13
to lives...@googlegroups.com
Thanks Sorella at #livescript.
I can now use := instead of = to update the list in an outer scope.
The script looks prettier :)

Reply all
Reply to author
Forward
0 new messages