https://github.com/devlop/buffer
Simple Buffer for automatically managing a stack with a callback
https://github.com/devlop/buffer
php
Last synced: about 1 year ago
JSON representation
Simple Buffer for automatically managing a stack with a callback
- Host: GitHub
- URL: https://github.com/devlop/buffer
- Owner: devlop
- License: mit
- Created: 2020-12-12T13:41:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-01T04:44:36.000Z (about 5 years ago)
- Last Synced: 2025-02-19T18:17:48.261Z (over 1 year ago)
- Topics: php
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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
```bash
composer require devlop/buffer
```
# Usage
## Manual
This way gives you the most power, but also forces you to take more responsibility over execution.
```php
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.
```php
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
},
)
```