Hyperpolyglot: PHP, Perl, Python, Ruby
hyperpolyglot.org
hyperpolyglot.org
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, Ri believe io does this as well.
# 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].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.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).
class Int {
function __construct($int = 0) {
$this->value = $int;
}
}
instead of the ridiculously verbose thing they posted there.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.
class Int(object):
def __init__(self, value):
self.value = value
@property
def value(self):
print("Accessing value")
return self.value
@value.setter
def value(self, value):
print("Setting value")
self.value = value 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 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.
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.> 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.
$ 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. my $s = "foo bar baz";
my @b = $s =~ /\w+/g;
Granted, this is not a scanf, but neither are PHP/Python/Ruby examples.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™ :-)
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.
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.
count = 3
item = "ball"
print('%(count)s %(item)ss' % locals())