Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login
One-lined Python (www.onelinerizer.com) similar stories update story
75.0 points by Symmetry | karma 18650 | avg karma 3.07 2019-08-21 14:08:01+00:00 | hide | past | favorite | 17 comments



view as:

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.

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

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 it is ??, but ok.

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

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.

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

Legal | privacy