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


Groups > comp.lang.python > #21909

Re: Currying in Python

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@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; 'args': 0.05; 'subject:Python': 0.05; '"""': 0.07; 'behave': 0.07; 'decent': 0.07; 'type,': 0.07; 'python': 0.08; '"r"': 0.09; '-1.': 0.09; 'be:': 0.09; 'caller': 0.09; 'empty,': 0.09; 'false,': 0.09; 'positional': 0.09; 'received:mail-lpp01m010-f46.google.com': 0.09; 'exception': 0.12; 'def': 0.13; '16,': 0.15; 'argument': 0.15; '**kwargs):': 0.16; 'allocates': 0.16; 'arg': 0.16; 'type:': 0.16; '\xa0def': 0.16; '\xa0for': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'exists': 0.18; 'repeated': 0.18; 'sfxlen:2': 0.19; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; '\xa0if': 0.23; 'keyword': 0.24; 'static': 0.24; 'modify': 0.25; 'cc:2**0': 0.26; 'code': 0.26; 'all,': 0.27; 'function': 0.27; 'fact': 0.27; 'raise': 0.28; 'second': 0.28; 'message- id:@mail.gmail.com': 0.29; 'true,': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'arguments.': 0.30; 'collections': 0.30; 'none,': 0.30; 'sets.': 0.30; '(as': 0.31; 'pretty': 0.31; 'version': 0.32; 'adds': 0.32; 'go.': 0.32; 'received:209.85.215.46': 0.32; "i've": 0.32; 'does': 0.32; 'too': 0.33; 'fri,': 0.34; 'duplicate': 0.34; 'function.': 0.34; 'data.': 0.36; 'checks': 0.37; 'received:google.com': 0.37; 'skip:" 10': 0.37; 'received:209.85': 0.38; 'enough': 0.38; 'allows': 0.38; 'could': 0.38; 'some': 0.38; 'should': 0.38; 'data': 0.38; 'describe': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'being': 0.40; 'views': 0.40; 'unique': 0.61; 'more': 0.61; 'your': 0.61; 'hope': 0.61; '8bit%:4': 0.63; 'our': 0.63; 'here': 0.64; 'evaluate': 0.71; '--->': 0.84; 'descriptive': 0.84; 'remembering': 0.84; 'to:addr:mail.python.org': 0.91
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:content-transfer-encoding; bh=5lYOmqUQJ8JVs33DE8fNhhdwK4RmOReON3un3ym+lrI=; b=0j3jKgrV6c6oEd1ROq16fvsufzlHIdZLI06pd1Tetb4KXBQFb3osHNFaFbV2+0RFIs drmzwyuLa2vwfCFN5SnrsPBMXr6RUuq7A0t6THMF22LZgyJAZ86lv932U/tUqq+hUEmq wqE2D/rwkV92crx9Jtqoq42qQU353bEMR1Y6gCqsXaXS9twlfn9DEIACax4r8HLJOyIz 0YOyStTDTXeVkyg8lx9bwbIOOSkqWZhVg2rD7N8hXNsdaamINMxLAoUVy7XffM77+Koj BU3c+FfVseC6JMFBECjM7xIFX45xtNrS4/FvU64/g0dpglf37zvEcRAfcfj94Yk1oAzG zfgg==
MIME-Version 1.0
In-Reply-To <4f63e724$0$1386$4fafbaef@reader1.news.tin.it>
References <4f63e724$0$1386$4fafbaef@reader1.news.tin.it>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 19 Mar 2012 17:20:43 -0600
Subject Re: Currying in Python
To Kiuhnm <kiuhnm03.4t.yahoo.it@mail.python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.818.1332199274.3037.python-list@python.org> (permalink)
Lines 89
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332199274 news.xs4all.nl 6982 [2001:888:2000:d::a6]:57160
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21909

Show key headers only | View raw


I hope you don't mind if I critique your code a bit!

On Fri, Mar 16, 2012 at 7:21 PM, Kiuhnm
<kiuhnm03.4t.yahoo.it@mail.python.org> wrote:
> Here we go.
>
> --->
> def genCur(f, unique = True, minArgs = -1):

It is customary in Python for unsupplied arguments with no default to
use the value None, not -1.  That's what it exists for.

>    """ Generates a 'curried' version of a function. """
>    def geng(curArgs, curKwargs):
>        def g(*args, **kwargs):
>            nonlocal f, curArgs, curKwargs, minArgs;    # our STATIC data
>
>            if len(args) or len(kwargs):

Collections evaluate as true if they are not empty, so this could just be:

            if args or kwargs:

>                # Allocates data for the next 'g'. We don't want to modify our
>                # static data.
>                newArgs = curArgs[:];
>                newKwargs = dict.copy(curKwargs);
>
>                # Adds positional arguments.
>                newArgs += args;
>
>                # Adds/updates keyword arguments.
>                if unique:
>                    # We don't want repeated keyword arguments.
>                    for k in kwargs.keys():
>                        if k in newKwargs:
>                            raise(Exception("Repeated kw arg while unique = True"));
>                newKwargs.update(kwargs);

Since you're writing this for Python 3 (as evidenced by the use of the
nonlocal keyword), you could take advantage here of the fact that
Python 3 dictionary views behave like sets.  Also, you should use a
more specific exception type:

                if unique and not kwargs.keys().isdisjoint(newKwargs):
                    raise ValueError("A repeated keyword argument was supplied")

>                # Checks whether it's time to evaluate f.
>                if minArgs >= 0 and minArgs <= len(newArgs) + len(newKwargs):

With minArgs defaulting to None, that would be:

                if minArgs is not None and minArgs <= len(newArgs) +
len(newKwargs):

>                    return f(*newArgs, **newKwargs);    # f has enough args
>                else:
>                    return geng(newArgs, newKwargs);    # f needs some more args
>            else:
>                return f(*curArgs, **curKwargs);    # the caller forced the evaluation
>        return g;
>    return geng([], {});
>
> def cur(f, minArgs = -1):
>    return genCur(f, True, minArgs);
>
> def curr(f, minArgs = -1):
>    return genCur(f, False, minArgs);

The names "cur" and "curr" are terrible.  Good names should describe
what the function does without being too onerous to type, and the
addition of the duplicate "r" is not an obvious mnemonic for
remembering that the first one prohibits duplicate keyword arguments
and the second one allows them.  Why not more descriptive names like
"curry" and "curry_unique"?

That's all I've got.  All in all, it's pretty decent for a Python newbie.

Cheers,
Ian

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


Thread

Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-17 02:21 +0100
  Re: Currying in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-17 01:46 +0000
    Re: Currying in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-17 02:14 +0000
  Re: Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-19 00:49 +0100
  Re: Currying in Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-19 17:20 -0600
    Re: Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-20 01:24 +0100
    Re: Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-20 02:13 +0100
    Re: Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-20 11:06 +0100
  Re: Currying in Python Arnaud Delobelle <arnodel@gmail.com> - 2012-03-20 07:11 +0000
    Re: Currying in Python Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-20 11:13 +0100

csiph-web