2017 © Pedro Peláez
 

library easy-callback

Utility for creating simple callback functions for filter and map functions in readable way

image

farafiri/easy-callback

Utility for creating simple callback functions for filter and map functions in readable way

  • Sunday, December 11, 2016
  • by farafiri
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

easy-callback

Library for providing short and easy way for creating callback., (*1)

Functional programing is very powerful concept and I like to use this paradigm in my work. However current php closure syntax is quite verbose. Check following example:, (*2)

/** filter books written by $author */
$books = array_filter($allBooks, function($book) use ($author) {
    return $book->getAuthor() === $author;
});

vs:, (*3)

use EasyCallback\f;

/** filter books written by $author */
$books = array_filter($allBooks, f()->getAuthor()->ecEq($author));

Doc / Examples

Library always works on instances of EasyCallback\Wrapper class (all functions in this library returns instance of EasyCallback\Wrapper)., (*4)

use EasyCallback\f;

f(); //Callable returning first parameter. Equivalent to:
function ($p1) {return $p1;};
f(2); //Callable returning 2-nd parameter. Equivalent to:
function ($p1, $p2) {return $p2;};
f($value, true); //Callable returning $value. Equivalent to
function () use ($value) {return $value;};

//EasyCallback\Wrapper support array syntax:
$f['a']; // Equivalent to:
function(...$params) use ($f) {return $f(...$params)['a'];}

f()['a']; //Equivalent to:
function($p1) {return $p1['a'];};
f(2)['a']; //Equivalent to:
function($p1, $p2) {return $p2['a'];};
f()['a']['b']; //Equivalent to:
function($p1) {return $p1['a']['b'];};
f($value, true)['a']; //Equivalent to:
function() use ($value) {return $value['a'];};
f($value, true)[f()]; //Equivalent to:
function($p1) use ($value) {return $value[$p1];};

//Similarly get property syntax and method call syntax is supported:
f()->exampleProperty; //Equivalent to:
function($p1) {return $p1->exampleProperty;};
f()->exampleMethod(1); //Equivalent to:
function($p1) {return $p1->exampleMethod(1);};
f()->exampleMethod1()->exampleMethod2(); //Equivalent to:
function($p1) {return $p1->exampleMethod1()->exampleMethod2();};

The Versions

11/12 2016

dev-master

9999999-dev

Utility for creating simple callback functions for filter and map functions in readable way

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires