-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
quicksort #209
quicksort #209
Conversation
Does this assume that strings can be compared with the |
I believe it's comparing the bytes within the string with |
ah. then is that test assuming that a string literal makes a writable buffer of bytes? i believe string literals are supposed to be read only. there should actually by a type check error when using a string literal as a |
@thejoshwolfe here is a version that has |
@stereosteve cool. but why does that code have two sort functions? why not use the more general-purpose pub fn operatorCompare(inline T: type, a: T, b: T) -> Cmp {
return if (a > b) Cmp.Greater else if (a < b) Cmp.Less else Cmp.Equal;
} And it seems like |
@thejoshwolfe yeah I was trying to do something like that, but couldn't figure out how to :(. Still a zig noob. Like I changed
and called it like:
and compiler says:
I tried a few other things as well, but to no avail... Are there anonymous functions in zig? Or is there a way to partially apply the Type parameter of Perhaps the answer is to just require that the user alway provide a compare function. |
Sounds like you have run into a bug in the compiler. I'll have a look.
Currently, no.
Does this count? fn operatorCompare_i32(a: i32, b: i32) -> Cmp {
operatorCompare(i32, a, b)
} One thing that may work, if the prototype of the sort function is: pub fn quicksort(inline T: type, array: []T, inline cmp: fn(a: T, b: T)->Cmp) {
// ...
} So use the T from the outer generic param instead of the comparator having its own generic type. |
Provide some handy functions for builtin comparable types.
Cool that all makes sense. Okay, I updated
Let me know if that's a good approach and if you like the naming. If so I'll add helper functions for the rest of the types. |
I'd like to know what is the priority of #130, because it will dictate the standard library implementation such as this. |
@fsaintjacques i don't think we need to finalize this api just yet. as cool as it would be to implement a standard sort api that used some kind of comparable trait to decide how to compare elements, function pointers work just fine and are even slightly more powerful, at least as far as i understand the trait idea. i think this PR should move forward with function pointers for now. when #130 happens we can change the standard library to use it. |
No description provided.