Skip to content

heheheheheehyqo/memorize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memorize

Packagist Version Packagist PHP Version Support GitHub Workflow Status

A small function for memorize a heavy computed value

Install

composer require hyqo/memorize

Usage

Without memorize, each call getValue() method will return a new $counter value, nothing changes:

class Foo() {
    private $counter = 0;

    public function getValue(): int
    {
        return $this->counter++;
    }
};

$foo = new Foo();
$foo->getValue(); // 0
$foo->getValue(); // 1
$foo->getValue(); // 2

With memorize, each call will return the first calculated value:

use function Hyqo\Memorize\memorize;

class Foo() {
    private $counter = 0;

    public function getValue(): int
    {
        return memorize(function () {
            return $this->counter++;
        });
    }
};

$foo = new Foo();
$foo->getValue(); // 0
$foo->getValue(); // 0
$foo->getValue(); // 0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages