KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
Je n'ai fait celle-ci plus longue que parce que je n'ai pas eu le loisir de la... – Blaise Pascal (Lettres Provinciales)
← MSDN: C#: Create Elegant Code With Anonymous Methods, Iterators, And Partial ClassesTags are so cool! →

Daily link icon Tuesday, February 21, 2006

PHP's 'create_function' function

PHP's create_function function is so cumbersome to use that I'm creating shortcuts for the most common cases. Very often all you want to do is a map and return a particular field out of an associative array. So:

<?php
function mapfield($key$func=null){
    return 
create_function('$a',"return $func(\$a['$key']);");
}
?>

Used as in:

<?php
function tags_join($tags){
    
#given an array of tags give the canonical space separated listing
    
return join(' ',array_unique(array_map(mapfield('Name'), $tags)));
}
?>
← MSDN: C#: Create Elegant Code With Anonymous Methods, Iterators, And Partial ClassesTags are so cool! →

Comments XML gif

Harry Fuecks (http://www.phppatterns.com) wrote:

You might be interested in this: http://metaphp.sourceforge.net/index.php?page=fp

The word "extension" is used in the sense of extension of the language (using PHP itself). Code in in the include.tgz - functional.php (http://sourceforge.net/projects/metaphp).

∴ Harry Fuecks | 22-Feb-2006 3:26am est | http://www.phppatterns.com | #9196

Keith (http://keithdevens.com/) wrote:

Thanks, last night I noticed that project mentioned from your J4P5 article linked from your PHP rant.

I looked at that part of the project, and I'm definitely not interested. Like every function uses an eval and they waste time and space defining things like id and not. They pretty much throw in the kitchen sink, including redefining a bunch of functional features that PHP already has. And sometimes they redefine things badly:

<?php
// The canonical "prepend" operation. Surely no faster than linear time,
// whereas a list-oriented language would do this in constant time.
// -- DB
function cons($val$list) {
    return 
array_merge(array($val), $list);
}
?>

Hello? What happened to array_unshift? These people are wankers trying to redefine Lisp in PHP (and doing it in a largely impractical way) rather than just using PHP.[1]

Plus, I already do use a largely functional style with PHP, and if I need any improvements (such as my mapfield) I can just define them myself.

Footnotes:
[1]: Note that their cons will copy all the elements of $list into a new array and return it. I know that was their intent when designing the library... everything copies and returns so that their library is more 'functional'. But this just supports my point that they're wankers.

Keith | 22-Feb-2006 8:21am est | http://keithdevens.com/ | #9197

Harry Fuecks (http://www.phppatterns.com) wrote:

...but interesting anyway. Some time I'd kinda like to do a "PHP Zoo" for strange projects people have done with it which are well out of the scope it's sanely used for.

∴ Harry Fuecks | 22-Feb-2006 2:47pm est | http://www.phppatterns.com | #9198

Keith (http://keithdevens.com/) wrote:

Though, their fx function was a good idea:

<?php
/**
    90% of lambda functions take one argument:
*/

function fx($body) {
    return 
lam('$x'$body);
}
?>

So now I have:

<?php
function fx($body){ return create_function('$a',$body); }
function 
fxe($expr){ return fx("return $expr;"); }
function 
mapfield($key$func=null){ return fxe("$func(\$a['$key'])"); }
?>

Joining in the wanking myself a tiny bit. Smiley I definitely will mine the entire library for any other decent ideas.

Keith | 22-Feb-2006 8:30pm est | http://keithdevens.com/ | #9199

Keith (http://keithdevens.com/) wrote:

Argh:

<?php
// Want the set difference?
// This computes setA - setB.
// Set members are represented as array values.

function set_difference($a$b) {
    
assert(is_array($a));
    
assert(is_array($b));
    
    
$map histogram($a);
    foreach(
$b as $x) unset($map[$x]);
    return 
array_keys($map);
}
?>

Or, you could just use array_diff.

Keith | 22-Feb-2006 11:25pm est | http://keithdevens.com/ | #9200

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.133s.

(Used 8 db queries)

mobile phone