Address an elemet from a list from Data.List.Linear
Nobody has claimed this yet.
- Dominant language
- Haskell
- Stars
- 359
- Forks
- 45
- PR merge metrics
- No merged PRs in 30d
Description
Is your feature request related to a problem? Please describe.
Data.List.Linear doesn't seem to provide a simple function akin to (!!) for the non-linear List. What would be the recommended way of doing it?
Describe the solution you'd like
This is a prototypical linearListElemAtM and linearListElemAt that allow one to address the element by providing a (monadic in the M-variant) function that duplicates the element.
linearListElemAtM :: forall a m.
Control.Functor.Linear.Monad m =>
(a ⊸ m (a, a)) -> [a] ⊸ Int -> m (Maybe a, [a])
linearListElemAtM dupM xs idx =
let !(ys, zzs) = L.splitAt idx xs
in case L.uncons zzs of
Just (a, zs) -> dupM a Control.Functor.Linear.>>= \(a1, a2) ->
Control.Functor.Linear.pure (Just a1, ys L.++ a2 : zs)
Nothing -> Control.Functor.Linear.pure (Nothing, ys)
linearListElemAt :: forall a.
(a ⊸ (a, a)) -> [a] ⊸ Int -> (Maybe a, [a])
linearListElemAt dupF xs idx = L.runIdentity (linearListElemAtM (\x -> L.Identity (dupF x)) xs idx)
Describe alternatives you've considered
I have not found what's the "recommended" way for doing it.
Additional context
This prototype does assume Dupable instance for a, for Dupable instance a specialized function may be provide, instead.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the Data.List.Linear API and its existing splitAt, uncons, and append operations. Compare the proposed linearListElemAtM and linearListElemAt signatures with the module's conventions, including how duplication is represented. Done means resolving the Dupable question and establishing the intended behavior for valid and out-of-range indices.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100