help-octave
[Top][All Lists]
Advanced

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

Re: object behavior with Octave...


From: Sergei Steshenko
Subject: Re: object behavior with Octave...
Date: Sat, 14 Feb 2009 11:55:56 -0800 (PST)



--- On Sat, 2/14/09, Ben Abbott <address@hidden> wrote:

> From: Ben Abbott <address@hidden>
> Subject: Re: object behavior with Octave...
> To: "James Moliere" <address@hidden>
> Cc: "'Octave users list'" <address@hidden>
> Date: Saturday, February 14, 2009, 5:16 AM
> On Feb 13, 2009, at 11:59 PM, James Moliere wrote:
> 
> > Hello,
> > I'm trying to do some object based programming
> with Octave.
> >
> > For example, I can do this
> > x.a = 1
> > and then to display x.a, I can type
> > x.a
> > ans = 1
> >
> > I can also do something like this
> > x.f1 = @sin
> > x.f2 = @cos
> >
> > ...running a function would look like...
> > x.f1(pi)
> > ans = 1.2246e-016
> >
> > I'd like to do something like an anonymous
> function similar to
> >
> > x.f3 address@hidden response = (arg1, arg2)
> >     response = arg1+arg2;
> > endfunction
> >
> > notice that this structure is starting to look like a
> class function?
> > ...this is what I'm trying to do.
> >
> > Thanks!
> > James Moliere
> 
> Please tell us what version of Octave are you running.
> 
> Using 3.0.3 or the developers sources you can do ...
> 
>       x.f3 = @(arg1,arg2) arg1 + arg2;
> 
> If you have something more complex ...
> 
>       function result = response (arg1, arg2)
>       ...
>       endfunction
>       x.f3 = @response;
> 
> Ben
> 

Ben, IIUC, the OP wanted an anonymous function.

Your solution with

function result = response (arg1, arg2)
...
endfunction
x.f3 = @response;

introduces a named function called "response" in this case, i.e. there
should be no other function called "response".

Is there a way to implement the same thing anonymously ?

In Perl it would be:

$x{f3} = # %x is hash, "f3" is key
sub # "sub" is a keyword, in this context it'll produce anonymous code
    # reference
  {
  my ($arg1, $arg2);
  # implement the "f3" method body here
  };

Thanks,
  Sergei.


      


reply via email to

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