Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PHP is awesome

Name: Anonymous 2007-08-28 17:56 ID:xY3vpdBA

This is how you create a anonymous function in PHP:

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');


Name: Anonymous 2007-08-28 18:01 ID:gB87X6D8

(lambda (a b) (log (* a b)))

Name: Anonymous 2007-08-28 18:38 ID:SysrucEy

Equivalent code in Python:
newfunc = lambda a,b: "ln(%g) + ln(%g) = %g" % (a, b, math.log(a*b))

15 characters shorter, and will automatically format the result to use exponential format. Clearly, Python is superior to PHP

Name: Anonymous 2007-08-28 18:40 ID:iIfQEJGo

´^ิ౪^ิ) d-DURRR

Name: Anonymous 2007-08-28 18:58 ID:Heaven

notice how the parameters in >>1 are strings

Name: Anonymous 2007-08-28 19:06 ID:Heaven

>>3
Notice how this guy's arguments are actually strings. This is why Python fails.

Name: Anonymous 2007-08-28 20:09 ID:Heaven

>>6
Except that doesn't actually produce a function, it just returns a string. This is why you fail. Troll harder.

Name: Anonymous 2007-08-29 0:20 ID:OPjHCuVO

>>6
Notice how what you wrote is a string.

Name: Anonymous 2007-08-29 1:21 ID:HPI8m7N7

FACADE

Name: Anonymous 2007-08-29 2:44 ID:Heaven

a squid string? yummy-yummy!

Name: Anonymous 2007-08-29 3:04 ID:cP4HcU4N

(log .) . (*)

Point-free style doesn't even require a lambda

Name: Anonymous 2007-08-29 5:03 ID:qJ09HyrI

>>11
log `dot` (*)
    where dot = (.).(.)

Readability++

Name: Anonymous 2007-08-29 5:23 ID:9HFPURtO

int (*newfunc)(int,int)=(int(*)(int,int))(void*)"<code here>";

Name: Anonymous 2007-08-29 5:25 ID:IMdGBO6m

>>13
holy FUCK!

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: Anonymous 2007-08-29 5:25 ID:9HFPURtO

Too bad c is fast as virgin fuck

Name: Anonymous 2007-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:

function lambda($param, $expr) {
    static $number = -1;
    $number++;
    $name = '___lambda' . $number;
    eval("function $name($param) {return $expr;}");
    return $name;
}


(Note that, although this uses eval, it's not any less safe than create_function.)

Example:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array_map(lambda('$x', '$x + 1'), $arr1);

Name: Anonymous 2007-08-29 8:24 ID:8KCp2ML8

__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________lambda

Name: Anonymous 2007-08-29 8:31 ID:9Gr5YZpg

>>13
>>14

What the fuck are you talking about? Of course it doesn't work.

Name: Anonymous 2007-08-29 12:28 ID:9HFPURtO

>>18
Yes it does work.
<code here> is not "return a+b;", it's machine code.

Name: Anonymous 2007-08-29 12:48 ID:aT+3lURb

________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Name: Anonymous 2007-08-29 12:55 ID:rHpyrffL

>>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.

Name: Anonymous 2007-08-29 12:59 ID:uND3NJur

>>21
Truth.

Name: Anonymous 2007-08-29 13:19 ID:+E6rBEUr

>>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.

Name: Anonymous 2007-08-29 13:20 ID:Heaven

>>23
HOLY FUCKING TRUTHWIN

But not Haskell. Try something with less HIV.

Name: Anonymous 2007-08-29 18:45 ID:sE0X2N8k

>>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.

Name: Anonymous 2007-08-30 5:25 ID:fZsUhJAA

I like your awesome >>16

Name: Anonymous 2007-08-30 8:01 ID:IRzPcUoU

there needs to be a language with forced anonymous functions

Name: Anonymous 2007-08-30 9:50 ID:UgnTrT17

>>27
have fun doing recursion

Name: Anonymous 2007-08-30 9:54 ID:Heaven

doing recursion
why would you want to do that

Name: Anonymous 2007-08-30 9:57 ID:GnxJCSzh

>>28
learn to Y combinator you stupid fuck

Name: Anonymous 2007-08-30 10:03 ID:9pV4PBg/

>>30
please show us how to implement Y without recursion. SPOILER: YOU CAN'T. NOT EVEN IN HASKELL

Name: Anonymous 2007-08-30 10:38 ID:Heaven

>>25
Yay! Now I can call one lambda inside another.

But it is still slower than ssslllooowww, and still insecure as hell. And too many lambdas will still fuck my system.

Oops.

Name: Anonymous 2007-08-30 10:40 ID:Heaven

>>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.

Use create_function at your own risk.

Name: Anonymous 2007-08-30 11:13 ID:9o1fznpz

>>31
(define Y
  (lambda (f)
    ((lambda (x)
      (f (x x)))
     (lambda (x)
       (f (x x))))))

Name: Anonymous 2007-08-30 11:15 ID:DLtHHP7w

>>28

function(n){
 return n==1?1:(f=function(f,n){return n*f.caller(n-1)})(f,n);
}

Name: Anonymous 2007-08-30 11:45 ID:Heaven

>>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)
}

Name: Anonymous 2007-08-30 11:46 ID:Heaven

>>36
oops, here's a version that actually works:
function(n){
 return (function(i){
   return i==1?0:arguments.callee(i-1)+(function(i){
    return i==1?1:arguments.callee.caller(i-1)
   })(i-1)
  })(n+1)
}

Name: Anonymous 2007-08-30 12:21 ID:GnxJCSzh

>>31
Yes you can actually, you really need to re-read SICP

Name: Anonymous 2007-08-30 13:31 ID:Xqzm1rko

>>32
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.

It's not insecure, it's unsafe. There's no problem with security, but it's a piece of shit to write and you can fail it at write time.

>>33
Ignoring the gaping security holes of create_function
Care to explain what do you call a security hole?

>>32-33
Same person

Name: Anonymous 2007-08-30 15:15 ID:DLtHHP7w

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)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List