Skip to content

Feature: fluent interface #45

@SamMousa

Description

@SamMousa

Would it be nice to have some kind of fluent interface?
That would allow for really readable code like:

toIter([1, 2, 3, 4, 5, 6])
    ->filter(function($v) { return $v % 2 === 0; })
    ->slice(0, 1)

I think it can be implemented relatively simply:

class Test implements \IteratorAggregate {
    private $iterator;

    public function __construct(iterable $iterator)
    {
        $this->iterator = \iter\toIter($iterator);
    }

    public function getIterator()
    {
        return $this->iterator;
    }

    public function filter(\Closure $predicate) {
        return new self(\iter\filter($predicate, $this));
    }
}

What do you think?

I could implement this and if we alter toIter to return the new type, it wouldn't even break backwards compatibility since people are expecting a generator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions