Comments (7)
Yes. I've been thinking about object comprehensions for a while -- the problem is with passing a variable as the comprehension source. Eg:
values: val for val, key in object.
We don't know at compile time if "object" is a JS array or object -- we can generate code that sniffs to tell, but it would probably be long and ugly. Alternately, we could use a different keyword for object comprehensions than "for", but that would be ugly in a different way.
Any ideas?
from coffeescript.
do we need to worry about the differences, since objects and arrays basically work the same when enumerated?
-
for k,v in obj
-
for k,v in array
k would work like each_with_index (nice if the key was an integer rather than a string?) -
for v in array
as it is currently -
far v in object
would enumerate the values (probably not used much)
from coffeescript.
for item in array
isn't compatible with Prototype.js, which is a problem. (Otherwise Prototype and CoffeeScript would go real nicely together). Also, the ordering -- for arrays you want the element to be the first argument, followed by the index. For objects you generally want the key to be first, although some frameworks follow array style with the value first and the key optional -- Underscore.js's iterators do this. Not sure how much of a problem any of that is. It would be nice not to completely break Prototype, though.
from coffeescript.
I dont see a problem with having the key second - in many ways it makes sense. It would certainly be a shame to break prototype - (grr for adding to Array.prototype!)
I think it might break your 'no std-lib' rule, but we could write some kind of iterator function, that used "instanceof Array" to switch. It might tidy up some of the code (__a etc everywhere), but there might be some ramifications I haven't thought of.
Something along the lines of:
function __iterate(_this, obj, fn, guard) { var i; function run(i, v) { with(_this) { if(!guard || guard(i, v) fn(i,v) } } if(obj instanceof Array) { for(i=0; i < obj.length; i++) run(i, obj[i]) } else { for(i in obj) run(i, obj[i]) } } count(i, v) for i in list => __iterate(this, list, count)
from coffeescript.
or perhaps could use a for .. in
loop with a check against obj instanceof Array && !isNaN(index)
??
from coffeescript.
or could use obj.hasOwnProperty(index) as a guard. See Resig's post here http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-an-attribute-in-javascript
This would also guard against methods being added to Object.prototype
from coffeescript.
Good idea on the hasOwnProperty -- it'll significantly slow down large comprehensions, but that's ok. Your suggested (for .. in) method is now the way that array/object comprehensions work on master. You can give it a try.
nums: {one: 1, two: 2, three: 3}
counting: key + '!' for val, key in nums
print(counting.join(' '))
Prints: one! two! three!
Closing the ticket... Let me know if you run into any issues and I'll reopen it.
from coffeescript.
Related Issues (20)
- Bug?: Cannot Base64 encode value: 0 (Legacy browser/WSH) HOT 2
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.