Giter Club home page Giter Club logo

Comments (7)

jashkenas avatar jashkenas commented on July 3, 2024

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.

weepy avatar weepy commented on July 3, 2024

do we need to worry about the differences, since objects and arrays basically work the same when enumerated?

  1. for k,v in obj

  2. for k,v in array
    k would work like each_with_index (nice if the key was an integer rather than a string?)

  3. for v in array
    as it is currently

  4. far v in object
    would enumerate the values (probably not used much)

from coffeescript.

jashkenas avatar jashkenas commented on July 3, 2024

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.

weepy avatar weepy commented on July 3, 2024

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.

weepy avatar weepy commented on July 3, 2024

or perhaps could use a for .. in loop with a check against obj instanceof Array && !isNaN(index) ??

from coffeescript.

weepy avatar weepy commented on July 3, 2024

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.

jashkenas avatar jashkenas commented on July 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.