help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: local functions in file


From: Etienne Grossmann
Subject: Re: local functions in file
Date: Sat, 3 Aug 2002 13:53:13 +0100
User-agent: Mutt/1.3.28i

On Fri, Aug 02, 2002 at 03:04:48AM -0500, John W. Eaton wrote:
# On  2-Aug-2002, Etienne Grossmann <address@hidden> wrote:
# 
# | # No, not yet.  But I started thinking about how to implement it, and it
# | # seems to me that if we are going to allow nested functions, that it
# | # should be possible to nest them to an arbitrary depth.  For example:
# | 
# |   I use
# | 
# |   function f1 ()
# | 
# |     eval (["function f2 ()",\
# |            ...
# |            "end"]);
# | 
# |   end
# | 
# | to declare a global function 'f2', and I find it very useful.
# 
# Hmm.  It seems that this would no longer create a globally visible
# function, but would instead have to create a function local to f1, at
# least if we try to stay consistent with the idea that
# 
#   eval (CODE)
# 
# is equivalent to
# 
#   CODE
# 
# (but needed when you can't write CODE until execution time).
# 
# Why is it useful to create a function like this?

  In my case, for :

1) I have a sequence of actions to do that depends on some
   (complicated) data that is known at run-time, but doesn't change
   afterwards. I have to read the data, do tests and choose what to
   do. I have to do it many times for the same data. In order not to
   drag the data around with me and not do the test each time, I write
   down the actions in a new function, et voilĂ . I get less overhead,
   I can discard the data, since all relevant information is in the
   function and I can give a convenient (reduced) arg list to the
   function.

2) Do functions that do numerical differentiation; that's not much
   faster than with a function but is sometimes useful.

3) Define functions from other functions (compose, fix some arguments
   etc). For example, in computer vision, there are many ways to
   relate 3D point coordinates to its 2D coordinates in an image;
   perspective projection is one, but there are others, more or less
   sophisticated. Many projection models are special cases of other
   models. I write a single function for the most complicated model
   and then define the simpler models as I need them.

# Would you be able to do what you want using Matlab's inline function
# objects and function handles instead?  If so, perhaps it is worth

  Undoubtedly. Using a string as a "pointer to function" works fine
  for me, though.

# implementing those at the same time as nested functions (though Matlab
# calls them "subfunctions", I think).
# 
# jwe


  Cheers,

  Etienne

PS : A little blurb about how perl does it.
 
     When you define a new function in perl (e.g. w/ 'eval'), it is
     visible in the current "namespace". Parts or all of a namespace
     can be made visible in other namespaces in a very flexible
     manner.

     Namespaces are implemented in a similar way to structs ('hashes',
     in perl). When a variable is accessed, its name is looked for in
     the current namespace; if it is not found, it may be looked for
     in 'parent' namespaces.

-- 
Etienne Grossmann ------ http://www.isr.ist.utl.pt/~etienne



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]