On Tue, 07 Nov 2006 02:29:57 -0800, Martin Albrecht  
<[EMAIL PROTECTED]> wrote:
> On Tuesday 07 November 2006 10:38, you wrote:
>> >> For example, in the latest SAGE (/home/was/sage-dev) try this (at  
>> the command prompt):
>> >>
>> >>     sage: sagex("def foo(n,m): return n*m + 17")
>> >>     sage: foo(10,20)
>> >>     217
>> >
>> > Cool, what do you think of:
>> >
>> >   sage: foo = lambdax("(m,n):return n*m  + 17")
>> >   sage: foo(10,20)
>> >   217
>> >
>> > Maybe the lambda name is a bit mislead due to the limitations of
>> > lambda's in
>> > Python.
>>
>> The above wouldn't work -- or more precisely -- I don't know how to make
>> it work.  With sagex about, you can make any block of SageX code, e.g.,
>> using """'s, and call sagex on it.  The result is that it is translated
>> to C, compiled, and loaded into the running SAGE.  The block of code
>> could define all kinds of things.  In fact, this is almost identical
>> to the %pyrex (=%sagex) blocks in the notebook (and the underlying
>> implementation
>> of it is literally the same).
>
> I was thinking about something like this:
>
> # create a random function name
> fn = random_function_name()
>
> # prepend it to make the block a full function definition
> s = fn + "(m,n):return n*m  + 17"
>
> # make sure to return the new function, or assign any other way
> s += "\n return "+fn
>
> # call pyrexc, gcc, etc.
> foo = sagex(s)
>
> It would be more restricted than sagex (which could exist side-by-side)  
> but
> Python programmers would almost instantly be familiar with it as they  
> know
> the lambda thing from Python.
>
> But I haven't really thought this through.

OK, it is dumb for me to have said "The above wouldn't work" about your  
idea,
given that it definitely could.   Good idea - it's pretty neat.

Basically sagex can take an optional argument which is a dictionary into
which is should inject all the variables defined when the code is executed.
So your lambdax idea is definitely possible.

With the SAGE preparser (for .sage files and in the notebook), we could
even do something so

     f = lambdax n,m : return n * m + 1

would really get SAGEx'd.  We could also make a special syntax so
whole blocks of code can be SageX'd on the fly, without having to
be included in triple quotes.  E.g.,

n = 5

def f(m):
     return m^2 + n*m


%sagex    # switch to SageX
def foo(n,m):
      k = n*m
      c-library calls
      cdef int i.
      etc.
      return k

%sage  # switch back to SAGE

a = 17

print foo(3,7)


What do you think?   Basically, I'm suggesting extending the % syntax
 from the SAGE notebook to .sage files.    If nothing else, it would
mean that when I edit a file with embeded SageX code, it would still
get nice syntax hilighting in emacs...

There could also be a %weave mode, etc.  And the % mode code take
arguments, e.g.,

%weave(compiler="CC")

Note that I think this would all be pretty easy to implement.

William

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to