Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

guild
Home

closures

php closures and lambda functions rfc

Submitted by vlado on Fri, 2008-06-20 10:45.closures | drupal | php

There is a discussion going on in php land about introducing closures and lambda functions, there was even a discussion on haskell-cafe about it (chx strikes again ;). About time, I would say. Having this functionality is a bonus. Having it implemented badly or half-arsed is going to do more damage than help. This is a short summary of what do I understand from the rfc and what do I think about it.

read more | vlado's blog | 1 comment

Closure

closures | design patterns | php
Intent
Create first class behaviours capable of storing state between use
Motivation
We want to maintain the history of computation, an maybe save and reuse the computation at a particular point in time
Implementation
The basic implementation is invisible in php - static variables in functions (but that doesn't allow us to have the usual features of a language with closures because functions in php are not first class objects, i.e we can't copy them), objects (they get us there, albeit more verbously). The examples below are borrowed from io-reader: php closures. Read the post and the full code to understand the full implementation.
Example
// fixed-point combinator function Y(Lambda &$le) { return lambda(get_defined_vars(), 'Lambda $f', ' return $f->call($f); ')->call(lambda(get_defined_vars(), 'Lambda $f', ' return $le->call(lambda(get_defined_vars(), \'$x\', \' return $f->call($f)->call($x); \')); ')); }
Note
Closures and objects are dual, compare behaviour with state and state with behaviours
Yet another note:
Alternatively have a look at this implementation or my emulating closures in php blurb
Final note:
All this is better avoided in php - too many underwater rocks
add new comment
Syndicate content
Home

dikini.net

spreading confusion by accident since 1970