Skip to content

selectIndices #107

@sullyj3

Description

@sullyj3

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions