Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by gfodor on Saturday August 13, 2011 @05:49PM
(#2882090)
That 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by enneff on Saturday August 13, 2011 @08:59PM
(#2882442)
I'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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by prodigal_erik on Saturday August 13, 2011 @10:11PM
(#2882558)
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'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).
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by zem on Saturday August 13, 2011 @10:18PM
(#2882568)
> Smalltalk definitely belongs on the list, it's the only one that takes "object-oriented" to its logical conclusion (dispatch can replace control structures).
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by eru on Sunday August 14, 2011 @07:25PM
(#2884807)
As 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by phaker on Saturday August 13, 2011 @07:29PM
(#2882284)
You 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.)
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by phaker on Saturday August 13, 2011 @07:51PM
(#2882326)
The 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by jsnell on Saturday August 13, 2011 @07:40PM
(#2882305)
Different definitions. You seem to be thinking of programs that are valid in multiple languages. The page is more of a dictionary of language constructs.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by phaker on Saturday August 13, 2011 @07:55PM
(#2882331)
> The page is more of a dictionary of language constructs.
Which would come incredibly useful if you wanted to write a polyglot program.
Why else would you use a reference like this? If you wanted to learn a language, or had to hack something in a language you don't know, you'll be better off using resources specific to given language(s).
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by roel_v on Sunday August 14, 2011 @09:36AM
(#2883605)
One would use this when writing a new program or modifying an existing one in a language one doesn't know, but when one does know another one well. Seems quite obvious.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by tincholio on Saturday August 13, 2011 @08:06PM
(#2882346)
Maybe you could contact them with the suggestion?
(There's at least couple more errors on the Haskell column, a typo, and a missing idiom for their 'each' example, which would be 'mapM_ print', basically).
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by Udo on Saturday August 13, 2011 @06:34PM
(#2882156)
Neat 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by grncdr on Saturday August 13, 2011 @11:58PM
(#2882765)
Speaking of ridiculously verbose, if they're going to pull in Moose to do OO in Perl they could at least use it's ability to provide default arguments instead of abusing BUILDARGS:
package Int;
use Moose;
has 'value' => ( is => 'rw', isa => 'Int', default => 0 );
no Moose;
Of course, Moose (while awesome) is hardly the only approach to OO in Perl:
package Int;
sub new {
my $cls = shift;
$cls = ref($cls) || $cls;
my $val = shift or 0;
bless \$val, $cls;
}
sub getValue { ${$_[0]} }
1;
Rather than wrapping the scalar int inside another data structure to be treated as an "object" it's possible to just take a reference and use that as our object. So in this case getValue is really just a verbose way to dereference ($int->getValue vs. $$int).
If you don't care about inheritance you could also take out the first two lines of ``new`` and just have:
package Int;
sub new { bless \($_[0] or 0) }
sub getValue { ${$_[0]} }
1;
Disclaimer: this kind of code is usually considered terrible Perl style and why people have such a hang up about the language, don't take this as idiomatic
Edit: Just noticed that they seem to think instance variables in Perl are private by default. Not sure where they got that idea? I guess Moose attributes are read-only by default, but that's hardly the same thing.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by Udo on Sunday August 14, 2011 @03:48AM
(#2883079)
Yes, 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by chc on Sunday August 14, 2011 @06:20AM
(#2883309)
This 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by Udo on Sunday August 14, 2011 @08:07AM
(#2883432)
Everything 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by tvon on Saturday August 13, 2011 @06:30PM
(#2882150)
The 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
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by petercooper on Saturday August 13, 2011 @06:01PM
(#2882112)
x.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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by petercooper on Saturday August 13, 2011 @06:23PM
(#2882139)
I 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.
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by petercooper on Saturday August 13, 2011 @11:10PM
(#2882674)
Not 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™ :-)
Re:Hyperpolyglot: PHP, Perl, Python, Ruby
by benatkin on Saturday August 13, 2011 @06:11PM
(#2882128)
I 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.
Included there:
i believe io does this as well.
Over on the ISWIM-family page the Haskell "command line args" program is ten lines long, when it could be:
I wish these were written by people who knew the languages.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.
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.
It's always the case. That's why writing them is interesting.
Which would come incredibly useful if you wanted to write a polyglot program.
Why else would you use a reference like this? If you wanted to learn a language, or had to hack something in a language you don't know, you'll be better off using resources specific to given language(s).
(There's at least couple more errors on the Haskell column, a typo, and a missing idiom for their 'each' example, which would be 'mapM_ print', basically).
If you don't care about inheritance you could also take out the first two lines of ``new`` and just have:
Disclaimer: this kind of code is usually considered terrible Perl style and why people have such a hang up about the language, don't take this as idiomaticEdit: Just noticed that they seem to think instance variables in Perl are private by default. Not sure where they got that idea? I guess Moose attributes are read-only by default, but that's hardly the same thing.
In contrast to Python, in Ruby all instance member variables are private.
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.
Backticks work:
Constants can be declared globally: Anonymous functions can be declared without create_function: I'm sure there are more. That said, I love these sorts of side-by-side comparisons.> 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.
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.
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.
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™ :-)
http://www.slideshare.net/joestump/starting-your-startup/65
As much as I love Perl, if I want local partners I'd better choose Ruby
python, multiline comment: not quite. print '''foo''' outputs "foo". This is a string literal, and """foo""" is more popular.