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.
Re:Pythonic syntax to declutter Javascript by malkia on Thursday January 14, 2010 @09:36PM (#1054105)
Nothing against python, but pyscript is terrible idea to go in the browser? Why? Because you can't remove the spaces, you can't compress your source code so that it uses less bandwidth.

Also really, honestly, I hate to post python script somewhere in forum, and because of the identation someone else cannot test it.

Hey there is [email protected] - right - you type something in google.wav - send it to the address above, and the clojure appspot program gives you back the result - nice REPL :)

Now how would that work with python, or this pyscript? Just thing about it?

Re:Pythonic syntax to declutter Javascript by sshumaker on Thursday January 14, 2010 @09:49PM (#1054117)
I would imagine you pre-process the pyscript before sending it to the browser as part of your deploy step. So you don't have the compression issues.
Re:Pythonic syntax to declutter Javascript by harto on Thursday January 14, 2010 @09:57PM (#1054129)
Actually, it looks like a separate script translates all script blocks with type="text/pyscript"
Re:Pythonic syntax to declutter Javascript by Semiapies on Friday January 15, 2010 @01:20PM (#1055464)
You can't minify, but you can still gzip.
Re:Pythonic syntax to declutter Javascript by harto on Thursday January 14, 2010 @09:54PM (#1054125)
This is reminiscent of those occasional attempts to "fix" Lisp's parentheses. In practise, I've never found JavaScript's braces and semicolons distracting. I just don't notice them that much.
Re:Pythonic syntax to declutter Javascript by mahmud on Thursday January 14, 2010 @11:13PM (#1054235)
JavaScript has been fixed, and its new name is jQuery.
Re:Pythonic syntax to declutter Javascript by ubernostrum on Thursday January 14, 2010 @11:40PM (#1054277)
jQuery only attempts to deal with the DOM. Confusing a browser API and a programming language is a mistake to be avoided.
Re:Pythonic syntax to declutter Javascript by clemesha on Friday January 15, 2010 @12:29AM (#1054337)
Broadly speaking, I honestly agree with this.
Re:Pythonic syntax to declutter Javascript by endtime on Thursday January 14, 2010 @08:03PM (#1053979)
Awesome, thanks. Going to see if I can use this for my crypto class (the project is to build an encryption system on top of Twitter, using GreaseMonkey).

Also - reminds me of CoffeeScript (which has similarly clean (if less Pythonic) syntax, but has to be compiled to Javascript, I believe). http://jashkenas.github.com/coffee-script/#overview

Re:Pythonic syntax to declutter Javascript by stcredzero on Friday January 15, 2010 @11:08AM (#1055119)
The stark modern minimalism of the language causes the meaning of code to float on the syntax like a feather on water.

I think Python is dandy. I will be starting a new project on it, actually. But "stark" and "modern" are relative. While there's a lot of very pretty Python that reads like pseudocode, there's also a bit of Python that goes down the same sinful path as C header files or Perl in the use of arcane symbols. There's no reason why things like meta-programming have to look like anything different from plain old programming.

That said, I think Python hits a very attractive middle ground. There is just enough syntax to fit the most basic cultural expectations of technical people as well as a large number of programmers. That, if you think about it, is a stunning achievement in human-interface design. (Read that straight-up!)

(And to be fair: Smalltalk - even more minimal, but ignores lots of important cultural expectations. Lisp - even more minimal and just about everything, no matter how meta, looks like normal Lisp. But on the downside, everything looks like normal Lisp.)

Re:Pythonic syntax to declutter Javascript by andybak on Thursday January 14, 2010 @08:37PM (#1054024)
Whatever anyone can say about Python you can't deny how nice it looks on the page.
Re:Pythonic syntax to declutter Javascript by Semiapies on Thursday January 14, 2010 @08:17PM (#1053994)
Very alpha (the lack of anonymous functions is a killer even for experimental use), but it is clean-looking. A project to watch or jump into.
Re:Pythonic syntax to declutter Javascript by __david__ on Thursday January 14, 2010 @08:37PM (#1054025)
Yuck. I appreciate Python as much as the next guy, but explicit curlies give you way more flexibility in the appearance of your code. Yes, that can be good or bad but so is anything that gives you more power.

Not to mention that in his samples half the curlies were unnecessary.

Re:Pythonic syntax to declutter Javascript by JeffJenkins on Thursday January 14, 2010 @10:11PM (#1054147)
Seems like you might be better off using the python->JS converted in pyjamas and writing things in straight python:

http://pyjs.org/

Re:Pythonic syntax to declutter Javascript by s3graham on Thursday January 14, 2010 @10:44PM (#1054195)
I wrote a browser one too http://skulpt.org/

I'm not sure if it's better to write in real Python or just know that you're using JS with different syntax ala pyscript. Emulating all the bizarre Python attribute access behaviour in JS (e.g. __get__) is quite complex.

Re:Pythonic syntax to declutter Javascript by ash on Friday January 15, 2010 @08:00AM (#1054779)
It can be educating to look at the history of Reia (Python/Ruby-like language for Erlang VM). At first its author tried to hard to make Reia indentation sensitive (like Python). Finally he reconsidered after discovering several limitiations: http://www.unlimitednovelty.com/2009/03/indentation-sensitiv...
Re:Pythonic syntax to declutter Javascript by donaq on Thursday January 14, 2010 @09:27PM (#1054093)
Hmm, a language interpreted by a parser implemented in a scripting language. Won't that be rather slow? I'd be more comfortable with using it as a compiler that outputs js I can then include, rather than letting clients' browsers interpret it on the fly.
Re:Pythonic syntax to declutter Javascript by tumult on Thursday January 14, 2010 @10:41PM (#1054187)
I imagine it does the parse up-front rather than a separate interpretation step during execution.
Re:Pythonic syntax to declutter Javascript by donaq on Friday January 15, 2010 @12:23AM (#1054333)
Granted, but that's still an extra step that I wouldn't want clients to have to take.
Re:Pythonic syntax to declutter Javascript by jrockway on Thursday January 14, 2010 @11:03PM (#1054218)
A parser implemented in a scripting language

What's a "scripting language"?

Won't that be rather slow?

Firefox is implemented in JavaScript. It's pretty fast, and a little more complicated than a parser.

Re:Pythonic syntax to declutter Javascript by pgbovine on Thursday January 14, 2010 @11:55PM (#1054298)
Firefox is implemented in JavaScript

i'm pretty sure that much of the performance-critical core is in C++; javascript is th UI/extensions layer, to the best of my knowledge

Re:Pythonic syntax to declutter Javascript by ZeroGravitas on Friday January 15, 2010 @07:18AM (#1054729)
They've been talking for years about written more and lower layers with Javascript. I'm not sure how far along they've got, it would be interesting to know.

Obviously parts are C++ but isn't interfacing with such code one of the defining characteristics of a "scripting language" and javascript in particular?

Re:Pythonic syntax to declutter Javascript by jrockway on Friday January 15, 2010 @02:22AM (#1054444)
Vimperator and Conkeror do some pretty complicated manipulation in pure JavaScript. (Sure, the rendering core is in C++, but that's because C++ was all they had when it was first developed.)
Re:Pythonic syntax to declutter Javascript by donaq on Friday January 15, 2010 @12:28AM (#1054336)
What's a "scripting language"?

Or interpreted language, if the nomenclature is that important.

Re:Pythonic syntax to declutter Javascript by jrockway on Friday January 15, 2010 @02:21AM (#1054443)
JavaScript in Firefox is compiled to native machine code (if you enable content.jit, that is.)
Re:Pythonic syntax to declutter Javascript by donaq on Friday January 15, 2010 @01:15PM (#1055442)
That's an interesting tidbit. Thanks.