markrogoyski / itertools-php

PHP Iteration Tools Library
MIT License
140 stars 11 forks source link

How about iterating resources? #15

Closed Smoren closed 1 year ago

Smoren commented 1 year ago

Hi Mark!

What do you think about the functionality for iterating resources in IterTools? For example, reading files. Something like this:

class Resource {
    public static function readCsv(
        resource $stream,
        ?int $length = null,
        string $separator = ",",
        string $enclosure = "\"",
        string $escape = "\\"
    ): \Generator {
        while (($row = fgetcsv($handle, $length, $separator, $enclosure, $escape)) !== false) {
            yield $row;
        }
    }
}

$stream = fopen('path/to/file.csv');

foreach (Resource::readCsv($stream) as $row) {
    // ...
}

fclose($stream);

BTW, this may confuse the Stream namespace. What do you think, maybe rename Stream to Fluent or something else?

markrogoyski commented 1 year ago

Hi @Smoren,

Thank you for the feature suggestion. Give me some time to think about this and I'll get back to you. Sorry to make you wait.

Thanks for understanding. Mark

Smoren commented 1 year ago

@markrogoyski

Thank you for the answer.

I will wait for your decision.

Smoren commented 1 year ago

Hi @markrogoyski,

I thought about the renaming of Stream namespace and I have a very simple idea.

Maybe Iterable or IterableWrapper?

Iterable::of($smth)
    ->pairwise()
    ->toArray();

I think it looks nice :)

markrogoyski commented 1 year ago

Hi @Smoren,

Thanks for your suggestion and continued interest to improve IterTools. I have not released a new version that has the Streams feature yet, so it is still possible to make changes.

However, I would argue that Stream is right name for this for three reasons.

1) The word "stream" gives the image of things flowing together continuously, like the fluent chaining of methods together. Since the stream repeatedly processes the data, it is a form of iteration, and fits in with the theme of IterTools.

2) This is a common name for this functionality. See Java Streams, C++ Streams API, numerous python stream libraries (streams, python-stream, streampy, Streams, python-streams, python-streaming , pyStream, pystreams), etc.

3) It differentiates it clearly from the looping constructs in the library. IterTools now provides tools to power loops, and tools to process iterables in a functional pipeline, consisting of a source, zero-or-more operations, and a terminal operation.

These are my reasons. I'm happy to continue to discuss if you like. Thanks. Mark

Smoren commented 1 year ago

Hi @markrogoyski,

Your arguments sound persuasive. I no longer insist on renaming. Have you decided anything about including resources in IterTools?

markrogoyski commented 1 year ago

Hi @Smoren,

I think resources is too big a topic. See here: https://www.php.net/manual/en/resource.php. Many things return resources, including network calls and protocols, database handles, etc.

Maybe narrow the scope to iterating files or something would help refine the idea and make it workable. I can immediately think of iterating lines of a file, and lines of a CSV. Anything else you would consider? And maybe the namespace is File rather than Resource to clearly indicate the intent?

Thanks, Mark

Smoren commented 1 year ago

Hi @markrogoyski,

I think namespace File is a good idea. I'll think about it's methods and their implementation.