473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

lambda inside of lambda?

I'm trying to use create_function to create a new function with a
function body which calls a lambda function, but with no luck.

I get an "unexpected $end" parser error. Is this possible, or do I
need to use some other approach?

Here is sample code which generates the error. The first part works,
the second part does not:
===============cut here======================================
<?php
function test($a,$b) {
if ($a==$b) return 0; return ($a<$b)?-1:1;
}

function outer($a,$b,$innerfunctionname) {
return $innerfunctionname($a,$b);
}

$innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
($a<$b)?-1:1;');

########First Part#############
#This works - prints -1
$outerfunction1 = create_function('$a,$b', "return outer(\$a,\
$b,'test');");
$result1 = $outerfunction1(3,4);
printf("Result1 is %d\n",$result1);

#######Second Part############
#This doesn't work -- prints "PHP Parse error: syntax error,
unexpected $end in /tmp/test.php(22) : runtime-created function on
line 1"
$outerfunction2 = create_function('$a,$b', "return outer(\$a,\
$b,'$innerfunction');");
$result2 = $outerfunction2(3,4);
printf("Result2 is %d\n",$result2);
?>

===============cut here======================================

Aug 17 '07 #1
2 1551
Chad Yoshikawa wrote:
I'm trying to use create_function to create a new function with a
function body which calls a lambda function, but with no luck.

I get an "unexpected $end" parser error. Is this possible, or do I
need to use some other approach?

Here is sample code which generates the error. The first part works,
the second part does not:
===============cut here======================================
<?php
function test($a,$b) {
if ($a==$b) return 0; return ($a<$b)?-1:1;
}

function outer($a,$b,$innerfunctionname) {
return $innerfunctionname($a,$b);
}

$innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
($a<$b)?-1:1;');

########First Part#############
#This works - prints -1
$outerfunction1 = create_function('$a,$b', "return outer(\$a,\
$b,'test');");
$result1 = $outerfunction1(3,4);
printf("Result1 is %d\n",$result1);

#######Second Part############
#This doesn't work -- prints "PHP Parse error: syntax error,
unexpected $end in /tmp/test.php(22) : runtime-created function on
line 1"
$outerfunction2 = create_function('$a,$b', "return outer(\$a,\
$b,'$innerfunction');");
$result2 = $outerfunction2(3,4);
printf("Result2 is %d\n",$result2);
?>

===============cut here======================================
Looks like a php bug to me. Lambdas' names start with the nul symbol
(\x00) and create_function seems to handle that incorrectly.

A test case

$a = "foo \x00";
var_dump($a);
var_dump(eval("return '$a';"));
$f = create_function('', "return '$a';");

I would report this.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Aug 17 '07 #2
C.
On 17 Aug, 07:35, gosha bine <stereof...@gmail.comwrote:
Chad Yoshikawa wrote:
I'm trying to use create_function to create a new function with a
function body which calls a lambda function, but with no luck.
I get an "unexpected $end" parser error. Is this possible, or do I
need to use some other approach?
Here is sample code which generates the error. The first part works,
the second part does not:
===============cut here======================================
<?php
function test($a,$b) {
if ($a==$b) return 0; return ($a<$b)?-1:1;
}
function outer($a,$b,$innerfunctionname) {
return $innerfunctionname($a,$b);
}
$innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
($a<$b)?-1:1;');
########First Part#############
#This works - prints -1
$outerfunction1 = create_function('$a,$b', "return outer(\$a,\
$b,'test');");
$result1 = $outerfunction1(3,4);
printf("Result1 is %d\n",$result1);
#######Second Part############
#This doesn't work -- prints "PHP Parse error: syntax error,
unexpected $end in /tmp/test.php(22) : runtime-created function on
line 1"
$outerfunction2 = create_function('$a,$b', "return outer(\$a,\
$b,'$innerfunction');");
$result2 = $outerfunction2(3,4);
printf("Result2 is %d\n",$result2);
?>
===============cut here======================================

Looks like a php bug to me. Lambdas' names start with the nul symbol
(\x00) and create_function seems to handle that incorrectly.

A test case

$a = "foo \x00";
var_dump($a);
var_dump(eval("return '$a';"));
$f = create_function('', "return '$a';");

I would report this.

--
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok

Certainly I would expect it to work too.

I used create_function() quite a lot within PfP Studio and never had
this problem.

C.

Aug 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ian McMeans | last post by:
I was bitten by a bug today that depended on how lambda works. It took me quite a while to realize what was going on. First, I made multiple lambda functions inside a loop, each of which...
9
by: Darabos Daniel | last post by:
Hi! I was doing something like this: >>> def p( x ): .... print x .... >>> l = >>> for i in range( 5 ): .... l.append( lambda: p( i ) )
6
by: George Yoshida | last post by:
Just out of curiosity, is there any way to get the name of a lambda expression? Lambdas are anonymous functions, so it's a stupid idea to get the name of it. But if it's possible, how could I? ...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
7
by: Philip Smith | last post by:
I've read with interest the continuing debate about 'lambda' and its place in Python. Just to say that personally I think its an elegant and useful construct for many types of programming task...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
7
by: Deron Meranda | last post by:
This is an optimization problem, one that occurs deep inside a loop that gets executed thousands of times. I'm looking for something in Python which would act like an identity operator, or...
11
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.