Skip to content

Address an elemet from a list from Data.List.Linear #489

Description

@hellwolf

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions