Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate function #244

Open
gbagan opened this issue Oct 31, 2023 · 2 comments
Open

generate function #244

gbagan opened this issue Oct 31, 2023 · 2 comments

Comments

@gbagan
Copy link
Contributor

gbagan commented Oct 31, 2023

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
Copy link
Member

garyb commented Oct 31, 2023

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.

@gbagan
Copy link
Contributor Author

gbagan commented Nov 5, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants