Convert any Python file into a single line of code
github.com
github.com
I don't write Perl much anymore, but that was far and away my __favorite__ way to use it. I love coding in Python now, more than I ever liked programming in any language (except perhaps lisp), but I still don't feel I can use it on the command line that way.
In the context of a Python program, I can't see the value of a Lovecraftian lambda (unless maybe in code golf comments?), but I found this article very appealing mainly because it's quite a feat. Doing anything nontrivial in a lambda is a pain in the ass, so the fact that someone wrote a tool to do it is awesome.
Still brings me back to highschool, where we toyed with that kind of tricks (someone builds something, the others try to figure out how it works). Fun times!
The one big thing I notice this is doing "wrong", is that it is not linear in recursion depth: Each statement increases the stack depth by 1. The way around this is to instead sequence the commands using arrays, since python guarantees the execution order is left to right. So his example could have been rewritten as:
(lambda s: [s.__setitem__('x', 3), s.__setitem__('y', 4), print(s['x'] < s['y'] < 5)])({})
(though for this example you could just modify the result of globals())Then you can also do trickery to get for and while loops into generator expressions as opposed to lambdas. The hard part is handling continues and breaks. For instance:
while <cond>:
<stmts>
can get compiled into something like list(<compiled_stmts> for _ in itertools.takewhile(lambda _: <compiled_cond>, itertools.cycle([1])))
(though you'll want to replace list with a function that evaluates the iterator but doesn't take up O(N) space where N is the number of iterations, something like a no-op accumulator)To handle breaks and continues, you'd have to make your <compiled_stmts> be aware that a break/continue was called (a flag on some meta table was my plan), so they can terminate early. You'd also have to change <compiled_cond> to terminate the loop (on a break) and reset the continue flag.
This made me think of this video with Jim Weirich demonstrating functional patterns, transformations and the Y combinator:
Highly recommened. It's very related to what seems to be going on here, and actually quite funny.
Well that was disappointing...
Second try: Your code could not be one-lined. Open problem: yield
Third try: Your code could not be one-lined. Open problem: raise
Fourth try: Your code could not be one-lined. Not yet implemented: classdef
So, any file without class definitions, try/except/raise/yield (and probably some more). Not my definition of 'any' :)
Okay, this was enough to scare me away from it :)
^^ Legendary
Seems like a non sequitur?
In which case, yeah, it's a little incongruous. The "space" bit is talking about the textual representation of the program produced, while the "time" bit is talking about the runtime characteristics of the program.