Beispiel #1
0
 /**
  * Add "title" macros for "html" service location.
  *
  * @param  \Illuminate\Contracts\Foundation\Application  $app
  *
  * @return void
  */
 protected function addHtmlExtensions(Application $app)
 {
     $html = $app->make('html');
     $html->macro('title', function ($title = null) use($html) {
         $builder = new Title($html, memorize('site.name'), ['site' => get_meta('html::title.format.site', '{site.name} (Page {page.number})'), 'page' => get_meta('html::title.format.page', '{page.title} — {site.name}')]);
         return $builder->title($title ?: trim(get_meta('title', '')));
     });
 }
 /**
  * Build HTML::title() callback.
  *
  * @param  \Illuminate\Contracts\Foundation\Application  $app
  *
  * @return callable
  */
 protected function buildHtmlTitleCallback(Application $app)
 {
     return function ($title = null) use($app) {
         $title = $title ?: trim(get_meta('title', ''));
         $page = Paginator::resolveCurrentPage();
         $data = ['site' => ['name' => memorize('site.name')], 'page' => ['title' => $title, 'number' => $page]];
         $data['site']['name'] = $this->getHtmlTitleFormatForSite($data);
         $output = $this->getHtmlTitleFormatForPage($data);
         return $app->make('html')->create('title', trim($output));
     };
 }
Beispiel #3
0
{
    public function compute($key);
}
class Fancy implements FancyInterface
{
    public function compute($key)
    {
    }
}
# OOP way to Memorization
class FancyCache implements FancyInterface
{
    protected $wrapped;
    protected $cached = array();
    public function __construct(FancyInterface $wrapped)
    {
        $this->wrapped = $wrapped;
    }
    public function compute($key)
    {
        if (!isset($this->cached[$key])) {
            $this->cached[$key] = $this->wrapped->compute($key);
        }
        return $this->cached[$key];
    }
}
# Fucntional Way to memorize.
$f = new Fancy();
$callable = [$f, 'compute'];
$f_cache = memorize($callable);
$f_cache($key);
Beispiel #4
0
        $full_args = array_merge($args, func_get_args());
        return call_user_func_array($func, $full_args);
    };
}
$wild = new Card('5H');
function is_wild($wild, $card)
{
    return $wild->value == $card->value;
}
function find_pair($is_wild, $card1, $card2)
{
    return $card1->value == $card2->value || $is_wild($card1) || $is_wild($card2);
}
$wild_checker = partial('is_wild', new Card('5H'));
$pair_checker = partial('find_pair', $wild_checker);
$is_pair = $pair_checker(new Card('2H'), new Card('8D'));
# a step further: with memorization
function memorize($function)
{
    return function () use($function) {
        static $results = [];
        $args = func_get_args();
        $key = serialize($args);
        if (!isset($results[$key])) {
            $results[$key] = $function($args);
        }
        return $results[$key];
    };
}
$cached_checker = memorize($pair_checker);
$is_pair = $cached_checker(new Card('2H'), new Card('8D'));