<commit_before>One-liner
=========

Convert any Python file into a single line of code which has the same functionality.

No newlines allowed.

No semicolons allowed, either.

Installation and Usage
----------------------

    git clone https://github.com/csvoss/oneliner
    python oneliner/main.py name_of_target_file.py

Examples
--------

**Before:**

    x = 3
    y = 4
    print (x < y < 5)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [[__print(d.x<d.y<5) for d.y in [(4)]][0] for d.x in [(3)]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

That line looks complicated, because we need some tricks to import the print function and to support certain tricks which are needed for more complicated features such as `while` and `if`. For a program as simple as this one, though, you can think of it as working this way:

     (lambda x: (lambda y: print(x<y<5))(4))(3)

**Before:**

    def f(x):
        return x+5
    print f(13)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [__print(d.f(13)) for d.f in [(lambda x:[(d.x+5) for d.x in [(x)]][0])]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

...or, if you want to think about something more simplified:

    (lambda f: print(f(13)))(lambda x: x+5)

**Before:**

    def guess_my_number(n):
        while True:
            user_input = raw_input("Enter a positive integer to guess: ")
            if len(user_input)==0 or not user_input.isdigit():
                print "Not a positive integer!"
            else:
                user_input = int(user_input)
                if user_input > n:
                    print "Too big! Try again!"
                elif user_input < n:
                    print "Too small! Try again!"
                else:
                    print "You win!"
                    return True
    guess_my_number(42)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [(lambda ___: None)(d.guess_my_number(42)) for d.guess_my_number in [(lambda n:[(__y(lambda __this: (lambda d: (lambda __after: [(lambda __after: (lambda ___: __after(d))(__print('Not a positive integer!')) if (d.len(d.user_input)==0 or (not d.user_input.isdigit())) else [(lambda __after: (lambda ___: __after(d))(__print('Too big! Try again!')) if d.user_input>d.n else (lambda __after: (lambda ___: __after(d))(__print('Too small! Try again!')) if d.user_input<d.n else (lambda ___: d.True)(__print('You win!')))(lambda d: __after(d)))(lambda d: __after(d)) for d.user_input in [(d.int(d.user_input))]][0])(lambda d: __this(d)) for d.user_input in [(d.raw_input('Enter a positive integer to guess: '))]][0] if d.True else __after(d))(lambda d: None))))(d) for d.n in [(n)]][0])]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

FAQ
---

### Oh dear God why?

Yeah. I'm sorry. But on the other hand, why not?

### Can't you use semicolons?

That would be against the spirit of this exercise. Why pass up a perfectly good excuse to abuse [lambda functions](https://docs.python.org/2/reference/expressions.html#lambda), [ternary expressions](https://docs.python.org/2/reference/expressions.html#conditional-expressions), [list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions), and even the occasional [Y combinator](http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator)? Never pass up an opportunity to use the Y combinator.

Analysis
--------
### Space

*O(n)*. No code is ever duplicated, so the one-lined code produced is linear in the size of the input code.

### Time

I'm guessing it's slower, but no more than linearly so; I have yet to analyze this.

Since while loops and for loops are implemented with recursion, you might encounter unfortunate maximum-recursion-depth-exceeded errors.

Open Problems
-------------
* try/except/finally
* raise/assert
* classes and OOP
* global variables
* del
* exec
* with
* yield
* from foo import bar<commit_msg>update README with tips<commit_after>One-liner
=========

Convert any Python file into a single line of code which has the same functionality.

No newlines allowed.

No semicolons allowed, either.

Installation and Usage
----------------------

    git clone https://github.com/csvoss/oneliner
    cd oneliner
    python main.py name_of_target_file.py

Examples
--------

**Before:**

    x = 3
    y = 4
    print (x < y < 5)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [[__print(d.x<d.y<5) for d.y in [(4)]][0] for d.x in [(3)]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

That line looks complicated, because we need some tricks to import the print function and to support certain tricks which are needed for more complicated features such as `while` and `if`. For a program as simple as this one, though, you can think of it as working this way:

     (lambda x: (lambda y: print(x<y<5))(4))(3)

**Before:**

    def f(x):
        return x+5
    print f(13)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [__print(d.f(13)) for d.f in [(lambda x:[(d.x+5) for d.x in [(x)]][0])]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

...or, if you want to think about something more simplified:

    (lambda f: print(f(13)))(lambda x: x+5)

**Before:**

    def guess_my_number(n):
        while True:
            user_input = raw_input("Enter a positive integer to guess: ")
            if len(user_input)==0 or not user_input.isdigit():
                print "Not a positive integer!"
            else:
                user_input = int(user_input)
                if user_input > n:
                    print "Too big! Try again!"
                elif user_input < n:
                    print "Too small! Try again!"
                else:
                    print "You win!"
                    return True
    guess_my_number(42)

**After:**

    (lambda __builtin__: (lambda __print, __y, d: [(lambda ___: None)(d.guess_my_number(42)) for d.guess_my_number in [(lambda n:[(__y(lambda __this: (lambda d: (lambda __after: [(lambda __after: (lambda ___: __after(d))(__print('Not a positive integer!')) if (d.len(d.user_input)==0 or (not d.user_input.isdigit())) else [(lambda __after: (lambda ___: __after(d))(__print('Too big! Try again!')) if d.user_input>d.n else (lambda __after: (lambda ___: __after(d))(__print('Too small! Try again!')) if d.user_input<d.n else (lambda ___: d.True)(__print('You win!')))(lambda d: __after(d)))(lambda d: __after(d)) for d.user_input in [(d.int(d.user_input))]][0])(lambda d: __this(d)) for d.user_input in [(d.raw_input('Enter a positive integer to guess: '))]][0] if d.True else __after(d))(lambda d: None))))(d) for d.n in [(n)]][0])]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))

FAQ
---

### Oh dear God why?

Yeah. I'm sorry. But on the other hand, why not?

### Can't you use semicolons?

That would be against the spirit of this exercise. Why pass up a perfectly good excuse to abuse [lambda functions](https://docs.python.org/2/reference/expressions.html#lambda), [ternary expressions](https://docs.python.org/2/reference/expressions.html#conditional-expressions), [list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions), and even the occasional [Y combinator](http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator)? Never pass up an opportunity to use the Y combinator.

Analysis
--------
### Space

*O(n)*. No code is ever duplicated, so the one-lined code produced is linear in the size of the input code.

### Time

I'm guessing it's slower, but no more than linearly so; I have yet to analyze this.

### Tips

The one-lined code tends to contain many nested lambdas; if there are too many, Python will refuse to run it.

    $ python main.ol.py
    s_push: parser stack overflow
    MemoryError

This can be fixed using pypy.

    $ pypy main.ol.py

However, since while loops and for loops are implemented with recursion, you might encounter `maximum recursion depth exceeded` errors during runtime if your loops go on for too long. I do not yet know of a fix for this.

Open Problems
-------------
* try/except/finally
* raise/assert
* classes and OOP
* global variables
* del
* exec
* with
* yield
* from foo import bar
{'fraction_non_alphanumeric': 0.1574601048990068, 'fraction_numerical': 0.007811628166499275, 'mean_word_length': 3.6077120822622106, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '158178', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2845, 'n_words': 941}