5 ms·
One-lined Python
- sp332 7y agoThe demo at the top doesn't work because it's expecting python 3 but the demo code is python 2.
- trymas 7y agoEven if fixed to python 3, still throws an error: > Your code could not be one-lined. name 'reduce' is not defined
- np_tedious 7y agoPython3 put it in functools, I think. Ah the need for an import would prevent one lining. Never quite understood that decision
- gvx 7y agoReduce 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__ https://docs.python.org/3/library/functions.html#__import__ __import__('functools').reduce
- jmmcd 7y agoThe article goes on to use `__import__()` to one-line imports.
- obenn 7y agoNeat, I just learned what the Y combinator actually is!
- virusduck 7y agoI 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.
- olooney 7y agoI 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.
- xamuel 7y agoIf 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 https://en.wikipedia.org/wiki/Ackermann_function
- sethgecko 7y agoIf 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 https://www.youtube.com/watch?v=pkCLMl0e_0k
- kiddico 7y agoFollowed by watching the rest of his videos! Each one is gold.
- pohl 7y agoNo newlines allowed. No semicolons, either. In my head I read this in the voice of Harry Dean Stanton (Repo Man).
- RocketSyntax 7y agogreat for submitting complex one liners via docker exec