|
Re: Groovy, JRuby, or Scala: Which JVM Language to Learn Next?
|
Posted: Oct 13, 2007 5:16 PM
|
|
> > Hoe about Jython (Python). If I'm not mistaken here's > the Python version.... > > Jython 2.2 supports enumerate(). So one can omit the line > counting boilerplate:...
To make it even more terse (very un-Pythonic and would only run in CPython 2.5 and later):
MAXLENGTH=4
for filename in sys.argv[1:]: for l in ("%s line=%d chars=%d" % (filename, n, len(line)) for n, line in enumerate(file(filename)) if len(line) > MAXLENGTH): print l
Also, the question is whether this example is a good one for comparison?
One that involves some basic error handling and resource management would be more appropriate.
The following C++ code provides equivalent functionality including automatic resource management (in this case files). It isn't as horrible as I would have expected it to be.
const int MAXLENGTH = 4;
int main(int argc, char* argv[]) { for (int i=1; i<argc; i++) { std::ifstream ifs(argv[i]); std::string l; int n = 1; while (std::getline(ifs, l)) { if (l.size()>MAXLENGTH) std::cout << filename << " line=" << n << " chars=" << l.size() << std::endl; n++; } } return 0; }
Now, what would happen if we add some simple error testing and recovery to the above? How would the code complexity and ugliness increase?
Comparison of the rate of growth of ugliness/verbosity with functionality might be a better way to compare terseness.
I think I am straying too far from the Java platform centric nature of this discussion. So I'll stop now.
That said, I think I will go with Scala, because it is the only one that gives me the good things from both the OO and functional worlds without weird syntax, and has slightly better integration with the Java platform.
|
|