purescript-arrays icon indicating copy to clipboard operation
purescript-arrays copied to clipboard

generate function

Open gbagan opened this issue 1 year ago • 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.

gbagan avatar Oct 31 '23 11:10 gbagan

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.

garyb avatar Oct 31 '23 17:10 garyb

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)

gbagan avatar Nov 05 '23 17:11 gbagan