ChrisCavs / aimless.js

The missing JavaScript randomness library.
https://chriscavs.github.io/aimless-demo/
MIT License
824 stars 13 forks source link

Refactor uniqFuncSequence to prevent mutation of input array #1 #11

Closed ghsamm closed 1 year ago

ghsamm commented 1 year ago

Summary

In this PR, I refactored uniqFuncSequence to avoid mutation of the input array. Instead of manipulating the original array, the function now creates a copy using Array.prototype.slice and performs operations on it.

Motivation

Mutation of input can lead to unexpected side effects, especially when the function is used in different contexts or when the input is reused later in the code. This change is a step towards enhancing the predictability and purity of functions across our codebase.

Discussion

While this PR focuses on uniqFuncSequence, I think it's worth discussing the potential benefits of avoiding mutation throughout the library. This approach could improve readability, predictability, and better adhere to functional programming principles.

ChrisCavs commented 1 year ago

if you look carefully, uniqFuncSequence does not actually mutate the input array. there are no mutation events, since sliceOut returns a shallow copy of two portions of the array and ties them together into a new one.

I don't believe there is mutation anywhere else in the repo, but feel free to check 👍