Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finally use loop variables in nested functions? #3556

Closed
xyrius opened this issue Jul 27, 2014 · 4 comments
Closed

finally use loop variables in nested functions? #3556

xyrius opened this issue Jul 27, 2014 · 4 comments
Labels

Comments

@xyrius
Copy link

xyrius commented Jul 27, 2014

we know in Javascript this bug can be solved with a self executing function with the
loop variables passed as arguments to the nested function, which looks horrible...

I hoped that Coffee solved this and we could do it like below:

show= {}
for method in [ 'upper', 'lower' ]
    show[method]= ( text ) ->
        text= text.toUpperCase() if method is 'upper'
        text= text.toLowerCase() if method is 'lower'
        console.log text

show.upper( 'big!' )  # big!

but, disappointment, the Javascript bug persists..
for now I still can't find another/better way than this:

show= {}
for _method in [ 'upper', 'lower' ] then do ->
    method= _method
    show[method]= ( text ) ->
        text= text.toUpperCase() if method is 'upper'
        text= text.toLowerCase() if method is 'lower'
        console.log text

show.upper( 'big!' )  # BIG!

Note the 'then' after the for loop, as I have to use it now, doesn't make sense to me. It looks almost accidental that the compiler accepts this, of course I can skip 'then' and move the
'do ->' to the line below, but that only adds another redundant line and indentation

@vendethiel
Copy link
Collaborator

Moving to #2518 :).

@nullpotent
Copy link

If that's the real code, I have to say that it smells, badly.
It doesn't even do well for an SSCE Nevertheless, I think I have an answer for you...
An idiomatic approach is to use do, like this:

show= {}
for method in [ 'upper', 'lower' ]
  show[method] = do (method) ->
    return ( text ) ->
      text = text.toUpperCase() if method is 'upper'
      text = text.toLowerCase() if method is 'lower'
    console.log text
show.upper( 'big!' )  # big!

@xyrius
Copy link
Author

xyrius commented Jul 27, 2014

@iccthedral

There you write the anonymus function fix that I hate so much. Please read first before you reply, the first two lines of my request tell enough to make your 'idiomatic' solution for 'me' completely pointless.

Your answer is even less ssce compliant as there is a bug in it and fails if executed. And no, this is not 'real code'... I made this up for the example. Maybe I can please you to change the used names to foo and bar?

@xyrius
Copy link
Author

xyrius commented Jul 27, 2014

@Nami-Doc

Thanks, I already wondered where that discussion took place:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants