Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'example:': 0.03; 'else:': 0.03; 'operator': 0.03; 'syntax': 0.04; 'true,': 0.05; '"""': 0.07; '(using': 0.07; 'subject:code': 0.07; 'oh,': 0.09; 'statements': 0.09; 'url:blog': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'useful,': 0.14; '""")': 0.16; '"),': 0.16; '"enter': 0.16; '"while"': 0.16; "(i'm": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; '*any*': 0.16; '10:59': 0.16; '17:30': 0.16; 'abusing': 0.16; 'assignments': 0.16; 'f(arg):': 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; 'wraps': 0.16; 'wrote:': 0.18; 'library': 0.18; "python's": 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'bonus': 0.22; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'header:User-Agent:1': 0.23; 'mon,': 0.24; "i've": 0.25; 'second': 0.26; 'post': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'on,': 0.29; 'points': 0.29; 'url:mailman': 0.30; 'evil': 0.31; 'another': 0.32; 'quite': 0.32; 'url:python': 0.33; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'so,': 0.37; 'two': 0.37; 'expressed': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'url:mail': 0.40; 'simple,': 0.60; 'most': 0.60; 'break': 0.61; 'url:5': 0.61; 'name:': 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'name': 0.63; 'july': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'deals': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; '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=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=pZmYNFwZ2lXsE9HdFX2vK0Uab1ycDxDM3fSmbqmX0O0=; b=cAD4QHX6yXjdGkj4RRVTCFoSaGPKL/5J6pZeFkyZpZA3Q0xaqauCaizAoKqYKEoxcy YIxvFars+lih2abufbs434SjvMigCbid7FzbOcmbQhlQWnZeZGWhDWNOgT52hAn/RzZQ APPrfZvhRmNku9sV3Q+xp0lGnyi9cmKP3PLKcF3J/5P/pWKJSjz0G7zzZ6GV8S7lFcME dJ4EnsOnelrhHHf39+evNq9loiY3BRfTkrEfFDAU2BPUL6mQGhI07yQXuF6HCDZSwh9j qmaqzFlEsGT/2dGATvfs4g/PLx6I6DJXzLODNPBANVuc75VkhxiRr6bK5jYFgAqBekIP F+jg== X-Received: by 10.180.100.35 with SMTP id ev3mr13163289wib.12.1372707398466; Mon, 01 Jul 2013 12:36:38 -0700 (PDT) Date: Mon, 1 Jul 2013 20:36:29 +0100 From: Marcin Szamotulski To: Joshua Landau Subject: Re: Stupid ways to spell simple code References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 156 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372707405 news.xs4all.nl 15965 [2001:888:2000:d::a6]:52344 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49588 On 17:30 Mon 01 Jul , Joshua Landau wrote: > On 1 July 2013 14:14, Chris Angelico wrote: > > On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti wrote: > >> On 2013-06-30, Chris Angelico 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