RFC: Add Array.mapA
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
I have noticed List has mapA but Array doesnt. Same in batteries - List has traverse but Array doesnt.
I propose adding the following implementation:
def mapA {m : Type u → Type v} [Applicative m] {α : Type w} {β : Type u} (f : α → m β) (as : Array α) : m (Array β) :=
as.foldl (fun macc x => (fun acc y => acc.push y) <$> macc <*> f x) (pure (Array.emptyWithCapacity as.size))
I suppose it was not added bc of performance worries. But maybe can use @[csimp] to optimize for known monads? Or is there other way?
- User Experience: This provides a standard way to map Applicative effects over Arrays, consistent with the existing
ListAPI. - Beneficiaries: Users working with
Arraywho currently have to convert toListand back to perform effectful operations. - Maintainability: This reduces boilerplate for users and aligns
Arraywith other traversable collections in the standard library.
Community Feedback
https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/RFC.3A.20Add.20Array.2EmapA
Impact
This would allow for more ergonomic and performant code when working with Array and Applicative functors. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.
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 linked List.mapA implementation in Init/Data/List/Control.lean and the Array/List traversal context in Batteries/Data/List/Basic.lean. Review the linked Zulip discussion and performance question before proposing changes. Done means the API and optimization approach are agreed and the resulting Array.mapA work is accepted.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100