Stupid ways to spell simple code
Marcin Szamotulski
mszamot at gmail.com
Mon Jul 1 15:36:29 EDT 2013
On 17:30 Mon 01 Jul , Joshua Landau wrote:
> On 1 July 2013 14:14, Chris Angelico <rosuav at gmail.com> wrote:
> > On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti <neilc at norwich.edu> wrote:
> >> On 2013-06-30, Chris Angelico <rosuav at gmail.com> wrote:
> >>> So, here's a challenge: Come up with something really simple,
> >>> and write an insanely complicated - yet perfectly valid - way
> >>> to achieve the same thing. Bonus points for horribly abusing
> >>> Python's clean syntax in the process.
> >>>
> >>> Go on, do your worst!
> >>
> >> I've often thought it was redundant for Python to support 'if'
> >> when it has dictionaries, cf the rationale for having no
> >> 'switch'.
> >>
> >> valid_name = None
> >> while not valid_name:
> >> name = input("Enter your name: ")
> >> valid_name = {
> >> True: lambda: print("No name longer than 20 letters."),
> >> False: lambda: True,
> >> }[len(name) > 20]()
> >>
> >> Much better.
> >
> > Good! Good! But, waaaah. Waaaah.
> >
> > def get_name():
> > while True:
> > name = input("Enter your name: ")
> > yield {
> > True: lambda: print("No name longer than 20 letters."),
> > False: lambda: name,
> > }[len(name) > 20]()
> > name = next(filter(None,get_name()))
>
> Oh, cruel. But you can do worse. Who needs "while" when you have
> filter(iter(FUNCTION, object()))?
>
> def get_name():
> name = input("Enter your name: ")
> return [
> lambda: name,
> lambda: print("No name longer than 20 letters."),
> ][len(name) > 20]()
>
> name = next(filter(None, iter(get_name, object())))
>
>
> But who needs *any* of this! Defining functions is so old-hat. It's
> all already in the standard library (using only assignments and function-calls):
>
> from functools import partial
> from operator import getitem, ge, methodcaller
> from itertools import compress, tee
> apply = methodcaller("__call__")
> ret_true = partial(getitem, [True], 0)
> print_invalid = partial(print, "No name longer than 20 letters.")
> inputs = iter(partial(input, "Enter your name: "), ...)
> inputs, valid = tee(inputs)
> valid = map(len, valid)
> valid = map(partial(ge, 20), valid)
> side_effect_valid = map(partial(getitem, [print_invalid, ret_true]), valid)
> side_effect_valid = map(apply, side_effect_valid)
> valid_inputs = compress(inputs, side_effect_valid)
> name = next(valid_inputs)
>
>
> Which can be "neatly" expressed as two statements (I'm struggling to
> got it to one without those evil lambdas):
>
> from functools import partial
> from operator import getitem, ge, methodcaller
> from itertools import compress, tee
>
> inputs, valid = tee(iter(partial(input, "Enter your name: "), ...))
>
> name = next(
> compress(
> inputs,
> map(
> methodcaller("__call__"),
> map(
> partial(
> getitem,
> [
> partial(print, "No name longer than 20 letters."),
> partial(getitem, [True], 0)
> ]
> ),
> map(
> partial(ge, 20),
> map(len, valid)
> )
> )
> )
> )
> )
>
>
> Beautiful, see?
>
>
> Of course, the most powerful function deals with this much more quickly:
>
> exec("""
> while True:
> name = input("Enter your name: ")
>
> if len(name) <= 20:
> break
>
> else:
> print("No name longer than 20 letters.")
> """)
> --
> http://mail.python.org/mailman/listinfo/python-list
Here is another example which I came across when playing with
generators, the first function is actually quite useful, the second
generator is the whole fun:
from functools import wraps
def init(func):
"""decorator which initialises the generator
"""
@wraps(func)
def inner(*args, **kwargs):
g = func(*args, **kwargs)
g.send(None)
return g
return inner
@init
def gen(func):
x = (yield)
while True:
x = (yield func(x))
now if you have function f
def f(arg):
return arg**2
then calling f(5) is the same as
g = gen(f)
g.send(5)
I wrote a blog post where I did include this as a `useless` example:
http://pycorner.herokuapp.com/blog/5
Best regards,
Marcin
More information about the Python-list
mailing list