1
\$\begingroup\$

I haven't spent too much time with CoffeeScript and am trying to have a simple counter:

casper.on('step.complete', ( ->
        i = 0
        return (step) ->
            i += 1
            @echo "Step#{ i }"
    )())

Which works fine but seems to have a bit too many parenthesis for CoffeeScript.

\$\endgroup\$
3
\$\begingroup\$

You could do this:

casper.on 'step.complete', do -> # invoked right away
  i = 0
  -> @echo "Step#{ ++i }" # implicitly returned function

Parentheses: None :-)

The do keyword is the equivalent of (-> ...)(), i.e. it means "invoke the following function immediately" (it's also useful to avoid closure pitfalls in loops, by the way.)

The ++i will increment i before it's echo'ed, and CoffeeScript has implicit return, so the return keyword can be skipped.

Lastly, I left out the step function argument, as you're not using it for anything; you're always adding 1. You can always add it back in of course

\$\endgroup\$
1
\$\begingroup\$

I would certainly distangle the code and create a named function instead of an anonymous one.

step_creator = ->
    i = 0
    return (step) ->
        i += 1
        @echo "Step#{ i }"

casper.on 'step.complete', step_creator()
\$\endgroup\$
  • 1
    \$\begingroup\$ This is still an anonymous function. \$\endgroup\$ – meagar Mar 18 '13 at 19:31
  • 1
    \$\begingroup\$ @meagar, one of the functions is no longer anonymous, isn't it? \$\endgroup\$ – Winston Ewert Mar 19 '13 at 3:21
  • 2
    \$\begingroup\$ @meagar, isn't that an implementation detail? From the perspective of the programmer, he's creating a function named step_creator. \$\endgroup\$ – Winston Ewert Mar 19 '13 at 15:43
  • 1
    \$\begingroup\$ @meagar, some versions of CoffeeScript actually would compile it as a named function, they don't do it now because of bugs in IE. So I submit that it is in fact an implementation detail. From the perspective of the CoffeeScript programmer whether its a named or anonymous javascript function doesn't matter. \$\endgroup\$ – Winston Ewert Mar 19 '13 at 16:28
  • 1
    \$\begingroup\$ @meagar, I'm saying that what it compiles into is irrelevant. In C++, a for loop compiles into a number of branches and conditions. But that's an implementation detail. It would be silly to claim that C++ doesn't really have a for loop because its compiled into conditions and branches. So I think its equally incorrect to claim that CoffeeScript doesn't have named function simply because they are always compiled into anonymous functions in Javascript. \$\endgroup\$ – Winston Ewert Mar 19 '13 at 16:58
0
\$\begingroup\$

A further tweak to Flambino's answer https://codereview.stackexchange.com/a/24054/27783:

casper.on 'step.complete', do (i=0) ->
    -> @echo "Step#{ ++i }"

which compiles to:

casper.on('step.complete', (function(i) {
  return function() {
    return this.echo("Step" + (++i));
  };
})(0));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.