News for hackers. Stuff that matters.
Re:Dropbox dives into CoffeeScript by sync on Thursday September 13, 2012 @03:57PM (#4518237)
Big, big fan of CoffeeScript and glad to see Dropbox hopping aboard.

That being said, some of their examples are lackluster.

  @originalStyle = {}
  for k in ['top', 'left', 'width', 'height']
    @originalStyle[k] = @element.style[k]
Should really be something like:

  @originalStyle = ['top', 'left', 'width', 'height'].reduce (hash, position) -> 
    hash[position] = @element.style[position]
    hash
  , {}
... though that shows off some CoffeeScript warts.

Also,

  Sharing =
    init: (sf_info) ->
      for list in [sf_info.current, sf_info.past]
        for info in list
          @_decode_sort_key info
Why aren't they using CoffeeScript classes?

  class Sharing
    constructor: (sfInfo) ->
      ...
Re:Dropbox dives into CoffeeScript by gfodor on Thursday September 13, 2012 @04:08PM (#4518291)
On the former, I think you'll find most people find their implementation more readable/idiomatic, and the second case I'd guess is because they were doing a port and so probably didn't want to make too many semantic leaps.
Re:Dropbox dives into CoffeeScript by lowboy on Thursday September 13, 2012 @05:21PM (#4518685)
Why are you bringing reduce() into this? Seems to fly in the face of its common purpose, stated on the MDN page: Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.

By doing it this way, you've introduced another symbol for us to consider (hash) and by departing from the standard for loop format, you've made it less idiomatic, which means more people will have to expend mental energy parsing it.

Re:Dropbox dives into CoffeeScript by alexangelini on Thursday September 13, 2012 @04:10PM (#4518305)
Your first example is the exact reason I dislike CoffeeScript. Sure it's clever, but at a glance it is much harder to determine exactly what's going on.
Re:Dropbox dives into CoffeeScript by csense on Thursday September 13, 2012 @04:58PM (#4518569)
Parent's reduce() example is harder to read than the original, and about the same length. If CoffeeScript had dictionary comprehensions like Python 2.7 / 3.x, we could make the code shorter while preserving and easier to comprehend:

  @originalStyle = {k : @element.style[k] for k in ['top', 'left', 'width', 'height'}
Unfortunately this syntax was proposed and rejected [1]. But with a simple to_hash library function, you can achieve something similar [2]:

  to_hash = (pairs) ->
     hash = {}
     hash[key] = value for [key, value] in pairs
     hash

  @originalStyle = to_hash ([k, @element.style[k]]
     for k in ['top', 'left', 'width', 'height'])
[1] https://github.com/jashkenas/coffee-script/issues/467

[2] https://gist.github.com/2271874

Re:Dropbox dives into CoffeeScript by rgarcia on Thursday September 13, 2012 @04:17PM (#4518352)
Really they should just use underscore:

    @originalStyle = _(@element.style).pick ['top', 'left', 'width', 'height']
Re:Dropbox dives into CoffeeScript by mcantor on Thursday September 13, 2012 @08:14PM (#4519354)
What's with the dangling , {} on your first example? I can't tell if it's a typo or a weird wrapped line rendering issue or something. I hope that's not considered idiomatic CoffeeScript.
Re:Dropbox dives into CoffeeScript by alinajaf on Thursday September 13, 2012 @05:27PM (#4518719)
Just another datapoint, but contrary to the other comments here I find your reduce example a lot more readable than the original. This could be because it's fairly idiomatic ruby.
Re:Dropbox dives into CoffeeScript by byroot on Thursday September 13, 2012 @09:12PM (#4519535)
For me this in idiomatic ruby would be:

  original_style = element.style.only('top', 'left', 'width', 'height')
Or if you do not want to rely on ActiveSupport:

  original_style = Hash[%w(top left width height).map{ |p| [p, element.style[p]] }]
Re:Dropbox dives into CoffeeScript by alinajaf on Friday September 14, 2012 @02:21AM (#4520336)
Without ActiveSupport I'd probably type something like this:

  %w(top left width height).reduce({}) { |memo, pos| memo[position] = @element.style[position] and memo }
Re:Dropbox dives into CoffeeScript by nahname on Thursday September 13, 2012 @05:07PM (#4518622)
It says they mostly just ported the JS over using JS2Coffee. Maybe they plan to go back through everything and actually convert it over to coffeescript?