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


Groups > comp.lang.python > #21919

Re: Currying in Python

References <4f63e724$0$1386$4fafbaef@reader1.news.tin.it> <CALwzidndq-u4hqK8GDXoCDQhT22fDJbaiFkmgrQvv=gAs0i4EQ@mail.gmail.com>
Date 2012-03-20 07:11 +0000
Subject Re: Currying in Python
From Arnaud Delobelle <arnodel@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.822.1332227523.3037.python-list@python.org> (permalink)

Show all headers | View raw


On 19 March 2012 23:20, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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

I don't know if all the rest of the code is below, but this line above
would only be necessary if you want to rebind f, curArgs, minArgs.
You don't seem to do it, so I think this line is unnecessary.

Also, your naming of variables disagrees with PEP 8 :)

>>            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[:];

Semicolon to end a statement?

>>                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
> --
> http://mail.python.org/mailman/listinfo/python-list

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