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

2011-07-19 Thread Thomas Jollans
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Kurian Thayil
On Tue, Jul 19, 2011 at 11:52 AM, Thomas Jollans t...@jollybox.de wrote:

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


Hi All,

Thanks guys for the reply. I now understand something. Since Im a newbie,
let me read over and over so that I will get the complete picture of
everyone's idea. :-)

Regards,
Kurian Thayil.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Dave Angel

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Ian Kelly
On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans t...@jollybox.de 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Terry Reedy

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Thomas Jollans
On 19/07/11 18:49, Ian Kelly wrote:
 On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans t...@jollybox.de 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-19 Thread Ian Kelly
On Tue, Jul 19, 2011 at 10:58 AM, Thomas Jollans t...@jollybox.de 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Partial Function Application -- Advantages over normal function?

2011-07-18 Thread Kurian Thayil
Hi,

I am a newbie in python and would like to learn GUI programming. I would like 
to know what exactly is Partial Function Applicaton (functool.partial())? Or 
how is it advantageous compared to normal functions? Or is there any 
advantange? Thanks in advance.

Regards,
Kurian Thayil.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Paul Woolcock
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:

Let's say you have a function called `add`, that takes two parameters:

 def add(left, right):
... return left + right

Now let's say you want a function that always adds 2 to a number you give
it.  You can use partial function application to do this:

 from functools import partial
 add2 = partial(add, right=2)

Now, you have a new function, `add2`, that takes one parameter:

 add2(4)
  6


---
Paul Woolcock
pwool...@gmail.com





On Mon, Jul 18, 2011 at 6:13 AM, Kurian Thayil kurianmtha...@gmail.com
wrote:

 Hi,

 I am a newbie in python and would like to learn GUI programming. I would
like
 to know what exactly is Partial Function Applicaton (functool.partial())?
Or
 how is it advantageous compared to normal functions? Or is there any
 advantange? Thanks in advance.

 Regards,
 Kurian Thayil.
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Steven D'Aprano
Kurian Thayil wrote:

 Hi,
 
 I am a newbie in python and would like to learn GUI programming. I would
 like to know what exactly is Partial Function Applicaton
 (functool.partial())? Or how is it advantageous compared to normal
 functions? Or is there any advantange? Thanks in advance.

It is mostly for functional programming style.

But one lucky side-effect of the implementation is that partial functions
*may* sometimes be faster than the alternative written in pure Python,
provided the original function is written in C:


from functools import partial
from operator import add

def add_one(x):
return add(1, x)  # Like 1+x

add_two = partial(add, 2)

from timeit import Timer
setup = from __main__ import add_one, add_two
t1 = Timer(add_one(42), setup)
t2 = Timer(add_two(42), setup)



And in action:

 t1.timeit()
0.7412619590759277
 t2.timeit()
0.3557558059692383

So in this example, the partial function is about twice as fast as the one
written in Python.

This does not necessarily apply for all functions, but it sometimes is
useful.


-- 
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread woooee
Partial can be used in a GUI program, like Tkinter, to send arguments
to functions.  There are other ways to do that as well as using
partial.  The following program uses partial to send the color to the
change_buttons function.
from Tkinter import *
from functools import partial

class App:
   def __init__(self, parent):
self.my_parent = parent
self.my_parent.geometry(200x100+10+10)

self.R = list()
for ctr, color in enumerate((Red, Blue, Green)):
btn = Radiobutton(self.my_parent, text=color, value=ctr+1,
  command=partial(self.change_buttons, color))
btn.grid(row = 2, column = ctr+1)
btn.deselect()
self.R.append(btn)
self.R[0].select()
self.change_buttons(Red)


   def change_buttons(self, color):
   self.my_parent.configure(bg=color)
   for btn in self.R:
btn.configure(bg=color)

if __name__ == __main__:
root = Tk()
root.title (Color Option)
app = App(root)
root.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Terry Reedy

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:

Let's say you have a function called `add`, that takes two parameters:


 def add(left, right):

 ... return left + right

Now let's say you want a function that always adds 2 to a number you
give it.  You can use partial function application to do this:

  from functools import partial
  add2 = partial(add, right=2)

Now, you have a new function, `add2`, that takes one parameter:

  add2(4)


Or you can directly write

def add2(x): return x + 2

or more generically

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

functool.partial is essential a generic version of makeadder in that it 
also abstract the function/operator. It is useful when one has a 
function but perhaps not the source. It's limitation is that args are 
frozen left to right while the above example freezes the right operand.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Terry Reedy

On 7/18/2011 3:23 PM, woooee wrote:

Partial can be used in a GUI program, like Tkinter, to send arguments
to functions.  There are other ways to do that as well as using
partial.  The following program uses partial to send the color to the
change_buttons function.
from Tkinter import *
from functools import partial

class App:
def __init__(self, parent):
 self.my_parent = parent
 self.my_parent.geometry(200x100+10+10)

 self.R = list()
 for ctr, color in enumerate((Red, Blue, Green)):
 btn = Radiobutton(self.my_parent, text=color, value=ctr+1,
   command=partial(self.change_buttons, color))
 btn.grid(row = 2, column = ctr+1)


This is a nice illustration. For future reference: enumerate now takes a 
start value as second parameter. Given as 1, you do not need to remember 
to add 1 for each usage.


for ctr, color in enumerate((Red, Blue, Green),1):
btn = Radiobutton(self.my_parent, text=color, value=ctr,
  command=partial(self.change_buttons, color))
btn.grid(row = 2, column = ctr)


 btn.deselect()
 self.R.append(btn)
 self.R[0].select()
 self.change_buttons(Red)

def change_buttons(self, color):
self.my_parent.configure(bg=color)
for btn in self.R:
 btn.configure(bg=color)

if __name__ == __main__:
 root = Tk()
 root.title (Color Option)
 app = App(root)
 root.mainloop()



--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Dave Angel

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Thomas 'PointedEars' Lahn
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-18 Thread Thomas 'PointedEars' Lahn
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.
-- 
http://mail.python.org/mailman/listinfo/python-list