On Thu, Aug 15, 2013 at 7:39 AM, Jason Grout <[email protected]>wrote:

> On 8/15/13 9:28 AM, Jean Dubois wrote:
>
>>
>>
>> Op donderdag 15 augustus 2013 13:42:40 UTC+2 schreef Jason Grout:
>>
>>     On 8/15/13 5:24 AM, Jean Dubois wrote:
>>      > pi = lambda x: pari(x).primepi()
>>
>>     lambda is a way of making a short function without having to name it.
>>     The result of the above line is that pi(x) will call
>> pari(x).primepi().
>>        As David mentioned, the real work is done by Pari here.
>>
>>     More technical documentation for lambda functions is here:
>>     
>> http://docs.python.org/2/**tutorial/controlflow.html#**lambda-forms<http://docs.python.org/2/tutorial/controlflow.html#lambda-forms>
>>     
>> <http://docs.python.org/2/**tutorial/controlflow.html#**lambda-forms<http://docs.python.org/2/tutorial/controlflow.html#lambda-forms>
>> >
>>
>> The documentation on this url refers to Python's use of lambda.
>> Unfortunately at this moment it is not totally clear to me what the
>> inner relationships are between Python and Sage,
>> therefore I don't know what is relevant for Python and what is relevant
>> for Sage.
>>
>
> Most everything that is relevant to python is relevant to Sage.  When you
> interact with Sage, you are basically writing Python code.
>
>
>  What is the utility of  "anonymous functions"? In the example I got from
>> "adventures in group theory" the function is after all named "pi" so
>> it's no longer anonymous?
>> Maybe some one could show the benefit of using this lamba-syntax by
>> defining a function "classically" and by defining it "the lambda way"
>>
>>
> This function would have been better to define in the normal Python way:
>
> def pi(x):
>     return pari(x).primepi()
>
> Anonymous functions are nice when you don't want to go through the hassle
> of thinking of a name, like suppose you want to sort a list of numbers
> based on the cosine of the number:
>
> sorted([1,2,3,4,5], key=lambda x: cos(x))


Here's another example along these lines, which is actually useful.

You can do this:

def f(x):
    return x*random()
plot(f, (0,20))

... but with a lambda function, you can do the same with just this one line:

plot(lambda x: x*random(), (0, 20))

However, and this is the key point, the above is vastly different than

x = var('x')
plot(x*random(), (0,20))

If you can understand why, you'll appreciate lambda.

William

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to