-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataStructure] iterating over data structures #83
Comments
/cc @veewee |
Would it make sense to make the data structures immutable? That way the questions are answered with:
Not sure if it makes sense from a consumer pov though. I've also researched some other languages: Most use something like |
I like the idea of immutable data structures. If i understand correctly: $queue = new Queue();
$queue = $queue->enqueue('hello');
$other = $queue->enqueue('hey');
$queue->count(); // 1 ['hello']
$other->count(); // 2 ['hello', 'hey']
$queue->dequeue(); // 'hello'
$queue->count(); // 0 []
$other->count(); // 2 ['hello', 'hey'] maybe we can do the same as in |
The Having both options is nice, but also more work to create and maintain. If I understand the haskell priority queue well, it also works in an immutable way. (But I am an absolute noob in that language :) ) |
Here's a real world usage of PriorityQueue ( https://github.com/nuxed/http-server/blob/develop/src/Nuxed/Http/Server/Handler/NextMiddlewareHandler.hack#L5-L26 ): final class NextMiddlewareHandler implements Server\IHandler {
/** @var SplPriorityQueue<Server\IMiddleware> $queue */
private SplPriorityQueue $queue;
private Server\IHandler $handler;
public function __construct(SplPriorityQueue $queue, Server\IHandler $handler) {
$this->queue = clone $queue;
$this->handler = $handler;
}
public function handle(Message\IServerRequest $request): Message\IResponse
{
if (0 === $this->queue->count()) {
return await $this->handler->handle($request);
}
$middleware = $this->queue->dequeue();
return $middleware->process($request, $this);
}
} if we switch to immutable DS, this is how the API would look like ( correct me if i'm wrong ): final class NextMiddlewareHandler implements Server\IHandler {
/** @var SplPriorityQueue<Server\IMiddleware> $queue */
private SplPriorityQueue $queue;
private Server\IHandler $handler;
public function __construct(SplPriorityQueue $queue, Server\IHandler $handler) {
$this->queue = clone $queue;
$this->handler = $handler;
}
public function handle(Message\IServerRequest $request): Message\IResponse
{
if (0 === $this->queue->count()) {
return await $this->handler->handle($request);
}
[$queue, $middleware] = $this->queue->dequeue();
$this->queue = $queue;
return $middleware->process($request, $this);
}
} the return type of |
I think tupples are getting more and more known in the community. The immutable implementation is indeed more verbose.
Next on the topic of iterations:
examples: // Iterables
$items = map([...$queue], ($value) => 'something');
$items = filter([...$queue], ($event) => $event instanceof SomeEvent);
// ...
// Looping over them with more control:
// We could add an isEmpty - which makes sense
while ($queue->count()) {
$value = $queue->dequeue();
}
// More advanced -> see your NextMiddlewareHandler For your specific use-case a mutable version might make more sense. Even though I don't fully grasp the idea behind the NextMiddleware. It does something different each time it is handled, but in a regular server middleware stack - it only goes through the handle once? For immutable it could look like this: public function dispatch($event)
{
$queue = $this->queue;
while ($queue->count()) {
[$queue, $subscriber] = $queue->dequeue();
$subscriber($event);
}
return $event;
} Which could also be used with iterator functions to make it less verbose. |
It would be really nice to have a fallback with an optional mapping to https://github.com/php-ds. |
see discussion in #53 ( #53 (comment) )
Questions:
The text was updated successfully, but these errors were encountered: