Skip to content

Proposal: add replace function for String #3760

@l-monnier

Description

@l-monnier
  • I have read CONTRIBUTING.md.
  • I have checked that there is no existing PR/issue about my proposal.

Summary

Add a replace function for String to base similar to the one for Text in Haskell (see function description on Hackage).

replace : (needle : String) -> (replacement : String) -> (haystack : String) -> String 

Motivation

This proposal comes from a discussion which occurred on Zulip.

The proposal

As suggested by @mattpolzin there should be a discussion on how this function should be implemented:

a) using FFIs (for efficiency); or
b) using pack and unpack for simplicity.

At the time being, I have made my own implementation which can be found on Codeberg.

It relies on a replace function for List which definition can be found in this module of the same Codeberg repo.

Of course, there are other ways to do it (more efficient ones/which do not rely on assert_total).
My goal was to come with a simple implementation relying on existing functions.

Alternatives considered

Current situation with my own library

My implementation fits my needs, but the day I want to publish on pack libraries relying on it, I would need to publish this library as well, which only contains a couple of functions.

Internal module

I could have this replace function as an internal module of my libraries, but this is such a common need that I would probably have to implement it multiple times, which is not really ideal in terms of maintenance (considering others may do exactly the same!).

Add it to contrib

This was my first thought, but given the state of affairs, this is not really a solution.
Or it would imply to move first Data.String.Extra and Data.List.Extra to their own package at https://github.com/idris-community as per RFC #2866.

Conclusion

I'm just seeking for a way forward for a better solution than "roll my own package" for such cases.

I also see it as an opportunity to update CONTRIBUTING.md as Idris2 has now a de-facto package manager with pack and things seem to be moving forward with RFC #2866 mentioned above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions