I don't have a clue how that works.. is it portable? (not that I'd actually use it in a real C program :p)
Amazing!
Name:
Anonymous2007-08-29 5:25 ID:9HFPURtO
Too bad c is fast as virgin fuck
Name:
Anonymous2007-08-29 5:32 ID:dqo1AGje
PHP's create_function and 1.5-class functions (variable functions, e.g. $a = 'strlen'; $a($b)) as well as array_map and company can make your life easier if you have to work with PHP. The syntax for create_function, or lack of, requires quote hell though, and you have no nested scoping, so all you're getting are anonymous functions. At most, you can create an inefficient read-only closure if you var_export() your outer variables into the anonymous function's code.
However, there's a problem with create_function: PHP doesn't really have anon functions, they are all namefags, and they are returned as functions with an invalid character in their name. They work for $a = create_function(...); $a($b); but they won't work if you try to use one inside another, e.g. $a = create_function(...); $b = create_function(..., "... $a($c) ...");. In order to fix this, and to simplify the creation of simple no-statement anon functions, I've created this more Pythonic function I use to spare myself of some pain:
>>1
PHP's anonymous functions are putrid. Trust me, tried using them for a real live project.
All it does is have the interpreter make a function called something gay like __LAMBDA001 and so forth. You have to assign a name to that.
They run slower than slowpoke, they are a horrible security risk since it blindly passes the input into the interpreter, they can't be GC'd well (but PHP's GC already sucks, so yeah...) and it's a bitch to get one lambda inside another.
Don't fucking bother. Just make a fucking function. Save yourself some headahces.
>>1
PHP's built-in library is putrid. Trust me, tried using it for a real live project.
All it does is have the interpreter run a function called something gay like __GETSOMEAIDS and so forth. You have to assign a name to that.
It runs slower than slowpoke, it is a horrible security risk since it blindly passes the input into the interpreter, it can't be GC'd well (because PHP's GC already sucks, so yeah...) and it's a bitch to get one script inside another.
Don't fucking bother. Just write in fucking Haskell. Save yourself some headahces.
>>21
Read my awesome >>16, I addressed half of your issues. And of course functions can't be disposed of in PHP, so you better not spawn them like fuck inside a loop.
>>25
Great! Ignoring the gaping security holes of create_function (wait, PHP is a gaping security hole), ssssslllow speed, and the chance to completely fuck your system if you make too many lambdas (seen it happen), we can now call a lambda inside a lambda.
>>35
an even better example of recursion with anonymous functions: function(n){
return (function(i){
return i==1?0:arguments.callee(i-1)+(function(){
return i==1?1:arguments.callee.caller(i-1)
})(i-1)
})(n+1)
}
Of course, you can't expect for ($i = 100000; --$i; lambda(...)); to work decently.
why not? stuff like that works just fine in javascript... var a=new Array();
for( i = 100000; --i ; (function(){ a[i] = i })() );
alert(a.length)
>>39
No shit it was the same person. Fourchon is so slow I didn't think it took my first post. Perhaps it needs an EXPERT PROGRAMMER to rewrite it.
>It's not noticeably slow if you don't abuse it. Of course, you can't expect for ($i = 100000; --$i; lambda(...)); to work decently.
Why would you not expect a loop creating lambdas to work decently? I've done that in plenty of other languages. What's the excuse there?
>It's not noticeably slow if you don't abuse it. Of course, you can't expect for ($i = 100000; --$i; lambda(...)); to work decently.
create_function() just blindly calls eval on code passed into it. Oops!
Also, let's not forget the fact that Zend ignores optimizations on eval()ed code.
All of this adds up to one thing -- PHP's create_function and eval are horribly fucked... much like the language itself.
Name:
Anonymous2007-08-30 16:13 ID:kjnv7mW9
lambda functions unusable in create_function()
I made five distinct tests to bracket the issue:
TEST 1:
// Creating a function that calls an ordinary function (explict name)
function always_true(){return true;}
$afun = create_function('', 'return !always_true();');
echo "Calling $afun() " . ($afun() ? "returns true\n" : "returns
false\n");
TEST 2:
// Creating a function that calls an ordinary function (variable name)
function always_true(){return true;}
$b1 = 'always_true';
$bfun = create_function('','return !'.$b1.'();');
echo "Calling $bfun() " . ($bfun() ? "returns true\n" : "returns
false\n");
TEST 3:
// Creating a function that calls an anonymous function (explicit
name);
$c1 = create_function('','return true;');
$cfun = create_function('','return !'.chr(0).'lambda_3();');
echo "Calling $cfun() " . ($cfun() ? "returns true\n" : "returns
false\n");
TEST 4:
// Creating a function that calls an anonymous function (variable name)
$d1 = create_function('','return true;');
$dfun = create_function('','return !'.$d1.'();');
echo "Calling $dfun() " . ($dfun() ? "returns true\n" : "returns
false\n");
TEST 5:
// Using is_callable to get a callable version of anonymous function
name
$e1 = create_function('', 'return true;');
if(!is_callable($e1,false,$callable_e1)) die("Couldn't call anonymous
function.");
echo "Using $callable_e1 as name of anonymous function\n";
$efun = create_function('','return !'.$callable_e1.'();');
echo "Calling $efun() " . ($efun() ? "returns true\n" : "returns
false\n");
Expected result:
----------------
TEST 1:
Calling lambda_1() returns false
TEST 2:
Calling lambda_1() returns false
TEST 3:
Calling lambda_2() returns false
TEST 4:
Calling lambda_2() returns false
TEST 5:
Using lambda_1 as name of anonymous function [?]
Calling lambda_2() returns false
Actual result:
--------------
Actual Results:
TEST 1:
Calling lambda_1() returns false
TEST 2:
Calling lambda_1() returns false
TEST 3:
Parse error: parse error, unexpected '}' in test.php : runtime-created
function on line 1
Fatal error: Function name must be a string in test.php on line 4
TEST 4:
Parse error: parse error, unexpected '}' in test.php : runtime-created
function on line 1
Fatal error: Function name must be a string in test.php on line 4
TEST 5:
Using as name of anonymous function
Parse error: parse error, unexpected ')' in test.php : runtime-created
function on line 1
Fatal error: Function name must be a string in test.php on line 6
Name:
Anonymous2007-08-30 17:48 ID:Xqzm1rko
>>40-41
Because PHP does not have true anonymous or first-class functions you fuckface! Of course JavaScript can do it, but JavaScript is conceptually better than PHP in a lot of ways and does actually support functional programming. There's no excuse other than sucking. I'm not defending PHP, I'm just posting what I do to make my life better doing PHP. (At least I can pretend I'm doing something that resembles FP, have dynamic typing and built-in lists and dictionaries; it would be much, much worse with Java.)
create_function() just blindly calls eval on code passed into it. Oops!
So? You're not going to do create_function('$x', $_REQUEST['lol']), are you? You know what create_function is for, don't do anything crazy. Also, eval is SNAFU, but for other reason (that return insanity).
>>42
Yes, that's why you do what I said in >>16 to at least support it. I found that by accident as I was doing exactly that, so I cursed the guys who did PHP, and worked around it.
>>43
So, why the fuck should we use bad hacks with create_function? It's a horrible chunk of sludge. FP and PHP do not mix. GB2LISP. (actually love Lisp myself)
And no shit you aren't going to create_function('$foo', '$foo'); but what happens when you fuck up and use double quotes and end up with variable substitution you don't want (at all). What happens if the input is (gulp) generated or taken from a DB (seen it, what the fuck...)?
You really want to open up a dynamic entry point directly into the parser? In PHP?!?!?
Maybe in a language that wasn't already insecure, sure. But jesus... PHP?
>>45
Yeah, thanks, I really needed to hate PHP even more. Fuck even Python has less fucked up lambdas, and those are obviously implemented precisely in a manner to punish the people who want them.
Name:
Anonymous2010-04-21 15:09
>>46
They've done it somewhat correctly now. $f = function ($x) {return $x*2;}
echo $f(5); // 10
Say what you want about PHP, but at least it's never stagnant. It may never be the fastest or the most elegant, but it will always be an adequate tool for the job, for as long as server-side interpreted languages make sense on the web.
Say what you want about PHP, but at least it's never stagnant. It may never be the fastest or the most elegant, but it will always be an adequate tool for the job, for as long as server-side interpreted languages make sense on the web.
>>48
Wow, as if my objections to PHP have anything to do with speed or even elegance.
it will always be [...] adequate
No, this is my objection. In no sense is PHP adequate. The worst part of the whole mess is that it is invariably written by people who think it is adequate. And then they go and plug the resulting foetid mess into a relational database most of the time. Gross!
Name:
Anonymous2010-04-21 16:35
>>51 foetid I didn't know /prog/ was this literate.
>>56
I have bad news for you. My American English spell check gave me the go-ahead on that one. If you weren't literally a child you might remember that has been the preferred spelling on this side of the pond until perhaps recently. And no, I will not quit being old. At least not until Dennis Ritchie dies.
Name:
Anonymous2010-04-21 17:22
>>57
You've misconfigured your spelling checker. The American spelling is, and always has been, fetid, whereas the spelling you used, foetid, is British. You can tell because the British managed not to mangle the Latin the word is derived from.
Since you seem to be quite bad at technology, perhaps you should find a paper dictionary to confirm this for you if you do not believe me.
>>51
PHP is certainly adequate. It is a good solution for small web projects on shared hosting. It was built to interface with relational databases from its earliest versions, and it's not difficult to understand why. So far you've yet to indicate any reason behind your objections.
>>59
Given its wonderful track record on security (which in the offcial implementation is mainly a consequence of every single contributor being a brain-damaged moron, though its duplication in its imitators shows that much of it is owing to the dreadful mess of a language that it is as well), any project on shared hosting is precisely what it's worst for.
>>58
My spell checker is fine, but do go back to your hooked on fonics. I am not sure which dictionary you'd expect me to check -- any that publish only misspellings is certainly incorrect and, I would, hope unrepresentative (I happen to know, in this case from experience.) Perhaps if you'd read more you'd have found literate Americans using the old form. Are they wrong to do this because they're American or are they merely discounted for being literate?
>>59 So far you've yet to indicate any reason behind your objections.
Oh you wanted real, defensible reasons? I don't care to argue it, but if you want examples there are a ton of them at http://php.net/ -- not to mention reams of data supporting the notion that PHP programmers are the worst kind.
>>61
It's well-known that literate Americans use British spelling, because everything worth reading was written by Englishmen. That doesn't have any bearing on what the correct spelling in American is.