solidity-generators icon indicating copy to clipboard operation
solidity-generators copied to clipboard

Feature request: something equivalent to "Array.prototype.fill" from ES6

Open PaulRBerg opened this issue 3 years ago • 1 comments

See Array.prototype.fill. When used like this in JavaScript:

Array(5).fill(2)

It will yield the following output:

[2, 2, 2, 2, 2]

It seems to me that solidity-generators would be the right place to implement such a functionality in Solidity (better than forge-std).

The way I see it, there are a couple ways this could be implemented:

  1. Add a new function fill that does only this.
  2. Modify the behavior of linspace to continue filling the output with duplicated values of the second argument (ustop).

PaulRBerg avatar Dec 26 '22 15:12 PaulRBerg

Agreed, this does seem like the right library for this method.

The existing function names/signatures are modeled after numpy's method's of the same name, so perhaps we base this one on the full method:

>>> np.full(5, 2)
array([2, 2, 2, 2, 2])
function full(uint length, uint val) pure returns (uint[] memory arr) {
  // --- snip ---
}

I won't be able to implement this until after the holidays, but PRs definitely welcome if you want it sooner

mds1 avatar Dec 27 '22 15:12 mds1