Re: Partial Function Application -- Advantages over normal function?

Discussion in 'Python' started by Dave Angel, Jul 18, 2011.

  1. Dave Angel

    Dave Angel Guest

    On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    > On 7/18/2011 8:24 AM, Paul Woolcock wrote:
    >> Partial function application (or "currying") is the act of taking a
    >> function with two or more parameters, and applying some of the arguments
    >> in order to make a new function. The "hello world" example for this
    >> seems to be this:
    >> <snip>

    >
    > def makeadder(y)
    > def _add(x): return x+y
    > add2 = makeadder(2)
    >
    > <snip>


    A couple of typos in that code:


    def makeaddr(y):
    def _add(x): return x+y
    return _add


    DaveA
     
    Dave Angel, Jul 18, 2011
    #1
    1. Advertising

  2. Dave Angel wrote:

    > On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    >> def makeadder(y)
    >> def _add(x): return x+y
    >> add2 = makeadder(2)

    >
    > A couple of typos in that code:
    >
    >
    > def makeaddr(y):
    > def _add(x): return x+y
    > return _add


    I agree about the `return' statement, but not about the factory name; this
    has nothing to do with addresses (addr).

    --
    PointedEars

    Bitte keine Kopien per E-Mail. / Please do not Cc: me.
     
    Thomas 'PointedEars' Lahn, Jul 18, 2011
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote:

    > Dave Angel wrote:
    >> On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    >>> def makeadder(y)
    >>> def _add(x): return x+y
    >>> add2 = makeadder(2)

    >>
    >> A couple of typos in that code:
    >>
    >> def makeaddr(y):
    >> def _add(x): return x+y
    >> return _add

    >
    > I agree about the `return' statement, but not about the factory name; this
    > has nothing to do with addresses (addr).


    Supplemental: The above can be simplified to

    def makeadder(y): return lambda x: x + y

    --
    PointedEars

    Bitte keine Kopien per E-Mail. / Please do not Cc: me.
     
    Thomas 'PointedEars' Lahn, Jul 18, 2011
    #3
  4. On 19/07/11 00:33, Thomas 'PointedEars' Lahn wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >
    >> Dave Angel wrote:
    >>> On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    >>>> def makeadder(y)
    >>>> def _add(x): return x+y
    >>>> add2 = makeadder(2)
    >>>
    >>> A couple of typos in that code:
    >>>
    >>> def makeaddr(y):
    >>> def _add(x): return x+y
    >>> return _add

    >>
    >> I agree about the `return' statement, but not about the factory name; this
    >> has nothing to do with addresses (addr).

    >
    > Supplemental: The above can be simplified to
    >
    > def makeadder(y): return lambda x: x + y
    >


    In turn:

    makeadder = lambda y: lambda x: x + y

    Smells of Haskell.
     
    Thomas Jollans, Jul 19, 2011
    #4
  5. Dave Angel

    Dave Angel Guest

    Re: Re: Partial Function Application -- Advantages over normalfunction?

    On 01/-10/-28163 02:59 PM, Thomas 'PointedEars' Lahn wrote:
    > Dave Angel wrote:
    >
    >> On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    >>> def makeadder(y)
    >>> def _add(x): return x+y
    >>> add2 = makeadder(2)

    >> A couple of typos in that code:
    >>
    >>
    >> def makeaddr(y):
    >> def _add(x): return x+y
    >> return _add

    > I agree about the `return' statement, but not about the factory name; this
    > has nothing to do with addresses (addr).
    >


    The two changes that I made deliberately were adding the colon and
    adding the return statement. I'm not sure how the name got changed;
    that was accidental.

    DaveA
     
    Dave Angel, Jul 19, 2011
    #5
  6. Dave Angel

    Terry Reedy Guest

    On 7/19/2011 6:07 AM, Dave Angel wrote:
    > On 01/-10/-28163 02:59 PM, Thomas 'PointedEars' Lahn wrote:
    >> Dave Angel wrote:
    >>
    >>> On 01/-10/-28163 02:59 PM, Terry Reedy wrote:
    >>>> def makeadder(y)
    >>>> def _add(x): return x+y
    >>>> add2 = makeadder(2)
    >>> A couple of typos in that code:
    >>>
    >>>
    >>> def makeaddr(y):
    >>> def _add(x): return x+y
    >>> return _add

    >> I agree about the `return' statement, but not about the factory name;
    >> this
    >> has nothing to do with addresses (addr).
    >>

    >
    > The two changes that I made deliberately were adding the colon and
    > adding the return statement. I'm not sure how the name got changed; that
    > was accidental.


    Anyway, my apologies for posting quickly without testing and without
    saying so. I know better and too often leave in mistakes when I do.

    --
    Terry Jan Reedy
     
    Terry Reedy, Jul 19, 2011
    #6
  7. Dave Angel

    Ian Kelly Guest

    On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans <> wrote:
    >> Supplemental: The above can be simplified to
    >>
    >> def makeadder(y): return lambda x: x + y
    >>

    >
    > In turn:
    >
    > makeadder = lambda y: lambda x: x + y


    That's not an improvement. lambda is for making anonymous functions.
    If you're going to construct a lambda and bind it to a name, you
    should just use def.
     
    Ian Kelly, Jul 19, 2011
    #7
  8. On 19/07/11 18:49, Ian Kelly wrote:
    > On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans <> wrote:
    >>> Supplemental: The above can be simplified to
    >>>
    >>> def makeadder(y): return lambda x: x + y
    >>>

    >>
    >> In turn:
    >>
    >> makeadder = lambda y: lambda x: x + y

    >
    > That's not an improvement. lambda is for making anonymous functions.
    > If you're going to construct a lambda and bind it to a name, you
    > should just use def.


    No, it's not an improvement. It's an illustration.
     
    Thomas Jollans, Jul 19, 2011
    #8
  9. Dave Angel

    Ian Kelly Guest

    On Tue, Jul 19, 2011 at 10:58 AM, Thomas Jollans <> wrote:
    > No, it's not an improvement. It's an illustration.


    I get that. The difference I pointed out between your
    "simplification" and the other Thomas's is the reason why yours would
    be unpythonic whilst his is fine.
     
    Ian Kelly, Jul 19, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Dalberg
    Replies:
    4
    Views:
    610
    Karl Seguin
    Dec 8, 2005
  2. Sally Thompson
    Replies:
    10
    Views:
    6,216
    Sally Thompson
    Jun 26, 2004
  3. 79

    JMX advantages over RMI

    79, May 1, 2006, in forum: Java
    Replies:
    3
    Views:
    2,559
    Daniel Dyer
    May 2, 2006
  4. Kamil Burzynski
    Replies:
    17
    Views:
    25,268
    Richard Herring
    Mar 9, 2005
  5. Kurian Thayil
    Replies:
    3
    Views:
    557
    Terry Reedy
    Jul 18, 2011
Loading...

Share This Page