News for hackers. Stuff that matters.
Re:C++ has become a scripting language by brian_cloutier on Wednesday October 16, 2013 @08:17AM (#6559158)
> Ignoring include boilerplate and the like leaves us with roughly ten lines of code.

I'm not sure you get to claim a language is a scripting language and then ignore the boilerplate.

The equivalent python is 4 lines, just one more than the number of steps you're performing.

  import sys
  data = open(sys.argv[1]).readlines()
  lines = sorted(data)
  open(sys.argv[2], 'w').writelines(lines)
C++ is good at many things, but quickly creating readable scripts and live-coding with a REPL are not among them.
Re:C++ has become a scripting language by reirob on Wednesday October 16, 2013 @08:30AM (#6559230)
In Unix shell makes it in 2, with the bang line:

  #!/bin/sh
  sort < $1 > $2
My preferred scripting language ;)
Re:C++ has become a scripting language by usea on Wednesday October 16, 2013 @09:17AM (#6559440)
Requisite C# one-liner:

  Main(string[] args) { File.WriteAllLines(args[1], File.ReadAllLines(args[0]).OrderBy(x => x).ToArray()); }
Excludes using statements, .net library references. project file, solution file, framework config file...

I have invented a language called Croml, in which all source code compiles to the same program. This program reads in a file, sorts the lines, and writes it back out to another file. An empty source file accomplishes this task.

Re:C++ has become a scripting language by frozenport on Thursday October 17, 2013 @03:30AM (#6564079)
You are cheating because of all the menus you had to jump through just to make the project :-)
Re:C++ has become a scripting language by foobarred on Thursday October 17, 2013 @04:17AM (#6564179)
Use LinqPad then it's not cheating. It becomes:

File.WriteAllLines(args[1], File.ReadAllLines(args[0]).OrderBy(x => x));

Re:C++ has become a scripting language by usea on Thursday October 17, 2013 @02:36PM (#6567286)
I did actually use LINQPad. My comment was in jest :P

However, there's no Main in LINQPad which takes string[] args. Also File.WriteAllLines takes an array as its second parameter, while OrderBy returns an IEnumerable.

Re:C++ has become a scripting language by facorreia on Wednesday October 16, 2013 @04:19PM (#6561911)
Don't forget the quotes...

    sort < "$1" > "$2"
Re:C++ has become a scripting language by bcoates on Wednesday October 16, 2013 @03:28PM (#6561599)
It'll work on files (much) larger than RAM, too.
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)
Re:C++ has become a scripting language by pestaa on Wednesday October 16, 2013 @08:34AM (#6559262)
Absolutely yes, up to the point you're working with text. When objects (either implicit or explicit) start to interact with each other, shell scripting falls short quickly.
Re:C++ has become a scripting language by kamaal on Wednesday October 16, 2013 @10:01AM (#6559668)
'Text' covers a very large domain. Once you get any data to a ordered text form, Unix command line text processing utilities have literally zero competition when it comes succinctness, power and how quickly you can create solutions.

Utility of shell goes down. Not because problems are represented as 'Text', but rather shell languages lack features like exception handling, proper error checking and many other things- Which make it difficult to write large programs in it.

As a next extension, you can learn Perl.

I assure you, after that you will not need anything ever.

Re:C++ has become a scripting language by aboodman on Wednesday October 16, 2013 @03:29PM (#6561610)
> I assure you, after that you will not need anything ever.

I love it when people say things like this. As is their domain is the only domain in all programming.

For surely, you could write a competitive web browser in Perl. Or a first person shooter. Or a telecom system. Or a mars orbiter.

It makes you wonder why anyone ever invented anything else!

Re:C++ has become a scripting language by recuter on Wednesday October 16, 2013 @03:50PM (#6561747)
I read his post to mean he is talking about the 'Text' domain of things, not everything. He claims you won't want anything beyond Perl if/when you stick to handling text.
Re:C++ has become a scripting language by aboodman on Wednesday October 16, 2013 @04:43PM (#6562058)
Oh. On re-reading, that makes sense. I recant my snark.
Re:C++ has become a scripting language by recuter on Wednesday October 16, 2013 @06:34PM (#6562511)
I don't actually agree with him but I just wanted to thank you for posting the recant. :)
Re:C++ has become a scripting language by marshray on Thursday October 17, 2013 @02:52AM (#6563996)
You know if this kind of thing keeps happening on the internet I might just have to start counting on my toes.
Re:C++ has become a scripting language by GRMrGecko on Thursday October 17, 2013 @11:43AM (#6566106)
I like perl because of how it looks similar to php, and because it looks similar to php, the structure is somewhat like C. I am not sure how good Python is at text manipulation, but I'll bet it is similar to perl and can do, if not all the things perl can, most things perl can.

Languages I know are: C, Objective-C, Perl, PHP, Javascript, and bash. Languages I played in: Python, Ruby, C++, C#, lua, go.

I want to learn C++ as it has very good cross platform stuff. But don't know when to start. I want to learn python so I can help with mailpile, but don't know when to start. I want to learn go as I want to write my own chat protocol, but don't know when to start.

I am not biased with languages, I just don't know other languages and can't give a pros/cons comparing between them.

Re:C++ has become a scripting language by com2kid on Wednesday October 16, 2013 @06:12PM (#6562430)
> When objects (either implicit or explicit) start to interact with each other, shell scripting falls short quickly.

Not with PowerShell! PowerShell is amazing in how it takes the text pipe model of working and extends it to general purpose objects.

(Having access to all those .NET libraries is a nice bonus!)

Re:C++ has become a scripting language by tyrion on Wednesday October 16, 2013 @11:45AM (#6560282)
I may be wrong but I think that this is actually spawning a subprocess and thus is not an exact translation of the python example.

This bash script rewritten in python should be something like this:

  import subprocess, sys
  
  inp = open(sys.argv[1], 'r')
  out = open(sys.argv[2], 'w')
  
  sys.exit(subprocess.call('sort', stdin=inp, stdout=out, stderr=sys.stderr))

(I don't know why but it seems that the performance of this version are worse than the original proposed example with writelines/readlines)
Re:C++ has become a scripting language by reirob on Wednesday October 16, 2013 @03:39PM (#6561679)
So what? The Unix Shell library consists of all the executables that are on the system - for me this is the beauty. There is not much of abstraction between the Unix Shell and the system under it. And shell is very tolerant to other programing languages as well - as long as they allow to represent data in text form and through streams or files ;)
Re:C++ has become a scripting language by tyrion on Wednesday October 16, 2013 @05:19PM (#6562216)
Yes, I know that this is the correct way of doing it in bash. I posted this because someone might test the speed of the two scripts and conclude "bash is faster" while they actually measured the speed of "sort" probably.
Re:C++ has become a scripting language by judk on Thursday October 17, 2013 @12:12AM (#6563614)
Microbenchmarks are a distraction. Macrobenchmarks matter.

The real problem with bash is the mess you get when you start needing whitespace or arrays or error handling or non tabular objects or a computation not already implemented as a system program.

Re:C++ has become a scripting language by greyfade on Wednesday October 16, 2013 @12:25PM (#6560531)
It doesn't have to spawn a subprocess. Some shells (like those in Busybox and Toybox) reduce common POSIX userland tools to a function call.
Re:C++ has become a scripting language by robotresearcher on Wednesday October 16, 2013 @12:39PM (#6560618)
Right. And even it was implemented as a subprocess, it's still idiomatic shell code, i.e. what you would Actually Do.
Re:C++ has become a scripting language by dbaupp on Wednesday October 16, 2013 @08:32AM (#6559245)
You can even just write

  lines = sorted(open(sys.argv[1]))
since Python files act as iterators over lines.
Re:C++ has become a scripting language by MereInterest on Wednesday October 16, 2013 @08:29AM (#6559227)
Well, if you want a REPL for C++, there is always ROOT. Though, for the love of all that is holy in this world, I don't know why they chose to have a REPL for C++. Or why they chose the profoundly ungoogleable name of "ROOT".
Re:C++ has become a scripting language by natejenkins on Wednesday October 16, 2013 @08:34AM (#6559258)
If you instead search for 'root CERN' the first result is the following:

http://root.cern.ch/drupal/

Re:C++ has become a scripting language by pjmlp on Wednesday October 16, 2013 @04:23PM (#6561943)
It is actually called CINT.

http://root.cern.ch/drupal/content/cint

Re:C++ has become a scripting language by informatimago on Wednesday October 16, 2013 @08:31AM (#6559236)
Or one liners:

import sys; open(sys.argv[2], 'w').writelines(sorted(open(sys.argv[2], 'w').writelines())

(use-package :com.informatimago.common-lisp.cesarum.file) (setf (string-list-text-file-contents outpath) (sort (string-list-text-file-contents inpath) (function string<)))

Indeed, what kills languages like C or C++11 in the scripting domain, is the need for declarations and other kind of boiler plate. That said, with C++11 there are means to write a library requiring less declarations, but it's still a cultural problem (beside the hard work that it would require).

Re:C++ has become a scripting language by aidos on Wednesday October 16, 2013 @08:39AM (#6559292)
Your first example is erroneous - perhaps a good example of why you shouldn't try to squeeze everything into a single line of code :)

    import sys
    src = open(sys.argv[1])
    dest = open(sys.argv[2], 'w')
    dest.writelines(sorted(src))
Re:C++ has become a scripting language by cldr on Thursday October 17, 2013 @01:24AM (#6563831)
It's not really boilerplate in the classical sense, he's just talking about headers and lines that have only a brace on them. I think it's a fair statement.

Plus, he could have reduced lines even further to get down to about 6, like

   vector<string> data;
   string line;
   for (ifstream ifile(argv[1]); getline(ifile, line);)
       data.push_back(line);
   sort(begin(data), end(data));
   copy(begin(data), end(data), ostream_iterator<string>(ofstream(argv[2]), "\n"));
And his `return 0` was superfluous so I removed it.

Unfortunately we work with iterator pairs in C++; if we had ranges like D does, we could turn those last two into

   copy(sort(data), ...)
but alas we cannot.
Re:C++ has become a scripting language by alpatters on Thursday October 17, 2013 @06:10AM (#6564421)
You could use boost::range to make it even more succinct
Re:C++ has become a scripting language by 6ren on Wednesday October 16, 2013 @08:32AM (#6559243)
Once you remove the redundancy, you'll see it is pure information!