-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
deprecate count_ones and count_zeros to countones and countzeros #23531
Conversation
FWIW |
Agreed. It is a quite generic name for an uncommon low level operation. It is also (AFAIK) unlikely to be overloaded a lot so it could in my opinion use a longer descriptive name. |
Agreed. Suggestions? :) |
Hamming distance? :-) More seriously, maybe |
Regarding changes beyond underscore removal, perhaps consistency with The alternatives that occur to me immediately seem a bit unwieldy ( |
We could do something like bitcount(::typeof(isone), n::Integer) = # count_ones(n), likewise for iszero -> count_zeros We could also define I think |
How do you envision extending that The more I mull it over, the more I appreciate the simplicity and consistency of the status quo ( |
If we make |
Since |
I thought the same (I have a module to do that kind of things), but is there other uses in base for this? otherwise, it may be too complicated for Base (also, close to that is: The flipside is, why not have |
As long as numbers are iterable, I don't think we change the meaning of Some data points:
The following languages don't have any such functions built in: Python, Ruby, Perl, Matlab, R I could have done more but I got bored of Googling. My takeaway from this is that most languages don't seem to provide functionality for counting zeros, leading ones, or trailing ones. However, Rust does, and all of our functions are named identically to theirs. I typically take Rust's choices more seriously than other languages, as it's about as new as Julia so they're also trailblazing in some sense. I'm still not sure what the best choice is, but at least we have a bit of perspective from other languages. |
Nice data! :D Of all those names, Rust/Julia's strike me as far and away the best. I lean progressively more heavily to the status quo less underscores. Thanks for compiling that list! :) |
We could go with My only concern with |
Yes, I usually like following Rust's choices, but in that case |
Could |
|
I find So while I do see the potential for some confusion, I'm not sure the alternatives are less confusing/ambiguous. |
According to Wikipedia, the POSIX libc terminology speaks of "set bits". But even with that name, It could make sense to handle the diversity of cases via a second argument. We could have @inline function countbits(x, kind=:all)
if kind === :all
count_ones(x)
elseif kind === :leading
leading_ones(x)
elseif kind === :trailing
trailing_ones(x)
end
end
f(x) = countbits(x, :trailing)
@code_native f(1)
|
At this point I favor the minimal evolution of the status quo necessary to address the comments that sparked this discussion (
), particularly |
As much as I dislike extended bikeshedding sessions, I must say that IMHO |
Any ideas about how to get rid of the performance cost of throwing an error when Though I'm not opposed to |
Here's a similar possibility:
|
A mock implementation of Jeff's suggestion seems fine. Am I missing something? Thanks! julia> begin
function countbits(x, kind)
if kind === 0
count_zeros(x)
elseif kind === 1
count_ones(x)
else
error("cats!")
end
end
f0(x) = countbits(x, 0)
f1(x) = countbits(x, 1)
g0(x) = count_zeros(x)
g1(x) = count_ones(x)
end
g1 (generic function with 1 method)
julia> @code_llvm f0(1)
; Function f0
; Location: REPL[1]
define i64 @julia_f0_61723(i64) #0 {
top:
; Location: REPL[1]:12
; Function countbits; {
; Location: REPL[1]:4
%1 = xor i64 %0, -1
%2 = call i64 @llvm.ctpop.i64(i64 %1)
;}
ret i64 %2
}
julia> @code_llvm g0(1)
; Function g0
; Location: REPL[1]
define i64 @julia_g0_61738(i64) #0 {
top:
; Location: REPL[1]:14
%1 = xor i64 %0, -1
%2 = call i64 @llvm.ctpop.i64(i64 %1)
ret i64 %2
} |
@Sacha0 You're right, I tried reproducing the problem with the generated code and I couldn't, even with my example from above. Not sure what I did wrong the first time. So we can choose whatever API looks the best. |
From triage: the current names aren't so bad and we should probably just keep them. Plus this is a pure renaming issue; it doesn't really affect broader API or design issues. The fact that Rust has adopted these names is a further reason to leave them as-is. |
So even underscores in function names are considered OK? |
I think the consensus was that using underscores to separate words is not a big problem; the desire to avoid them is mostly to push toward simplifying function names to single words whenever possible. |
Surely we have bigger problems than the occasional presence of an underscore. |
This pull request deprecates
count_ones
andcount_zeros
tocountones
andcountzeros
as part of the API consistency review's underscore audit. Best!