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

Add String::takeLeft and String::takeRight #3159

Closed
pbiggar opened this issue Aug 10, 2021 · 7 comments
Closed

Add String::takeLeft and String::takeRight #3159

pbiggar opened this issue Aug 10, 2021 · 7 comments

Comments

@pbiggar
Copy link
Member

pbiggar commented Aug 10, 2021

Check naming matches list functions

@nicu-chiciuc
Copy link
Contributor

I assume we need a String::take instead of takeLeft/takeRight since that's the case for List.

@pbiggar
Copy link
Member Author

pbiggar commented Mar 10, 2023

They're not exact analogies. With Lists, it's implicit that you're taking from the front. Strings don't have a "front" because that depends on whether you read RTL (right-to-left) or LTR (with some languages being RTL), which is why we suggested takeLeft and takeRight for strings.

For Lists, it wouldn't be terrible to rename to "takeFront" and "takeBack" I think.

@nicu-chiciuc
Copy link
Contributor

nicu-chiciuc commented Mar 10, 2023

It took me quite a long time to realize this, but there's already a String::first and String::last.
https://github.com/darklang/dark/blob/main/backend/src/LibExecutionStdLib/LibString.fs#L582

Is this Issue still valid?

@pbiggar
Copy link
Member Author

pbiggar commented Mar 10, 2023

Apologies, this is harder than I expected.

Good point. This made me reread the original naming discussion (#2188) and I realized I misunderstood the left/right thing. Because left and right are rendering properties, we actually don't want to use "left" and "right" but we actually want to use "start/end" or "first/last" or "front/back" etc.

I think including "take" in the name is probably a good idea, and so is consistency.

So then the issue is that we sometimes use start/end, and sometimes first/last. Meanwhile lists use first/last, or nothing, or in one case "back" (List.pushBack).

I think we could probably use "start/end" consistently across both Lists and Strings? WDYT?

@nicu-chiciuc
Copy link
Contributor

First I tried getting some samples in different languages. The JS, F# and C# where manual, then I relied on gpt4, and checked afterward.

It seems that there isn't a good standard, even in the same language.

The only concern I have with start/end is how it would apply to languages written right-to-left (like Hebrew).

Language trimLeft trimRight takeLeft takeRight padLeft padRight takeFirstNArray takeLastNArray
Javascript trimStart() trimEnd() .substring(0, n) .substring(n) padStart(c, n) padEnd(c, n) arr.slice(0, n) arr.slice(-n)
C# TrimStart() TrimEnd() Substring(0, n) Substring(n) PadLeft(n, c) PadRight(n, c) arr.Take(n).ToArray() arr.Skip(arr.Length - n).Take(n).ToArray()
F# String.trimStart String.trimEnd take(n)/substring(0, n) take(-n)/substring(n) padLeft padRight List.take n arr List.takeLast n arr
Python str.lstrip() str.rstrip() str[:n] str[-n:] str.rjust(width, padding) str.ljust(width, padding) arr[:n] arr[-n:]
Go strings.TrimLeft(str, " ") strings.TrimRight(str, " ") str[:n] str[len(str)-n:] - - arr[:n] arr[len(arr)-n:]
Rust str.trim_start() str.trim_end() &str[..n] &str[str.len() - n..] - - &arr[..n] &arr[arr.len() - n..]
Haskell dropWhile isSpace str dropWhileEnd isSpace str take n str drop (length str - n) str - - take n arr drop (length arr - n) arr
Elm String.trimLeft str String.trimRight str String.left n str String.right n str - - List.take n arr List.drop (List.length arr - n) arr
OCaml Str.replace_first (Str.regexp "^\s+") "" str Str.replace_first (Str.regexp "\s+$") "" str String.sub str 0 n String.sub str (String.length str - n) n String.make width padding ^ str str ^ String.make width padding Array.sub arr 0 n Array.sub arr (Array.length arr - n) n
Java str.stripLeading() str.stripTrailing() str.substring(0, n) str.substring(str.length() - n) String.format("%" + width + "s", str).replace(' ', padding) String.format("%-" + width + "s", str).replace(' ', padding) Arrays.copyOfRange(arr, 0, n) Arrays.copyOfRange(arr, arr.length - n, arr.length)
Ruby str.lstrip str.rstrip str[0, n] str[-n, n] str.rjust(width, padding) str.ljust(width, padding) arr.first(n) arr.last(n)

@pbiggar
Copy link
Member Author

pbiggar commented Mar 20, 2023

Thanks for doing this! I guess it's trimStart and trimEnd then!

For RTL, it should work fine, as the RTL is just rendering, and "start" and "end" refer to the same place in both.

@StachuDotNet
Copy link
Member

Folding this issue into #5239, referenced there, to revisit later

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

No branches or pull requests

3 participants