Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49571

Re: Stupid ways to spell simple code

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'syntax': 0.04; 'true,': 0.05; '(using': 0.07; 'subject:code': 0.07; 'oh,': 0.09; 'statements': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '""")': 0.16; '"),': 0.16; '"enter': 0.16; '"while"': 0.16; "(i'm": 0.16; '*any*': 0.16; '10:59': 0.16; 'abusing': 0.16; 'assignments': 0.16; 'false:': 0.16; 'inputs': 0.16; 'itertools': 0.16; 'lambda:': 0.16; 'rationale': 0.16; 'redundant': 0.16; 'subject:simple': 0.16; 'true:': 0.16; 'worse.': 0.16; 'wrote:': 0.18; 'library': 0.18; "python's": 0.19; '>>>': 0.22; 'import': 0.22; 'bonus': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'on,': 0.29; 'points': 0.29; 'message-id:@mail.gmail.com': 0.30; 'evil': 0.31; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'yield': 0.36; 'so,': 0.37; 'two': 0.37; 'expressed': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'simple,': 0.60; 'most': 0.60; 'break': 0.61; 'name:': 0.61; 'name': 0.63; 'july': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'deals': 0.65; 'to:addr:gmail.com': 0.65; 'jul': 0.74; 'beautiful,': 0.84; 'good!': 0.84; 'partial': 0.84; 'valid)': 0.84; 'this!': 0.93; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=FWF1UV3BbKL/6EYWSEdCwZAHNFjAX2VPdBLU4+pJnCs=; b=lIR5bbUywOh6ZQEH3WwsWeGbOif2Oosulq8Pizm4as4YpYQrCQX84TnMfyPQIYJM4I m9VmMbzwvbjSjUYl5JXX4po5XqQrICfLusxz8qAc5g2HK8ReQdCwCmVjPftwWE3SyeHf qDtUN8zjdqrv+V19td+KQ+EY+9pKl0es4sclgBxTHBPdd03boV+tt/I8rkWTIQ5iAH+k wBb+K9YGOVtOX1TwY1wfOB0JfKR2dvMKv9ta9lAMMw/xxW5ePQ4oVSRrPRswEwqZE0Eu Ac1U8B1Moim4VB5kg7vE2eh/RjLk3cTP3VCVngws8/rqxSBMTVfHpvDIMQcjaYKMMmLS MXog==
X-Received by 10.112.182.39 with SMTP id eb7mr11968484lbc.30.1372696286757; Mon, 01 Jul 2013 09:31:26 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAPTjJmpvPPkM8sDaDu_jwXW1kHvhfrbN4niWeE3b_mX5SeSkyA@mail.gmail.com>
References <mailman.4021.1372572404.3114.python-list@python.org> <b3dcp4Fhl04U2@mid.individual.net> <CAPTjJmpvPPkM8sDaDu_jwXW1kHvhfrbN4niWeE3b_mX5SeSkyA@mail.gmail.com>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date Mon, 1 Jul 2013 17:30:46 +0100
Subject Re: Stupid ways to spell simple code
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4071.1372696293.3114.python-list@python.org> (permalink)
Lines 114
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372696293 news.xs4all.nl 15910 [2001:888:2000:d::a6]:41949
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49571

Show key headers only | View raw


On 1 July 2013 14:14, Chris Angelico <rosuav@gmail.com> wrote:
> On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti <neilc@norwich.edu> wrote:
>> On 2013-06-30, Chris Angelico <rosuav@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.")
""")

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-06-30 16:06 +1000
  Re: Stupid ways to spell simple code Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-30 07:58 -0700
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-30 16:08 +0100
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-01 01:20 +1000
    Re: Stupid ways to spell simple code Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:26 -0600
  Re: Stupid ways to spell simple code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 17:36 +0000
    Re: Stupid ways to spell simple code Roy Smith <roy@panix.com> - 2013-06-30 13:44 -0400
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-30 22:52 +0100
  Re: Stupid ways to spell simple code Neil Cerutti <neilc@norwich.edu> - 2013-07-01 12:59 +0000
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-01 23:14 +1000
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-01 17:30 +0100
    Re: Stupid ways to spell simple code Marcin Szamotulski <mszamot@gmail.com> - 2013-07-01 20:36 +0100
      Re: Stupid ways to spell simple code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 22:09 +0000
        Re: Stupid ways to spell simple code 88888 Dihedral <dihedral88888@gmail.com> - 2013-07-01 21:16 -0700
        Re: Stupid ways to spell simple code Marcin Szamotulski <mszamot@gmail.com> - 2013-07-02 07:22 +0100
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-02 02:32 +1000
  Re: Stupid ways to spell simple code Russel Walker <russ.pobox@gmail.com> - 2013-07-02 00:33 -0700

csiph-web