Oneliners



  • Generally, it's fun to figure out how to cram as much stuff as possible into as few lines of code as possible. Sometines, however, you can take it to the extreme.

    This thread is dedicated to the purpouse of creating one-line programs (or scripts) that do something interesting. Obviously, it isn't allowed to string lines together by just using ';' (or whatever ends lines in your language of choice).

    First, let me give you my little creation. This is a php page that will count how many times it was visited (note: since it's a pretty long line, it might wrap, but it's still one line):

    <? echo ($counter = 0) * ord($oldfile = file_get_contents(FILE))/ord($oldfile) * ($bytes = fwrite(fopen(FILE, "w"), str_replace($counter, $counter+true, $oldfile)))/$bytes; ?>



  • Do sql "programs" count?



  • arrShapeId.push(htmlAreas[i].match(/id=["|'][^"|']*(?=["|'])/)[0].replace('id='',''));

    This is a lie, actually. I defined the regex in a separate var first.



  •     for (int a = ~a == !3; a %=! 007, + ++a, 42 != !(0xa
    || ~!1, &--a)[7 ^ ~!a, (666 | ~777 ^ +a--) ? ~a-- && 0, 0
    <<! a: a += ~~a, +a] - a % !a, +-!a-- ^ a; (9 | 2, a) ? (a
    >>= (+-+!+~-a, -++a) ? -5: !!!a * ~a): *&a, 8 & !a) -a,
    +a, !&a;



    Nice isnt it? 😉



  • I've written quite a bit of Lisp, all of which could be put on a single line.



  • Show it.

    I'd like to see some Lisp.

    for (int a = ~a == !3; a %=! 007, + ++a, 42 != !(0xa
    || ~!1, &--a)[7 ^ ~!a, (666 | ~777 ^ +a--) ? ~a-- && 0, 0
    <<! a: a += ~~a, +a] - a % !a, +-!a-- ^ a; (9 | 2, a) ? (a
    >>= (+-+!+~-a, -++a) ? -5: !!!a * ~a): *&a, 8 & !a) -a,
    +a, !&a;


    Madness!

    !3 == a < 3 < a?


    I suspect it's a file access determine thingy, but the 42 makes me doubt it.



  • Any old Applesoft BASIC hackers around these parts?  Applesoft had a strict limit on the length of a single line of code.  That made the creation one liners a real challange, and the magazine Call Apple used to publish new ones each month.  I have an old 5.25 disk laying around somewhere that's full of them.  Many are quite amazing.



  • @dhromed said:

    Show it.I'd like to see some Lisp.

    As requested. Here's a function for use with TBNL[0] that checks a URL query parameter against a list of values or an arbitrary test function and returns the parameter value if it's valid and a default otherwise:

    On one line:

    (defun valid-parameter (parameter-name &key good-values test (default 'nil)) (cond ((boundp 'good-values) (or (find (parameter parameter-name) good-values) default)) (test (or (funcall test (parameter parameter-name)) default)) ('t default)))

    Split in to lines and indented for clarity:
    (defun valid-parameter (parameter-name &key good-values test (default 'nil))
    (cond ((boundp 'good-values)
    (or (find (parameter parameter-name)
    good-values)
    default))
    (test
    (or (funcall test
    (parameter parameter-name))
    default))
    ('t default)))

    Any formatting issues are the responsibility of telligentsystems.

    [0] http://weitz.de/tbnl



  • Are perl programmers allowed to join in on the fun?
    <code>
    perl -MIO::Socket::INET -e 'if($s=IO::Socket::INET->new(LocalPort=>1800, Proto=>"tcp", ReuseAddr=>1,Listen=>5) ||die $!) {do {$|++ && print($_) while <$in>} while(($in=undef) || ($in=$s->accept()))}'
    <code>
    Basic TCP server in perl.  From there, you can make it do port forwarding, remote commands [think ssh], netcat or a even a N-way chat client [if I recall correctly, that required a semicolon because of something dealing with the select system call].  That basic one is unix talk... you'll need to adjust the quotes to get it to run on NT.

    I'm looking on my hard drive for the web server one-liner that used just socket and fork.  These aren't really impressive, because I'm using the default installed modules.  I mean, I could write an XML-RPC one-liner that loaded functions at runtime without any additional modules.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.