7 ms·
Hyperpolyglot: PHP, Perl, Python, Ruby
- gfodor 15y agoThat these languages are so similar a table like this is possible leads one to think those programmers who consider themselves "polyglot" by programming in a subset of these aren't really "polyglot" at all.
- snprbob86 15y agoGo to http://hyperpolyglot.org/ http://hyperpolyglot.org/ directly. Included there: Scripting Languages: PHP, Perl, Python, Ruby Embeddable Languages: Tcl, Lua, JavaScript, Io Shell Languages: Bash, Zsh, AppleScript, PowerShell C Style Languages: C, C++, Objective C, Java, C# Pascal Style Languages: Pascal, Ada, PL/SQL, SQL/PSM Lisp Dialects: Common Lisp, Scheme, Clojure, Emacs Lisp Type Inference Languages: Standard ML, OCaml, Scala, Haskell Declarative Languages: Prolog, Erlang, Oz Concatenative Languages: Forth, PostScript, Factor Computer Algebra Software: Mathematica, Sage, Maxima Numerical Analysis Software: MATLAB, R
- polynomial 15y agoI would have liked for Go to have been included, (C style languages) not to mention Smalltalk.
- enneff 15y agoI'd like to add Go, but I have a couple of reservations. First, I can't edit the page. Second, the list of features in each table is so specific. Go has a lot that the C-like languages in that table don't and vice versa. It seems there aren't enough axes along which to adequately compare.
- prodigal_erik 15y agoSmalltalk definitely belongs on the list, it's the only one that takes "object-oriented" to its logical conclusion (dispatch can replace control structures). I'd suggest omitting Elisp and ObjC—they're both lackluster imitations of better languages that will do far more for the way you think, unless you happen to be developing for their captive platform (Emacs and iOS, respectively).
- zem 15y ago> Smalltalk definitely belongs on the list, it's the only one that takes "object-oriented" to its logical conclusion (dispatch can replace control structures). i believe io does this as well.
- Volpe 15y agoUnless I misunderstand what you mean, don't a host of languages do this? io, ruby, javascript?
- draegtun 15y agoRuby & Javascript have syntax defined in their parsers for control structures. for eg: # Ruby if someThing() doThis() else doThisInstead() end # Javascript/Perl/etc if (someThing()) { doThis() } else { doThisInstead() } Whereas Io & Smalltalk requires no new syntax and uses objects & messages: # Io if(someThing, doThis, doThisInstead) # Io like Ruby,etc if(something) then(doThis) else(doThisInstead) # Io like Smalltalk someThing ifTrue(doThis) ifFalse(doThisInstead) # Smaltalk someThing ifTrue: [doThis] ifFalse: [doThisInstead].
- eru 15y agoAs an aside, Haskell wouldn't need syntax for something like `if', but has it as a convenience. There's no syntax for looping, you can do that with higher-order functions.
- belial 15y ago
- gfodor 15y agoAh didn't notice that. This site suddenly got a whole lot more awesome :)
- rednaught 15y agoNice! I was going to suggest adding Lua and Tcl but they're available on the Embedded page. The link in the title goes to the Scripting page.
- beaumartinez 15y agoNeat, ridiculously comprehensive table. A few "warnings" I found in the first 10%: > Python: Statement separator: ; or sometimes newline More like newline or sometimes ;. When was the last time you saw a Python script with a semicolon? > Python: Multiline comment: '''comment line\nanother line''' That's a multiline string.
- tvon 15y agoThe Python bits do not seem to be written by a Python programmer, at least things like the above and this "package management" entry make little sense: $ python >>> help('modules') download pkg with matching python version $ tar xf libxml2-python-2.6.0.tar.gz $ cd libxml2-python-2.6.0 $ sudo python setup.py install That's just bizarre.
- dougws 15y agoYeah I came here to point this out. I'd understand if pip weren't mentioned, but easy_install has been the standard for many years.
- petercooper 15y agox.succ is not the Ruby equivalent of ++$x in PHP (or Perl). x.succ returns x + 1 (by default, on integers) but does not increment x as ++$x does (since numbers are immutable and that's what "x" is giving us in Ruby). The closest match would be x += 1 (shorthand for x = x + 1). I don't see x = x.succ in common usage but that would work too.
- gb 15y agoI don't disagree with what you're saying, but it does mention in the comment that x is not mutated.
- petercooper 15y agoI totally didn't notice that note but the page doesn't seem to have been updated since then :-) I still don't see why it doesn't just use the common syntax though that does the same thing. It's intriguing that the Python one has no example for that row. Perhaps they are specifically looking for idiomatic techniques, even if they're not commonly used. I note that the page is on a wiki so I could fix it, but given it's been there for years, I suspect there's a good reason no-one has already made the change.
- adyus 15y agoAs a Ruby noob and without researching too much, here's my question: would x.succ! do it?
- petercooper 15y agoNot if x were a regular old Fixnum. The reason is that x would be referring to a number like 1, 42, or whatever, and numbers are immutable in Ruby. You can't "change" a number in-place. If you could change Ruby's concept of 1 into 2, then 1 would cease to exist and that would be a Bad Thing™ :-)
- jfarmer 15y agoYeah, #succ is something else. For example, if your object supports #succ then you can use it in ranges. e.g., (1..10), but also ("apple".."kiwis") or ('A'..'Z'). In this case, String#succ returns a string with the last alphanumeric character transformed into its successor, e.g., "apple".succ == "applef". The only time I've used it is implementing the range interface; I've never called it directly, let alone to increment an integer.
- deleted 15y ago[deleted]
- benatkin 15y agoI think this shows that much of the time, the choice of open source scripting language isn't going to make or break a project. Ditto for well-maintained web frameworks. http://www.slideshare.net/joestump/starting-your-startup/65 http://www.slideshare.net/joestump/starting-your-startup/65
- systems 15y agoWell it may not make or break a start-up but it definitely help push it one direction or the other As much as I love Perl, if I want local partners I'd better choose Ruby
- lubutu 15y agoThese tables are comprehensive, but the authors don't seem to actually understand the languages' idioms. Over on the ISWIM-family page the Haskell "command line args" program is ten lines long, when it could be: import System main = getArgs >>= mapM_ putStrLn I wish these were written by people who knew the languages.
- phaker 15y agoYou can't create 'idiomatic polyglots'. You just hack things together until they sorta work, for more than few languages this basically involves large strings and lots of backslashes, or base64 or something like that. (Writing a polyglot that does something more clever is obviously much better style, but it's incredibly hard.)
- lubutu 15y ago"Polyglot" in this case is referring to a person who can write in multiple languages, not a program written in multiple languages.
- phaker 15y agoThe site doesn't mention which meaning of "polyglot" the people behind it had in mind. I assumed it was for people writing "polyglot" toy programs, because it'd be very useful resource. I very rarely encounter uses of the term "polyglot" referring to people knowing large number of programming languages, so I didn't think of this use. Looking at it now, from this perspective, I have to say that I don't think it would be useful for these people, not enough depth.
- tincholio 15y ago> I assumed it was for people writing "polyglot" toy programs, because it'd be very useful resource. It isn't so in this case, as the code for each language is clearly different. It's more of a 'construct translator' kind of thing.
- Udo 15y agoNeat table, one thing about PHP's member variables compared to Python/Ruby seems inaccurate though. In the example "define class", the member variable $value is declared private and then accessed through a getter and a setter, while the corresponding Python and Ruby examples use a public variable and hence look cleaner. The equivalent PHP version should be more along the lines of: class Int { function __construct($int = 0) { $this->value = $int; } } instead of the ridiculously verbose thing they posted there.
- msbarnett 15y agoThe Ruby example actually involves generating the getter and setter at runtime via attr_accessor. In contrast to Python, in Ruby all instance member variables are private.
- Udo 15y agoYes, but functionally the Ruby example is the equivalent of a public member variable. Since those are public by default in PHP, there is no need to complicate the code by first making it private and then implementing a getter and a setter.
- chc 15y agoThis is a kind of a semantic game. You might say they're "equivalent," but you're still doing different things — and different is very rarely truly equal. For example, the Ruby accessors could be hooked or altered later without changing the interface. Not so if you use a public variable (which Ruby doesn't have, for this very reason). The only language I know of where public variables and accessors are truly interchangeable is Scala.
- Udo 15y agoEverything is a semantics game: meaning matters. This is not about playing "gotcha" with trivialities. I'd like to convince you that there is a significant difference or bias about the way those languages are being showcased. Isn't the whole point of this table to compare equivalent snippets of code? You're right, those languages do things very differently from one another - especially when it comes to OO. In this case, the stated goal was to define a class that exposes a public property. In this I believe PHP and probably Perl were represented badly. If you don't believe me, look at the Python example: it's the exact same implementation I translated into PHP syntax above. So my terse implementation is an exact match for the Python example and it's a functional match for the Ruby example. Hence, I can't help but think that PHP and Perl were misrepresented by making them jump through unnecessary hoops that do nothing but make the language look more complicated.
- olliesaunders 15y agohttp://rosettacode.org/wiki/Rosetta_Code http://rosettacode.org/wiki/Rosetta_Code may be more comprehensive although not as immediately accessible
- nivertech 15y agoREPL for Python: ipython
- georgefox 15y agoSome of the PHP stuff, as others have noted with Python, etc., is misleading or outdated. Backticks work: echo `ls`; Constants can be declared globally: const PI = 3.14; Anonymous functions can be declared without create_function: $f = function($x) { return $x*$x; } I'm sure there are more. That said, I love these sorts of side-by-side comparisons.
- treetrouble 15y agoThere is a lot of non-idiomatic Ruby as well
- k33l0r 15y agoAlso it doesn't tell you that 'and' and 'or' are not equal to && and || in Ruby. http://hyperpolyglot.org/scripting#logical-operators http://hyperpolyglot.org/scripting#logical-operators
- belial 15y agoI've been developing with PHP for 2 years now and I didn't ever notice that we can make anonymous functions with PHP like that!! From where did you get that man??!! :D
- hippich 15y agoI might be incorrect regarding Perl's "scan" function, but I think you can implement it like this: my $s = "foo bar baz"; my @b = $s =~ /\w+/g; Granted, this is not a scanf, but neither are PHP/Python/Ruby examples.
- Qa8BBatwHxK8Pu 15y agoPython dosen't have NULL/NIL?
- dheerosaur 15y agoPython has "None".
- SimHacker 15y agoIt has no null??! Who's on first?
- Kwpolska 15y agopython, statement separator: not "sometimes", but "usually" python, multiline comment: not quite. print '''foo''' outputs "foo". This is a string literal, and """foo""" is more popular.
- smilliken 15y agoUnder variable interpolation, there is nothing listed for python, but the equivalent is this: count = 3 item = "ball" print('%(count)s %(item)ss' % locals())
- akivabamberger 15y agoneeds numpy filled out (for MATLAB/R) but otherwise really cool!