comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Bug or desired behavior in lambda functions?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Bug or desired behavior in lambda functions? [message #91136] Mon, 08 June 2015 10:34 Go to next message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
Hello,

Despite what the documentation might suggest, and one might expect, it seems there is no way to use string variables to make lambda functions.

If I try to make a lambda function with the code inside lambda(), all is well:

IDL> l=lambda('x: x^2')
IDL> l(2)
4

However, if I put the same code in a string and pass it to lambda, it does not accept the code:

IDL> expr='x: x^2'
IDL> l=lambda(expr)
% LAMBDA: Code must be of the form "arg1,arg2,... : statement"

I expect this is caused by IDL parser's special behavior when it encounters lambda(), which is what allows this

IDL> l=lambda(x: x^2)

So I think when I put a variable name inside the lambda() call, the parser thinks is a string literal, and tries to parse the variable name as code.

Is this intended behavior, or a bug?

Paulo
Re: Bug or desired behavior in lambda functions? [message #91140 is a reply to message #91136] Mon, 08 June 2015 13:31 Go to previous messageGo to next message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
The best workaround I found is using call_function:

IDL> expr='x: x^2'
IDL> l=call_function('lambda',expr)
IDL> l(3)
9

On Monday, June 8, 2015 at 2:34:12 PM UTC-3, Paulo Penteado wrote:
> Hello,
>
> Despite what the documentation might suggest, and one might expect, it seems there is no way to use string variables to make lambda functions.
>
> If I try to make a lambda function with the code inside lambda(), all is well:
>
> IDL> l=lambda('x: x^2')
> IDL> l(2)
> 4
>
> However, if I put the same code in a string and pass it to lambda, it does not accept the code:
>
> IDL> expr='x: x^2'
> IDL> l=lambda(expr)
> % LAMBDA: Code must be of the form "arg1,arg2,... : statement"
>
> I expect this is caused by IDL parser's special behavior when it encounters lambda(), which is what allows this
>
> IDL> l=lambda(x: x^2)
>
> So I think when I put a variable name inside the lambda() call, the parser thinks is a string literal, and tries to parse the variable name as code.
>
> Is this intended behavior, or a bug?
>
> Paulo
Re: Bug or desired behavior in lambda functions? [message #91146 is a reply to message #91136] Tue, 09 June 2015 06:09 Go to previous messageGo to next message
PMallas is currently offline  PMallas
Messages: 2
Registered: June 2015
Junior Member
On Monday, June 8, 2015 at 1:34:12 PM UTC-4, Paulo Penteado wrote:
> Hello,
>
> Despite what the documentation might suggest, and one might expect, it seems there is no way to use string variables to make lambda functions.
>
> If I try to make a lambda function with the code inside lambda(), all is well:
>
> IDL> l=lambda('x: x^2')
> IDL> l(2)
> 4
>
> However, if I put the same code in a string and pass it to lambda, it does not accept the code:
>
> IDL> expr='x: x^2'
> IDL> l=lambda(expr)
> % LAMBDA: Code must be of the form "arg1,arg2,... : statement"
>
> I expect this is caused by IDL parser's special behavior when it encounters lambda(), which is what allows this
>
> IDL> l=lambda(x: x^2)
>
> So I think when I put a variable name inside the lambda() call, the parser thinks is a string literal, and tries to parse the variable name as code.
>
> Is this intended behavior, or a bug?
>
> Paulo

I experimented with the same thing, I found this to work:

IDL> expr='x: x^2
IDL> l=lambda(expr)
% LAMBDA: Code must be of the form "arg1,arg2,... : statement"
% Execution halted at: $MAIN$
IDL> l=lambda(''+expr)
IDL> l(2)
4
Re: Bug or desired behavior in lambda functions? [message #91152 is a reply to message #91146] Tue, 09 June 2015 08:42 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
Neat trick!

Thanks.

> I experimented with the same thing, I found this to work:
>
> IDL> expr='x: x^2
> IDL> l=lambda(expr)
> % LAMBDA: Code must be of the form "arg1,arg2,... : statement"
> % Execution halted at: $MAIN$
> IDL> l=lambda(''+expr)
> IDL> l(2)
> 4
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Identify whether value is integer in IF statement
Next Topic: Multiple Processing

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 07:14:46 PDT 2025

Total time taken to generate the page: 0.00502 seconds