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

selectIndices #107

Open
sullyj3 opened this issue Sep 8, 2023 · 2 comments
Open

selectIndices #107

sullyj3 opened this issue Sep 8, 2023 · 2 comments

Comments

@sullyj3
Copy link
Contributor

sullyj3 commented Sep 8, 2023

I find myself constantly re-implementing this function:

-- | given a list of indices and a list, return the elements at those indices
-- Assumes indices are sorted. If indices that are out of bounds are given,
-- they are silently ignored.
selectIndices :: [Int] -> [a] -> [a]
selectIndices indices list = go indices list 0
  where
    go [] _ _ = []
    go _ [] _ = []
    go (index:indices) (x:xs) currentIndex
      | index == currentIndex = x : go indices xs (currentIndex + 1)
      | otherwise = go (index:indices) xs (currentIndex + 1)

Would you accept a PR for this?

@ndmitchell
Copy link
Owner

I really worry about the requirement for indices to be sorted - if you don't have that, it can't operate lazily, if you do have that, there is an easy to make mistake that is really hard to find. I don't really know a way to fix this API.

@sullyj3
Copy link
Contributor Author

sullyj3 commented Jan 16, 2024

fair point

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