Proposal: Rename String.duplicate/2

39 views
Skip to first unread message

Timotej Fartek

unread,
Jan 18, 2018, 4:59:32 AM1/18/18
to elixir-lang-core
Hello! First of all, good job on Elixir. It really is a joy to build applications with it.
Now about my proposal. I think that the function "duplicate/2" in the module String can be a bit confusing with the current naming. When I read "duplicate", the thing that immediately pops into my mind is "make a copy of", "multiply by two" etc. so when I'm working with String.duplicate I'm always a bit confused as to what is going to happen with my string.
If I call String.duplicate("a", 2) to me it looks like the end result is going to be "aaaa", because we take "a", we duplicate it (so it's "aa") and then we duplicate that again (because n=2), and return "aaaa". Of course this assumption is wrong as the end result is "aa".
What String.duplicate actually does is multiply the string n times. Python (the language) has this simplified to the point where you actually write "a" * 2, to get the result "aa".
I suggest we do something similar with Elixir and come up with a better name for the duplicate function. I propose String.repeat or String.multiply but I am also open to other ideas if you think this might cause extra confusion. The most technically correct version of the name in my opinion would be String.multiply, because even with String.repeat there could be some confusion: does String.repeat("a", 1) return "a" or "aa"?
I would like to make a pull request with the new function but first I want to hear what the community thinks about this idea.
Another version of the name could be "times". String.times("a", 1) would in my opinion clearly return "a". String.times("a", 3) would clearly return "aaa". 

Anyway this post is long enough as is 😅.
Thanks again for the wonderful work so far!

OvermindDL1

unread,
Jan 18, 2018, 10:11:16 AM1/18/18
to elixir-lang-core
I'd probably have chosen String.repeat, however String.duplicate makes sense to me grammatically as well as duplicating tends to be used 'as' multiplication in common speech (around where I am anyway, central USA).

Timotej Fartek

unread,
Jan 18, 2018, 10:15:37 AM1/18/18
to elixir-lang-core
Maybe it confuses me because I'm not a native speaker. Would you suggest we leave it alone (for now)?

OvermindDL1

unread,
Jan 18, 2018, 11:07:19 AM1/18/18
to elixir-lang-core
I'd personally leave it alone, there are other programming languages that use the same terminology as well I know I've run across.  :-)

Plus I really don't think its worth breaking compat even with a deprecation.  ^.^;

Peter Hamilton

unread,
Jan 18, 2018, 11:52:02 PM1/18/18
to elixir-l...@googlegroups.com

Second native speaker here weighing in that it's not confusing. Let's leave it as is.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/c207a461-31b7-4f16-9ce4-d28b9226e0ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages