Thursday, May 29, 2014

Obtaining Closure with Anonymous Functions - Notes

My Notes From: Obtaining Closure with Anonymous Functionshttps://joind.in/10657
Andrew Cassell
Loves Closures!
Normal Regular Named Functions. 
Boring!

Lambdas
$sum = function () { return true; }
echo $sum;

Lambdas are instance of Closure

callable, or Closure are good for type hinting.

Anonymous Functions aren't scary.

function($a) {return $a; }

Lambda and Closures have scope.

$LperGal = 10;
$galToL = function($gallons) use ($LperGal) {
echo $gallons * $LprGal;
}
echo $galToL(10);

Pass by reference... &$LperGal if you want to edit the value.

Closures have..
bindTo
--- bind ----
--- __invoice ----

bindTo changes the scope of $this when used inside the function.

Reasons to use.
Don't pollute namespace with functions.
Fewer function names laying around - reduces cruft.
Map/Reduce/Filter
array_map, array_reduce
Database
Currying
Memoization????
Monads
Dependancy Injection

Don't use function_create or the Closure::bind .... because???

Curry/Database Display using his funky library.
Database ORM thing that's popular in Russia??

Memoization -> the same as caching
Just storing stuff in an array to avoid double computation.

Monads -> MonadPHP
Allows you to focus on just making sure the data you want is available instead
of testing all the data.... I think??

No comments:

Post a Comment