Live data from Hacker News

Convert any Python file into a single line of code

github.com

21–27 of 27 posts

Re: Convert any Python file into a single line of code

#22
post #10

> Space: O(n). No code is ever duplicated, so the one-lined code produced is linear in the size of the input code. Seems like a non sequitur?

I'm not sure why you think that. Do you mean that normally, in this context "space" means the memory usage of a program, which isn't particularly related to the size of the program? 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.

Oh, you're right, I'm using "space" incorrectly. Fixed :)

Re: Convert any Python file into a single line of code

#23
I in the distant past when I was still in Uni attempted to do something similar, though I never finished it. I think I should take up my project again and try to finish it this time. I got stuck last time trying to figure out how to do a try/catch.

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'] 
(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 :
    
can get compiled into something like

  list( for _ in itertools.takewhile(lambda _: , 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 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 to terminate the loop (on a break) and reset the continue flag.

Re: Convert any Python file into a single line of code

#24
post #10

> Space: O(n). No code is ever duplicated, so the one-lined code produced is linear in the size of the input code. Seems like a non sequitur?

I'm not sure why you think that. Do you mean that normally, in this context "space" means the memory usage of a program, which isn't particularly related to the size of the program? 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.

Not really, what I meant was that it's pretty easy to go over O(n) without ever duplicating any code. Like just imagine if you had a string literal that held all the numbers from 1 to n^2.

Re: Convert any Python file into a single line of code

#26
First try: Your code could not be one-lined. Open problem: try-except

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' :)

Re: Convert any Python file into a single line of code

#27

Earlier quoted context omitted.

haha, I think the resulting one liners are a little over 80 characters :)

Eh, you can insert newlines wherever you want and it'll still work, plus the "80 char line" rule is at the top of the list of PEP8 rules you can break.

True, but then it's not on one line anymore, and the code likely ends up on more lines. If only there was some other version of the code that took up less lines and characters... :)
Post reply on HN