Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String functions work poorly with postfix apply #628

Closed
Goju-Ryu opened this issue Oct 19, 2024 · 2 comments · Fixed by #646
Closed

String functions work poorly with postfix apply #628

Goju-Ryu opened this issue Oct 19, 2024 · 2 comments · Fixed by #646
Labels

Comments

@Goju-Ryu
Copy link
Contributor

Goju-Ryu commented Oct 19, 2024

I'm currently developing functions that make heavy use of strings and the core::strings module. In this process I have been unable to use the postfix apply operator almost at all due to argument order. I would love if either the order got changed to make the string worked on the last argument or to make a set of postfix apply friendly versions.
I believe it would make string heavy code more easily readable and easier to refactor. As an example here is the current version of str_replace and a version using pipes on the assumption that the first argument of all string functions were moved to the end of the argument list:

# Current implementation
fn str_replace(s: String, pattern: String, replacement: String) -> String =
  if pattern == ""
    then s
    else if str_contains(s, pattern)
           then if str_slice(s, 0, str_length(pattern)) == pattern
               then str_append(replacement, str_replace(str_slice(s, str_length(pattern), str_length(s)), pattern, replacement))
               else str_append(str_slice(s, 0, 1), str_replace(str_slice(s, 1, str_length(s)), pattern, replacement))
           else s

# same implementation but with arguments moved and using pipes (also used a hypothetical prepend function instead of append)
fn str_replace(pattern: String, replacement: String, s: String) -> String =
  if pattern == ""
    then s
    else if str_contains(s, pattern)
           then if str_slice(s, 0, str_length(pattern)) == pattern
               then (s |> str_slice(str_length(pattern), str_length(s)) |> str_replace(pattern, replacement) |> str_prepend(replacement))
               else (s |> str_slice(                  1, str_length(s)) |> str_replace(pattern, replacement) |> str_prepend(str_slice(s, 0, 1)))
           else s
@sharkdp
Copy link
Owner

sharkdp commented Oct 23, 2024

Feel free to propose changes to the String function API! I wouldn't mind breaking changes if we make it easier to work with them.

@Goju-Ryu
Copy link
Contributor Author

Feel free to propose changes to the String function API! I wouldn't mind breaking changes if we make it easier to work with them.

Great! The I will open a PR for it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants