sharkdp / numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
https://numbat.dev
Apache License 2.0
1.21k stars 51 forks source link

String functions work poorly with postfix apply #628

Open Goju-Ryu opened 4 days ago

Goju-Ryu commented 4 days ago

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 commented 12 hours ago

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 commented 11 hours ago

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.