DEV Community

loading...

Discussion on: Are one liners always pythonic?

Collapse
chrisgreening profile image
Chris Greening

Did someone say one-line Brainf*ck interpreter?

( lambda ins, sys: ( ( lambda f: f( f, 0, 0, data=[0]*100000, bp=( (lambda f: f(f, [], {}, 0)[0][1])( lambda g, bs, bp, ix: ( ((bs, bp),) if ix >= len(ins) else ( (g(g, bs + [ix], bp, ix+1), )[0] if ins[ix] == '[' else ( (g(g, bs[:-1], bp, ix+1), bp.update({ix: bs[-1], bs[-1]:ix}),)[0] if ins[ix] == ']' else g(g, bs, bp, ix+1) ) ) ) ) ) ) )( lambda g, ptr, i, data, bp: ( None if i >= len(ins) else g(g, *{ '>': lambda d, p, i, brace_pairs: (p + 1, i+1,), '<': lambda d, p, i, brace_pairs: (p - 1, i+1,), '+': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]+1)%256))[:-1], '-': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]-1)%256))[:-1], '.': lambda d, p, i, brace_pairs: (p, i+1, sys.stdout.write(chr(d[p])))[:-1], ',': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, ord(sys.stdin.buffer.read(1))))[:-1], '[': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if not d[p] else i+1, ), ']': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if d[p] else i+1, ), }[ins[i]](data, ptr, i, bp), data, bp) ) ) ) )( ''.join(i for i in open(__import__('sys').argv[1]).read() if i in {'<', '>', '+', '-', '.', ',', '[', ']'}), (__import__('sys'), __import__('sys').setrecursionlimit(100000))[0], )
Enter fullscreen mode Exit fullscreen mode
Collapse
miguelmj profile image
MiguelMJ Author

An insane language should have an insane interpreter!

Collapse
qviper profile image
Viper

Lambda within a lambda. You deserve a medal.

Collapse
chrisgreening profile image
Chris Greening

lol can't claim the credit but definitely a bunch of clever tricks in there, here's another one-line Brainf*ck interpreter

Thread Thread
qviper profile image
Viper

Thanks for sharing. But oneliner is not that much readable. (I think)

Thread Thread
miguelmj profile image
MiguelMJ Author

That's true, but I guess for some they are like a challenge. It happens the same with esoteric languages... We could say that Python becomes an esoteric language when you apply the rule "all in one line" hahaha

Thread Thread
chrisgreening profile image
Chris Greening

Pretty recently I was writing a program that was going to be automated on a server with limited space so I crunched the program from 100 lines to like 15 awful one liners and it cut the size of the file down to like less than a kilobyte lol it was a fun challenge to see how small I could get the program and I could see people doing it for fun/proof of concept