Closed LightAndLight closed 1 year ago
The symmetry is already preserved when we have string.split sep "" == [""]
, because string.join sep [""] == ""
.
While trying to "fix" this, I realised that the way Rust's way of doing it (which we reuse in the interpreter) is already correct. The extra empty strings that string.split
generates are important for string.join
to be the inverse of string.split
.
I still think that string.splitc sep "" == [""]
is an easy way to make a mistake - the motivating usage still stands. I can't think of anything better right now.
I considered
splitc : Char -> String -> { prefixes : Array String, suffix : String }
and
splitc : Char -> String -> (| Match : Array String, NoMatch : String |)
but I think it'd be too inconvenient.
For the original use in https://github.com/LightAndLight/ipso/issues/271, I suggest the following:
bind paths <-
let paths = string.splitc ' ' <| string.stripc ' ' outPaths
if paths == [""]
then
comp
println "OUT_PATHS contains no paths"
exit.failure
else io.pure paths
cmd.run `/nix/var/nix/profiles/default/bin/nix copy --to $binaryCacheUrl $paths`
\x -> string.splitc ' ' <| string.stripc ' ' x
gestures at a potential set of functions:
partsc (delimiter : Char) (value : String) : Array String =
let stripped = stripc c value in
if stripped == ""
then []
else splitp (\c -> c == delimiter) stripped
parts (delimiter : String) (value : String) : Array String =
...
partsp (delimiter : Char -> Bool) (value : String) : Array String =
...
The split
family of functions focuses on the "negative space" of the input value (the delimiter) - it removes the delimiter and nothing else, which is why string.join
is its inverse. The parts
family focuses on the "positive space" of the input value - the pieces that are left after removing the delimiter. parts
is like a more general version of Haskell's words
.
The original example could be written using partsc
without any error checking:
cmd.run `command ${string.partsc ' ' outPaths}`
Closing in favour of https://github.com/LightAndLight/ipso/issues/280
Motivating usage (found during https://github.com/LightAndLight/ipso/issues/271):
If
words == ""
then this will callcommand
with a single argument - the empty string - becausestring.splitc ' ' "" == [""]
.It also increases the symmetry with
string.join
: