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
Negative indexes #42
Comments
In Elixir enums suport negative indexes: https://hexdocs.pm/elixir/Enum.html#at/3
|
With Elixir strings you can do: iex(1)> "snakes on a plane!" |> String.slice(-6..-1)
"plane!" In Ruby and Elixir you have range types where you have -1 representing the last element. ( |
I'm starting to think that Elixir's variant that specifies the length is the least confusing (i.e.: iex(1)> "freeby" |> String.slice(-2, 2)
"by"
iex(2)> "snakes on a plane!" |> String.slice(-6, 6)
"plane!"
iex(3)> "snakes on a plane!" |> String.slice(-6, 5)
"plane" |
Thanks for doing this research. I think that last example is clear So would that be |
👍
In the // take(from list: List(a), up_to n: Int) -> List(a)
take(from: task_list, up_to: 4)
// Or:
task_list |> take(up_to: 4) For invoice_label |> slice(at_index: 6, length: 8)
// Or:
slice(from: invoice_label, at_index: 6, length: 8) Then it's unambiguous and the What do you think? |
Looks good. What about length -> up_to? As you may not always get that many graphemes |
I agree, that would also make it more consistent with the invoice_label |> slice(at_index: 6, up_to: 8) I do think some people could interpret it as in index and that |
Good point, let's go for length then. |
In the (TODO) doc comments of
string.slice
( https://github.com/gleam-lang/stdlib/blob/master/src/gleam/string.gleam#L135 ), you see this example:Which surprises me because as a user of the
slice
function I would expect the function to be able to slice to the end of a string without having to know the length of that string beforehand.So personally I expect to be able to do this:
This is also how negative indexes work in Ruby for example (I'll have to check other languages though):
Thoughts?
The text was updated successfully, but these errors were encountered: