News for hackers. Stuff that matters.
Re:C++ has become a scripting language by rofrol on Wednesday October 16, 2013 @08:43AM (#6559315)
you don't need hashbang if you run it the same like python:

  sh script.sh
  python script.py
So one line vs two lines

  import sys
  open(sys.argv[1],'w').writelines(sorted(open(sys.argv[2]).readlines()))
Yep, bash is more readable for small scripts
Re:C++ has become a scripting language by tyrion on Wednesday October 16, 2013 @11:38AM (#6560238)
> Yep, bash is more readable for small scripts

I don't agree with that, in my opinion bash is more coincise while python is far more explicit and thus more understandable.

Python is not meant to be written in one line; the script could be rewritten in a readable way like this:

  import sys
  
  inp = open(sys.argv[1], 'r')
  out = open(sys.argv[2], 'w')
  
  out.writelines(sorted(inp))
Re:C++ has become a scripting language by brian_cloutier on Wednesday October 16, 2013 @04:56PM (#6562119)
This is my favorite python implementation in this thread. I daresay it's beautiful.

Including whitespace it's still half as long as the "11 line" core of the C++ script.

There's an unnecessary 'r'. Combined with the line rhythm established by opening the files in consecutive lines you ensure readers who would have been confused by the 'w' will quickly understand.

And the last line reads like programmer English; by which I mean English with a SVO word order. Not intuitive to the average person but quite parsable by anybody who's used a modern imperative language.

I can't even nitpick your choice of inp instead of input, the rhythm you setup and the contrast with 'out' means it's quite obvious what you mean.

Have an upvote.

Re:C++ has become a scripting language by ksrm on Wednesday October 16, 2013 @09:01PM (#6563066)
English is usually SVO; I assume you meant OVS?
Re:C++ has become a scripting language by hcarvalhoalves on Thursday October 17, 2013 @10:00AM (#6565349)
Obligatory functional style:

    import sys

    reduce(lambda i, o: o.writelines(sorted(i)),
           map(lambda args: open(*args),
               zip(sys.argv[1:], ('r', 'w'))))
If Guido didn't despised FP so much maybe we could get a nicer lambda syntax... but here's anyway.
Re:C++ has become a scripting language by tyrion on Tuesday October 22, 2013 @09:45PM (#6596222)
In my opinion using reduce, map and zip is not a good idea in this case. What are they needed for? I don't even think your approach is more functional than the examples above.

I mean, this one line should be equally functional and .. it's shorter and even more understandable:

  open(sys.argv[2], 'w').writelines(sorted(open(sys.argv[1]))
(Btw you're talking about a nicer lambda syntax but imho your example looks ugly because of all the unneeded stuff you've put into it)
Re:C++ has become a scripting language by 6ren on Wednesday October 16, 2013 @08:58AM (#6559368)
faster too

  $ time sort < t > t-sh
  real    0m0.016s
  user    0m0.008s
  sys     0m0.008s

  $ time py -c "import sys; open(sys.argv[1],'w').writelines(sorted(open(sys.argv[2]).readlines()))" t-py t
  real    0m0.088s
  user    0m0.056s
  sys     0m0.012s
BTW also a different result (I curled this page for the data).
Re:C++ has become a scripting language by tyrion on Wednesday October 16, 2013 @11:31AM (#6560202)
It is faster mainly because of the startup time of the python interpreter. You can improve performances using the "-S" on the python interpreter. I get inconsistent results with time, I think maybe because of context switching as stated here [1].

See my results here: http://pastebin.com/HMUErzee

1: http://stackoverflow.com/questions/9006596/is-the-unix-time-...

Re:C++ has become a scripting language by rofrol on Wednesday October 16, 2013 @12:36PM (#6560600)