6 ms·
You'll have to ask the author that. Now, I'd like to ask the author who cares how you implement 'yes' in Go ;-).
by the-smug-one 3y ago
You'll have to ask the author that. Now, I'd like to ask the author who cares how you implement 'yes' in Go ;-).
- 38 3y agoyes C lines of code: 112 yes Go lines of code: 9
- placesalt 3y ago16 lines of go, with comments & whitespace
- 38 3y ago134 lines of C, with comments & whitespace
- deleted 3y ago[deleted]
- not2b 3y agoThat isn't an apples-to-apples comparison. A C version could be similarly short, but the GNU yes implementer got carried away with efficiency considerations. If the Go program did the same, it would be longer.
- hunson_abadeer 3y agoIt feels like you're making a bad-faith argument here. You can implement 'yes' in a straightforward way in a couple lines of C, too. main(int argc, char** argv) { while (1) { if (argc > 1) for (int i = 1; i < argc; i++) printf("%s%c", argv[i], (i == argc - 1) ? '\n' : ' '); else puts("y"); } } The point other folks are making is that it's written differently for a reason. Maybe not a reason that's important, but at the very least, let's try to compare apples to apples.
- 38 3y agoI don't see include, which means you're ignoring warnings aren't printing them in the first place. Also testing against a number instead of boolean. Also you have a horrible hack instead of proper flag parsing. And you're also abusing brace elision just to reduce LOC. And again abusing ternary syntax for the same reason.
- deleted 3y ago[deleted]
- mturmon 3y agoIf you start with code golf, as you did, then this is where you end up. The only way to win is not to play?
- 38 3y agomine is just normal idiomatic Go code. thats not true of the C code.
- inferiorhuman 3y agoIt might be ugly, but that is idiomatic C code. C didn't even have boolean types until C99, and even then it's an "extension" of an integer type. You could argue about the loop itself, after all K&R specified "for(;;)", but the other commonly used (ergo idiomatic) infinite loops use precisely the same number of lines. "while(1)" is a perfectly idiomatic manner to create an infinite loop. Likewise a void return type for main was entirely legal until C99. The BSD yes(1) I've laying around only prints the first argument, so flag parsing? What flag parsing? Yes in nine lines of C inclusive of preprocessor macro invocations and white space. #include <stdio.h> int main(int argc, char **argv) { const char *phrase = argc > 1 ? argv[1] : "y"; while (1) { printf("%s\n", phrase); } return 0; }
- 38 3y agoI don't see proper flag parsing, I see an argv hack.
- staplung 3y agoOn my mac the go version of yes creates a binary that's 30x larger than the yes binary I have on a linux machine. 2MB vs ~65KB.
- svnpenn 3y agoon mine it is 1,354 KB. I prefer the 10x LOC savings over a megabyte of hard drive, but you do you.
- BenjiWiebe 3y agoBut if everything was written in Go it might be 1 megabyte * the number of users of coreutils. Might be worth using a few more lines of code to save a little bit of space on a lot of machines.
- commonlisp94 3y agoIt's not about hard drive space, it's about start up time. The 10x LOC (despite being a huge exaggeration) is also the fixed cost, not the marginal cost. You're only forging main loop and includes, not any fundamental complexity. This is also a funny argument coming from a Go programmer considering that Go trades off conciseness and expressivity for simplicity. Show me some of your favorite Go and I'm sure we can replace it with some concise C++.
- 38 3y ago> C++ I just physically shuddered
- commonlisp94 3y agoI bet you did.
- smashed 3y agoThis. In fact, most embedded Linux thingy will be running the busybox version of core utilities instead of gnu coreutils for binary size reasons. I other words, even gnu coreutils is too big.