Skip to content
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

Issue with distinct #341

Closed
chedkowski-leonsoftware opened this issue May 9, 2024 · 3 comments
Closed

Issue with distinct #341

chedkowski-leonsoftware opened this issue May 9, 2024 · 3 comments
Labels

Comments

@chedkowski-leonsoftware
Copy link

chedkowski-leonsoftware commented May 9, 2024

Steps required to reproduce the problem

I have two arrays:

$base_list = Collection::fromIterable([1, 2]);
$excluded_list = Collection::fromIterable([2])->distinct();

And I want to efficiently remove elements from the base list using the second list:

$excluded_map = $excluded_list->associate(
	callbackForKeys: static fn (int $key, $nid) => $nid,
	callbackForValues: static fn ($value) => $value,
);

$result = $base_list->filter(static fn ($row) => $excluded_map->get($row) === null);

Expected Result

  • [1]

Actual Result

  • [1,2]

Version

  • "php": ">=8.3",
  • "loophp/collection": "^7.0",

I found that something is wrong with ->distinct(). When I remove it, the code above works fine.

It seems to me that 'distinct' shouldn't have an impact on the outcome in this case. Am I mistaken 😉?

@drupol
Copy link
Collaborator

drupol commented May 9, 2024

Hi,

Thanks for the issue and taking the time to provide a quick reproducer.
The issue might be in the get operation, I need to make further check as soon as I have some time.

To fix the issue gracefully, I would use diff:

$result = $base_list->diff(...$excluded_list);

@chedkowski-leonsoftware
Copy link
Author

chedkowski-leonsoftware commented May 9, 2024

In my case, i start with a list of integers that are mapped to value objects:

$excluded_list = Collection::fromIterable([2])
	->distinct()
	->map(fn ($v) => new ValueObject($v));

later comes the filtering logic.

Currently, I've managed to "bypass" the lazy mechanism by creating a new collection

Collection::fromIterable($excluded_list->all());

after using distinct, this helps in my case.

Thanks for the quick response!

EDIT: Sorry, I accidentally closed this issue

Copy link

Since this issue has not had any activity within the last 5 days, I have marked it as stale.
I will close it if no further activity occurs within the next 5 days.

@github-actions github-actions bot added the stale label May 15, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants