Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

functions which take functions

102 views
Skip to first unread message

Kiuhnm

unread,
Apr 9, 2012, 2:57:00 PM4/9/12
to
Do you have some real or realistic (but easy and self-contained)
examples when you had to define a (multi-statement) function and pass it
to another function?
Thank you.

Kiuhnm

Ulrich Eckhardt

unread,
Apr 10, 2012, 8:29:03 AM4/10/12
to
Am 09.04.2012 20:57, schrieb Kiuhnm:
> Do you have some real or realistic (but easy and self-contained)
> examples when you had to define a (multi-statement) function and pass it
> to another function?

Take a look at decorators, they not only take non-trivial functions but
also return them. That said, I wonder what your intention behind this
question is...

:)

Uli

Kiuhnm

unread,
Apr 10, 2012, 9:36:26 AM4/10/12
to
That won't do. A good example is when you pass a function to re.sub, for
instance.

Kiuhnm

Eelco

unread,
Apr 10, 2012, 5:43:08 PM4/10/12
to
Wont do for what? Seems like a perfect example of function-passing to
me.

If you have such a precise notion of what it is you are looking for,
why even ask?

Kiuhnm

unread,
Apr 11, 2012, 6:55:26 AM4/11/12
to
On 4/10/2012 23:43, Eelco wrote:
> On Apr 10, 3:36 am, Kiuhnm<kiuhnm03.4t.yahoo.it> wrote:
>> On 4/10/2012 14:29, Ulrich Eckhardt wrote:
>>
>>> Am 09.04.2012 20:57, schrieb Kiuhnm:
>>>> Do you have some real or realistic (but easy and self-contained)
>>>> examples when you had to define a (multi-statement) function and pass it
>>>> to another function?
>>
>>> Take a look at decorators, they not only take non-trivial functions but
>>> also return them. That said, I wonder what your intention behind this
>>> question is...
>>
>>> :)
>>
>> That won't do. A good example is when you pass a function to re.sub, for
>> instance.
>>
>> Kiuhnm
>
> Wont do for what? Seems like a perfect example of function-passing to
> me.

That's more an example of function transformation.

> If you have such a precise notion of what it is you are looking for,
> why even ask?

As I said, I need real-life examples. Enough with prime numbers and
recursive qsort...

Kiuhnm

Antti J Ylikoski

unread,
Apr 11, 2012, 10:01:38 AM4/11/12
to
A function to numerically integrate another function comes as follows:

------------------------------------------------------

# Adaptive Simpson's integral.
#
# AJY 03-28-2012 from the Wikipedia.

import math

def simpsonsRule(f,a,b):
c = (a+b) / 2.0
h3 = abs(b-a) / 6.0
return h3*(f(a) + 4.0*f(c) + f(b))

def recursiveASR(f,a,b,eps,whole):
"Recursive implementation of adaptive Simpson's rule."
c = (a+b) / 2.0
left = simpsonsRule(f,a,c)
right = simpsonsRule(f,c,b)
if abs(left + right - whole) <= 15*eps:
return left + right + (left + right - whole)/15.0
return recursiveASR(f,a,c,eps/2.0,left) + \
recursiveASR(f,c,b,eps/2.0,right)

def adaptiveSimpsonsRule(f,a,b,eps):
"Calculate integral of f from a to b with max error of eps."
return recursiveASR(f,a,b,eps,simpsonsRule(f,a,b))

def invx(x):
return 1.0 / x


print("Simpson integral : ", \
adaptiveSimpsonsRule(invx,1.0,2.0,.000000001))
print("Exact value ln(2): ", math.log(2.0))
print("Value of epsilon : ", .000000001)


------------------------------------------------------------


kind regards, Antti J Ylikoski
Helsinki, Finland, the EU
http://www.tkk.fi/~ajy/
http://www.tkk.fi/~ajy/diss.pdf
antti.y...@gmail.com

Tim Roberts

unread,
Apr 12, 2012, 2:07:37 AM4/12/12
to
Kiuhnm <kiuhnm03.4t.yahoo.it> wrote:
>
>That won't do. A good example is when you pass a function to re.sub, for
>instance.

This is an odd request.

I often pass functions to functions in order to simulate a C switch
statement, such as in a language translator:

commands = {
'add': doAdd,
'subtract' : doSubtract,
'multiply' : doMultiply,
'divide' : doDivide
}

nextCommand = parseCommandLine( line )
invokeCommand( commands[NextCommand] )
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Kiuhnm

unread,
Apr 12, 2012, 11:48:44 AM4/12/12
to
On 4/11/2012 16:01, Antti J Ylikoski wrote:
> On 9.4.2012 21:57, Kiuhnm wrote:
>> Do you have some real or realistic (but easy and self-contained)
>> examples when you had to define a (multi-statement) function and pass it
>> to another function?
>> Thank you.
>>
>> Kiuhnm
>
> A function to numerically integrate another function comes as follows:
[...]

Thank you.

Kiuhnm

Kiuhnm

unread,
Apr 12, 2012, 11:58:43 AM4/12/12
to
On 4/12/2012 8:07, Tim Roberts wrote:
> Kiuhnm<kiuhnm03.4t.yahoo.it> wrote:
>>
>> That won't do. A good example is when you pass a function to re.sub, for
>> instance.
>
> This is an odd request.

All shall be revealed :)

>
> I often pass functions to functions in order to simulate a C switch
> statement, such as in a language translator:
>
> commands = {
> 'add': doAdd,
> 'subtract' : doSubtract,
> 'multiply' : doMultiply,
> 'divide' : doDivide
> }
>
> nextCommand = parseCommandLine( line )
> invokeCommand( commands[NextCommand] )

I like that very much. Thank you.

Kiuhnm

Jan Kuiken

unread,
Apr 12, 2012, 1:29:56 PM4/12/12
to
On 4/9/12 20:57 , Kiuhnm wrote:

> Do you have some real or realistic (but easy and self-contained)
> examples when you had to define a (multi-statement) function and pass it
> to another function?

I don't use it daily but the first argument of list.sort, i.e. the
compare function springs to mind.

Jan Kuiken


Kiuhnm

unread,
Apr 12, 2012, 7:07:57 PM4/12/12
to
Yes, I guess there are times when a lambda is not enough and one needs
to define a real function.
Thanks.

Kiuhnm

Antti "Andy" Ylikoski

unread,
Apr 13, 2012, 9:23:14 AM4/13/12
to
Ref: numerical integration, one of the most famous methods probably is
the Romberg method:

http://en.wikipedia.org/wiki/Romberg%27s_method

the program in the Wikipedia article above is in C, but translating it
into Python would be very, very easy.

yours, Antti J Ylikoski
0 new messages