News for hackers. Stuff that matters.
Re:Pythonic syntax to declutter Javascript by nir on Thursday January 14, 2010 @09:58PM (#1054134)
Javascript example in the article:

  function triangle(a,b){
    if(a > 0 && b > 0 ){
      function sqroot(x){
        if( x > 0 ){
          return Math.pow(x,.5);
        }
        else {
          return 0;
        }
      }
      return sqroot( a*a + b*b );
    }
    else {
      return 0;
    }
  }

Can be made much cleaner:

  function triangle(a,b){
    if (a <= 0 || b <= 0 ) return 0
    
    function sqroot(x){ return ( x > 0 ? Math.pow(x,.5) : 0 ) }
  
    return sqroot( a*a + b*b )
  }

I don't know about a whitespace-sensitive language in the browser. Much JS is written by non-coders, later quickly edited on live servers (yeah, I know. But that's reality) etc.. It all comes down to taste, of course, but personally I find Ruby at least as readable as Python, so I'm not sure whitespaces are where the elegance is found.
Re:Pythonic syntax to declutter Javascript by TNO on Thursday January 14, 2010 @10:23PM (#1054160)
With JavaScript 1.8.1:

    function triangle(a,b)
        a <= 0 || b <= 0 ? 0 : (a = a*a + b*b) > 0 ? Math.pow(a,.5) : 0;
Re:Pythonic syntax to declutter Javascript by jrockway on Thursday January 14, 2010 @10:57PM (#1054210)
Is ?: really not right-associative in JavaScript? I thought PHP was the only language that messed that one up...
Re:Pythonic syntax to declutter Javascript by TNO on Thursday January 14, 2010 @11:01PM (#1054217)
I wrapped it with parentheses out of habit. updated
Re:Pythonic syntax to declutter Javascript by jrockway on Thursday January 14, 2010 @11:04PM (#1054219)
That's a relief :)
Re:Pythonic syntax to declutter Javascript by WalkingDead on Friday January 15, 2010 @04:06AM (#1054540)
Doesn't work on V8, at least not on the version I am using. Which JS engine is 1.8.1 complaint?
Re:Pythonic syntax to declutter Javascript by sid0 on Friday January 15, 2010 @07:21AM (#1054734)
SpiderMonkey in Firefox 3.5 and above.