Copying stdin to stdout in Java
51–60 of 61 posts
Re: Copying stdin to stdout in Java
#52Earlier quoted context omitted.
"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671
Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .
$ printf 'binarydata'|awk '{ print }'|hexdump -C
00000000 62 69 6e 61 72 79 64 61 74 61 0a |binarydata.|
0000000bRe: Copying stdin to stdout in Java
#53Earlier quoted context omitted.
"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671
Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .
Re: Copying stdin to stdout in Java
#54The essence of the argument here is that Java is a poor language as it doesn't offer simple abstraction to copy standard input into standard output; that Java is too verbose and low-level for the task. The lack of direct abstraction is not a valid argument, because as a programmer, you shouldn't be writing the logic in Java, Python, C or Scala but rather a higher order domain language implemented in the chosen host l…
First, constantly having to write implementations like this introduces a ton of friction as compared with reusing existing implementations. Languages do have an influence on that.
Second, languages vary in "expressiveness," determining how much code you have to write in order to make an implementation like this. In one language, copying stdin to stdout fits into a natural idiom which also handles other cases naturally. In another language with less care for ergonomics, every task might be equally un-idiomatic.
In other words, a language can offer its own "higher order domain languages" for the core tasks that everyone is doing over and over again as part of their general programming. Or it can choose not to do that, because what it offers is already Turing complete. But then the ergonomics are bad, and it makes a real difference.
It seems wrong that when I am paying for things like long JIT warmups and stop-the-world GC, I am still writing piles of functions with low-level idioms that are no more expressive than C's.
If Java ships with a 'copy from one stream to another' primitive then I'd find that a much more compelling argument than that I can treat myself to reimplementing things like stream copying, sorting and basic data structures on a regular basis. It's unbelievably tedious and wasteful to do this, there's just no reason.
Re: Copying stdin to stdout in Java
#55There are some comments on the Gist claiming Python or Ruby is a one liner. My response: https://gist.github.com/kosta/9777932/#comment-1199293
Re: Copying stdin to stdout in Java
#56Python, shell, etc. isn't suitable for high scalability, fast applications (e.g. Apache Solr, Lucene, Hadoop etc.). And beginner examples like these make no sense. Java clearly has an important role in this world, maybe not in yours (if you're so irked by "verbosity").
Re: Copying stdin to stdout in Java
#57There are some comments on the Gist claiming Python or Ruby is a one liner. My response: https://gist.github.com/kosta/9777932/#comment-1199293
Your Python code is just wrong. I'm not sure what you think it's proving when you write explicitly wrong Python code, and then show that the output is wrong.
Could you elaborate? I want to understand how to fix it.
EDIT: I really want to know what is wrong with my code. It works. Is it a stylistic concern?
$ dd bs=1m count=10 if=/dev/random of=randomdata
10+0 records in
10+0 records out
10485760 bytes transferred in 0.878533 secs (11935535 bytes/sec)
$ python stdin_to_stdout.py
EDIT2: I think I know how you came to the conclusion my code is wrong. Perhaps next time you should read the __entire__ post. Thank you.Re: Copying stdin to stdout in Java
#58The essence of the argument here is that Java is a poor language as it doesn't offer simple abstraction to copy standard input into standard output; that Java is too verbose and low-level for the task. The lack of direct abstraction is not a valid argument, because as a programmer, you shouldn't be writing the logic in Java, Python, C or Scala but rather a higher order domain language implemented in the chosen host l…
Two things. First, constantly having to write implementations like this introduces a ton of friction as compared with reusing existing implementations. Languages do have an influence on that. Second, languages vary in "expressiveness," determining how much code you have to write in order to make an implementation like this. In one language, copying stdin to stdout fits into a natural idiom which also handles other ca…
My point was that limited expression means of a language are sometimes compounded by inability of a programmer to make a good use of the expression means already available to them.
I also understand the desire for more a expressive language, I am a programmer after all. One has to keep in mind, however, that the more expressive a language is the harder it is on the reader. Java code is trivial for a reader to follow (if not for the excessive verbosity sometimes covering up the true intent); much of Scala code base, on the other hand, is not that trivial to comprehend due to the high expressiveness of the language.
Re: Copying stdin to stdout in Java
#59If you want a single point generalization about Java programmers, I was one for years, and the way I would have implemented this involves minimally a BufferedReader and an InputStreamReader. You'll also probably need to catch the IOException. Plus or minus 20 lines or so. I don't hate Java, but "Java as it is spoken" does not often get used for Unix-style piping in a chain of small programs on a command line. It also…
Re: Copying stdin to stdout in Java
#60Earlier quoted context omitted.
It's not much shorter, but it was pretty easy to write it you know the libraries. package main import ("os"; "io") func main() { io.Copy(os.Stdout, os.Stdin) }
The order of os.Stdout, os.Stdin is interesting. I'd assume src->dest being more intuitive order. Any ideas if this dest By the way, Pipe in the same package is the other way around: ( PipeReader, PipeWriter)
dest = src // Variable assignment.
So the reason is consistency. I suppose the Pipe exception is to map Unix's pipes.