Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Partial Function Application -- Advantages over normal function?

Reply
Thread Tools

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

 
 
Dave Angel
Guest
Posts: n/a
 
      07-18-2011
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

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-18-2011
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-18-2011
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.
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      07-19-2011
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.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      07-19-2011
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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-19-2011
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

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      07-19-2011
On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans <(E-Mail Removed)> 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.
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      07-19-2011
On 19/07/11 18:49, Ian Kelly wrote:
> On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      07-19-2011
On Tue, Jul 19, 2011 at 10:58 AM, Thomas Jollans <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Partial Function Application -- Advantages over normal function? Kurian Thayil Python 3 07-18-2011 08:17 PM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Advantages of RAID6 Over RAID0 and RAID5 Silverstrand Front Page News 0 04-17-2006 05:47 PM
Advantages of using custom collections over datatables to bind to a datagrid? John Dalberg ASP .Net 4 12-08-2005 07:39 PM
Wide Angle Filters vs. Normal Ones.. Advantages/Disadvantages? Moe Digital Photography 7 02-19-2004 11:04 AM



Advertisments