purescript-arrays
purescript-arrays copied to clipboard
generate function
I think this function can be useful
generate :: Int -> (Int -> a) -> Array a
It builds an array of the given length by applying the function to each index.
I use it in many projects.
I can create a PR if necessary.
I think I just map
over a range
when I've needed something like this in the past - that's slightly less efficient than it could be, but it's pretty much just the cost of one array allocation.
I understand your point of view.
In addition to the overhead of map
over range
, I see the following advantages in favor of a generate
function.
-
generate
is present in theData.Vector
module of Haskell. -
0 .. (n-1) <#> f
(instead ofgenerate n f
) requires handling the case where n <= 0 (in particular when n = 0)