purescript/purescript-arrays

generate function

gbagan opened this issue · 2 comments

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.

garyb commented

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 the Data.Vector module of Haskell.
  • 0 .. (n-1) <#> f(instead of generate n f) requires handling the case where n <= 0 (in particular when n = 0)