Skip to content
/ buffer Public

Simple Buffer for automatically managing a stack with a callback

License

Notifications You must be signed in to change notification settings

devlop/buffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version License

Buffer

Simple Buffer for iterating over an iteratable and regularly applying a callback.

This allows you to consume a large array with minimal memory usage.

Installation

composer require devlop/buffer

Usage

Manual

This way gives you the most power, but also forces you to take more responsibility over execution.

use Devlop\Buffer\Buffer;

$bigFuckingArray = [...]; // array containing between zero and many many items

$buffer = new Buffer(
    10, // max Buffer size
    function (array $items) : void {
        // callback to apply when buffer size reaches max
    },
);

foreach ($bigFuckingArray as $key => $value) {
    $buffer->push($value); // the Buffer callback will automatically be applied when needed
}

// important, after looping over the array, remember to manually call the flush() method to apply the callback on last time if needed
$buffer->flush();

Automatic

This way is the easiest way to use the Buffer and requires least work from you.

use Devlop\Buffer\Buffer;

$bigFuckingArray = [...]; // array containing between zero and many many items

Buffer::iterate(
    $bigFuckingArray, // input iterable
    10, // max Buffer size
    function (array $items) : void {
        // callback to apply when buffer size reaches max
        // the callback will also be called one last time after finishing iterating if needed
    },
)