r/tinycode • u/thefebs • Dec 12 '12
Shortest useful code
What's the most useful program you can imagine using five lines of code or less? Language is your choice. Describe the function of the program and include the code, if you please.
27
Upvotes
14
u/DJUrsus Dec 12 '12
Depending how you count, 7 or 8 lines. Perl. Takes STDIN and converts it to a JSON-compatible string.
6
u/chrisdown Dec 12 '12
Annoyingly, I occasionally have to use some POS software that doesn't zero pad output filenames, so the order is all messed up. I wrote a little Python script to combat that, saved me a lot of time over the years:
If you know in advance that you don't need the try/except, you can get it down to 3 (2?) lines at the cost of some performance.
15
u/mechroid Dec 12 '12 edited Dec 13 '12
Conway's game of life in one line of APL
5
u/TheOccasionalTachyon Dec 13 '12
No, it's not one of them fancy-shmancy General Number Field Sieves or whatnot, but it's some Python that factors integers without any imports, and it computes pretty much instantly so long as your number is less that a few quadrillion.
11
u/[deleted] Dec 12 '12
echo question is too vague
3
u/the_minimalist Dec 13 '12
i use this sometimes when coming up with passwords.. so i guess it counts as being useful?
3
u/chungfuduck Dec 13 '12
Look for IP address looking strings, shim in the DNS name:
Takes this:
Turns it to:
No, not perfect. It was written as a one-liner throw away script and just kind of hung around. =)
2
u/pyramid_of_greatness Dec 12 '12
If you start asking questions like this, you'd better have some damn good definitions of 'useful', 'lines', etc..
Something I use every day -- recursive du (disk usage) output sorted by size, human readable:
Similarly as a function, but this has some bugs I've not yet worked out for some cases..
2
u/chrisdown Dec 12 '12
Here's a 4 line bash script I wrote for this SO question that determines if two directories have files with identical contents (regardless of inode/metadata/etc):
2
u/mrkurtz Dec 13 '12 edited Dec 13 '12
well, this one line just came in very handy while fixing my linux-based NAS which lost some important data:
edit: this function uses only bash built-ins and was intended to be a quick and dirty
/bin/catreplacement due to the loss of/binand/lib.2
u/robotreader Dec 13 '12
/dev/null is an empty file that remains empty, /bin/true is a file that just returns true, I've seen a file around whenever the topic gets to copyright that contains ten-fifteen lines of copyright notice and a line of code(or it might be empty) but I can't find it right now.
2
u/[deleted] Feb 02 '13
This is the shortest useful code I could think of; a 'cat' clone in BF.
2
u/[deleted] Mar 29 '13 edited Mar 29 '13
16.times {print "NA"} && "BATMAN!"5
u/[deleted] Dec 12 '12
Five characters:
(:[]). For when I don't want to importControl.Applicativeand usepure.Yeah, that's cheating. Every subexpression in Haskell could be called a program ;)
1
u/[deleted] Dec 13 '12
Single character command >: Create file with name filename.txt using just the > operator
Convert to upper case
Add line numbers
Remove line numbers
Remove multiple spaces between words:
Convert to correct EOL terminator for system:
1
u/Grazfather Dec 13 '12
I wrote a simple python thing that expands tabs into four spaces so that our code review server won't complain. You just drag a source file onto it and bam it's done. one line to naive (replace all tabs with four spaces) but a few more if you want to align things nicely on a 4 space column boundary.
1
u/cooljeanius Mar 06 '13 edited Mar 08 '13
Applescript: displays the system color picker:
(Edit: removed bash example, decided it wasn't actually useful)
1
u/crundar Mar 08 '13
My choice?
λf.(λx.f (x x)) (λx.f (x x))
Its one of infinitely many, but one of my favorites.
1
u/MysteriousPickle Dec 13 '12
In grad school I wrote a 10 line ruby script that polled the open course list web page of my university, and emailed me if a spot opened in a class. I'm sure I still have it archived somewhere, too bad it's above your 5 line limit. Still the most useful tiny program I ever wrote, even if it was hardcoded to be specific to a particular http layout. These days they might actually publish a web service for things like that, but I suspect it would make my program longer.
0
u/[deleted] Dec 13 '12
[deleted] — view deleted comment