6 ms·
Sure, it's roughly readable, but even here Perl makes several aspects of the loop implicit and buried in syntax (the line variable, stdin as source of the lines
by lars512 16y ago
Sure, it's roughly readable, but even here Perl makes several aspects of the loop implicit and buried in syntax (the line variable, stdin as source of the lines).
- pyre 16y agoPerl doesn't make it implicit, the programmer does. It could easily be rewritten as: foreach my $line (<STDIN>) { foreach my $word (split($line)) { print "$word\n" if $word =~ /ing$/; } } The programmer made a conscious decision to read STDIN implicitly in a while loop and then call split with implicit usage of $_, then went on to complain that too much was hidden away. I could make the python example equally obtuse: import sys, re for a in sys.fopen(sys.stdin.fileno(),'r',1): for b in re.split(a,r''): if re.search(b,r'.*ing$') print b Compressed forms: # PERL map { print "$_\n" } grep { /ing$/ } map { split($_) } <STDIN>; # PYTHON import sys for a in [ a for a in [ b for a in sys.stdin for b in a.split() ] if a.endswith('ing') ]: print a # PYTHON import sys print "\n".join(filter(lambda x: x.endswith('ing'), [ var2 for var1 in sys.stdin for var2 in var1.split() ]))
- lars512 16y agoIndividual programmer style does matter, but languages also guide our style. I'm often asked to convert Perl scripts to Python scripts, and the originals don't exhibit the "optional readability" you're advertising, and which Python encourages by default. An entirely different angle on all of this is that NLTK is aimed at novice programmers (linguists) as well as expert programmers (CS students). Perhaps readability is a different concept for these two user groups...
- phaylon 16y agoWell, since Perl was first designed by a linguist, one might think they aren't all of one mindset :)
- l0nwlf 16y ago"There should be one-- and preferably only one --obvious way to do it." and your code mylord does not passes the bill. Also I do agree with OP, python support readability and it make sense to choose it over other languages where clarity matters. Every language have their own use-case, python suits here quite well.
- kscaldef 16y agoWell, of course, when you assume that the design criteria of Python are the correct ones you reach the conclusion that Python is the preferred language to use. However, even Python fails here. I would say it's "obvious" that you should use filter rather than the for/if combination. Perhaps to someone else it's "obvious" that you should use a list comprehension.
- sigzero 16y agoIf find that once you get beyond the "easy" in code there is rarely only "one obvious way" to do it.