Skip to content

added shuffle to list #2066

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

Merged
merged 1 commit into from
Mar 15, 2025
Merged

Conversation

mazerty
Copy link
Contributor

@mazerty mazerty commented Mar 14, 2025

As seen together, here's the shuffle function for the List :)

Sorry for the "mess" with the two new packages for the random number generator, it was the only way I could find to import it in the test 😬

Also I tried to add a test in the immutable-flow.js file, but I don't really understand how it works 😅

@bdurrer
Copy link
Contributor

bdurrer commented Mar 14, 2025

Math.random is probably faster, but wouldn't Crypto.getRandomValues() be the better choice?

while (current) {
destination = Math.floor(random() * current--);

tmp = mutable.get(destination);
Copy link
Contributor

@bdurrer bdurrer Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it need to check for no-op changes, where destination == source?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh i just took the fisher-yates algorithm as it is without tinkering with it
imho the "source = destination" case would likely happen with small lists, in which case performance is not really an issue
in huge lists, the probability is much smaller, therefore i think the "cost" of the "if equals" would be greater than the potential gain from it, wdyt ?

i admit that i didn't run any benchmark for this 😅 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mainly thinking of how good the shuffle is, if it could end up not doing anything. But I am also not sure if I have a logical error in my reasoning of how the algorithm works 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdurrer you can read about the FIsher-Yates algorithm here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

I tried to shuffle 100000 times an array and the repartition seems pretty good.

@mazerty
Copy link
Contributor Author

mazerty commented Mar 14, 2025

Math.random is probably faster, but wouldn't Crypto.getRandomValues() be the better choice?

hmm not sure we need cryptographically strong random values here 🤔 i would go for speed here no ?

@jdeniau
Copy link
Member

jdeniau commented Mar 14, 2025

Don't bother testing flow.
I do not use it either, and honestly : who is?

I'm 🤏 to drop support.

@jdeniau jdeniau merged commit 98c5923 into immutable-js:main Mar 15, 2025
5 checks passed
@jdeniau
Copy link
Member

jdeniau commented Mar 15, 2025

Thanks @mazerty . I will release that in the next minor.
I just want to change some TS typing before (on another thing)

@mazerty mazerty deleted the added-shuffle-to-list branch March 16, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants