Hacker News new | past | comments | ask | show | jobs | submit login
One-lined Python (onelinerizer.com)
75 points by Symmetry 56 days ago | hide | past | web | favorite | 16 comments



The demo at the top doesn't work because it's expecting python 3 but the demo code is python 2.


Even if fixed to python 3, still throws an error:

> Your code could not be one-lined. name 'reduce' is not defined


Python3 put it in functools, I think. Ah the need for an import would prevent one lining.

Never quite understood that decision


Reduce isn't used much. Things that tend to use reduce in languages like Haskell tend to be built from other primitives in Python.

Instead of reduce, you could use https://docs.python.org/3/library/functions.html#__import__

    __import__('functools').reduce


The article goes on to use `__import__()` to one-line imports.


I like this. Here is a one-liner I wrote recently that calculates the 10th Fibonacci number:

    (lambda n: n(lambda x: x+1)(0))(((lambda f: (lambda x: x(x))(lambda y: f(lambda z: y(y)(z))))(lambda f: (lambda f: lambda n: (lambda f: lambda x: x) if (lambda b: b(True)(False))((lambda n: n(lambda x: (lambda x: lambda y: y))(lambda x: lambda y: x))(n)) else f(n))(lambda n:(lambda m: lambda n: (lambda m: lambda n: (lambda n: n(lambda x: (lambda x: lambda y: y))(lambda x: lambda y: x))((lambda m: lambda n: n((lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda u: u)))(m))(m)(n)))((lambda n: lambda f: lambda x: f(n(f)(x)))(m))(n))(n)((lambda n: lambda f: lambda x: f(n(f)(x)))(lambda f: lambda x: f(x)))(n)((lambda m: lambda n: lambda f: lambda x: m(f)(n(f)(x)))(f((lambda m: lambda n: n((lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda u: u)))(m))(n)(lambda f: lambda x: f(x))))(f((lambda m: lambda n: n((lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda u: u)))(m))(n)((lambda n: lambda f: lambda x: f(n(f)(x)))(lambda f: lambda x: f(x)))))))))(lambda f: lambda x: f(f(f(f(f(f(f(f(f(f(x))))))))))))
Paste that into a Python 3 interpreter, you should see "55". Somewhere in there is the Y-contaminator and several other tricks similar to what this article describes.


If you like that sort of thing, try doing similar code for the Ackermann function [1]. In a certain sense that can be made semi-formal, if you can do this sort of thing for the Ackermann function, then you can do it for almost anything.

https://en.wikipedia.org/wiki/Ackermann_function


If you want your mind to blow up I recommend watching this year's "Lambda Calculus from the Ground Up" PyCon talk by the wizard David Beazley https://www.youtube.com/watch?v=pkCLMl0e_0k


Followed by watching the rest of his videos! Each one is gold.


I want this to work so badly. Sadly, I think the Python on the website is committing seppaku rather than being forced to abuse its brethren.


I believe is Seppuku (https://en.wikipedia.org/wiki/Seppuku)


I believe it is 切腹, but ok.


"Seppuku" is the accepted romanization of 切腹; why does it seem incorrect to you?


Neat, I just learned what the Y combinator actually is!


No newlines allowed. No semicolons, either.

In my head I read this in the voice of Harry Dean Stanton (Repo Man).


great for submitting complex one liners via docker exec




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: