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

Fix String.split/3 example in guide and add note in docs #14223

Merged
merged 5 commits into from
Jan 24, 2025

Conversation

sabiwara
Copy link
Contributor

Close #14222

@sabiwara
Copy link
Contributor Author

Will backport this after merge

@@ -33,14 +33,14 @@ We can also use options to limit the splitting algorithm to a maximum number of

```elixir
iex> String.split("1 2 3", " ", [trim: true, parts: 2])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should pick another example now? Although the only other option that comes to mind is inspect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to invert the examples: first we show the parts option and then we show the trimming one. It may be more logical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about to_timeout?
it's in Kernel, and very easy to understand

to_timeout(hour: 1, minute: 30)
5400000

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh but people who installed an old version might not have it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this example from inspect examples is nice

inspect([1, 2, 3], pretty: true, width: 0)
"[1,\n 2,\n 3]"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand it requires additional context on where timeouts are used? There is always Date.shift though.

If we assume people have a recent enough version of Elixir, I think the context can be pretty minimal and the examples natural enough. We could say something like

Various functions in Elixir or Erlang libraries accept a timeout in milliseconds. For these cases, we can use the to_timeout helper:

iex> to_timeout([minute: 1])
60000

iex> to_timeout([minute: 1, second: 30])
90000

iex> to_timeout(minute: 1, second: 30)
90000

Copy link
Contributor Author

@sabiwara sabiwara Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another example using just primitive types (I don't love it):

iex(1)> Base.encode32("foobar")
"MZXW6YTBOI======"
iex(2)> Base.encode32("foobar", case: :lower)
"mzxw6ytboi======"
iex(3)> Base.encode32("foobar", case: :lower, padding: false)
"mzxw6ytboi"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s try keeping as is but different order. Do you want me to push something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that doesn’t work, I think Base can be a good second option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s try keeping as is but different order.

Sorry I didn't get it 😅

Do you want me to push something?

Yes please 🙏

lib/elixir/lib/string.ex Outdated Show resolved Hide resolved
@sabiwara sabiwara merged commit adcb7c6 into elixir-lang:main Jan 24, 2025
10 checks passed
@sabiwara sabiwara deleted the fix-string-split-docs branch January 24, 2025 22:11
@sabiwara
Copy link
Contributor Author

Backported

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

Successfully merging this pull request may close these issues.

The Document about "Keyword lists and maps" maybe has errors.
2 participants