|
gonadic io posted:
so does string in python have a default sort key for that to work?
|
# ? Jan 18, 2015 18:56 |
|
|
# ? Jun 12, 2024 15:02 |
|
Shaggar posted:so does string in python have a default sort key for that to work? yes
|
# ? Jan 18, 2015 18:57 |
|
Shaggar posted:so does string in python have a default sort key for that to work? i don't care, i hope to never program in python again
|
# ? Jan 18, 2015 18:58 |
|
ps https://docs.python.org/3/library/functools.html#functools.cmp_to_key
|
# ? Jan 18, 2015 18:58 |
|
gonadic io posted:i don't care, i hope to never program in python again although i wouldn't immediately kill myself, unlike with fortran or matlab
|
# ? Jan 18, 2015 18:58 |
|
please don't mention the m-word
|
# ? Jan 18, 2015 18:59 |
|
lol that this exists.
|
# ? Jan 18, 2015 19:00 |
|
why is that function defined like this: Python code:
how does it get a key from this?
|
# ? Jan 18, 2015 19:09 |
|
Shaggar posted:seems like its overloading a bunch of comparison operators? how is that different from just using compareTo? Very carefully, Shaggar! Haha
|
# ? Jan 18, 2015 19:11 |
|
also does that class K get created and destroyed every time the cmp_to_key function is invoked?
|
# ? Jan 18, 2015 19:15 |
|
Brain Candy posted:please don't mention the m-word It is triggering
|
# ? Jan 18, 2015 19:22 |
|
shaggar have you ever stopped to think about literally any new concept for even a couple of seconds before feigning bemusement and asking a volley of backfiring loaded questions that serve only to reveal how little you really understand about anything
|
# ? Jan 18, 2015 19:28 |
|
sort keys were claimed to not use comparisons and that doesnt seem to be true based on the python documentation so I am asking for clarification. I don't use python so I really don't know which is correct
|
# ? Jan 18, 2015 19:31 |
|
Shaggar posted:yes this uses compareto/compare which isn't part of python no it doesn't, it uses the default comparer. the python version doesn't not use comparison, it just no longer allows you to specify a custom comparison.
|
# ? Jan 18, 2015 19:34 |
|
EVGA Longoria posted:python ain't down with timtowdi? but how will i know how whimsical i was feeling the day i wrote it if i can't tell whether I dereferenced a hash with -> or {}???? it's a nice rule of thumb but it's not a great commandment to obey no matter what extremes dude
|
# ? Jan 18, 2015 19:34 |
|
gonadic io posted:although i wouldn't immediately kill myself, unlike with fortran or matlab fortran is fine
|
# ? Jan 18, 2015 19:35 |
|
cmp_to_key is using a design pattern.
|
# ? Jan 18, 2015 19:35 |
|
GrumpyDoctor posted:no it doesn't, it uses the default comparer. the python version doesn't not use comparison, it just no longer allows you to specify a custom comparison. the default comparer in c# uses the default comparable for the objects being compared. also, like most comparison operations, orderby allows you to additionally specify a custom comparer. either way the claimed reason for eliminating compare from python was that it was faster which is clearly not the case since its doing the same thing, only in a more limiting and confusing way. it appears that the actual reason was because someone wanted everyone to use operator overloads for comparison which is about the worst thing I can think of.
|
# ? Jan 18, 2015 19:40 |
|
Shaggar posted:seems like its overloading a bunch of comparison operators? how is that different from just using compareTo? the key is the K it returns Yo look it at this Java code:
or i could make another list Java code:
|
# ? Jan 18, 2015 19:43 |
|
but the K isn't a value you could compare. its an object containing overloads that reference the same expensive comparison.
|
# ? Jan 18, 2015 19:58 |
|
I guess maybe I don't understand why a compareTo which first compares lastname, and then compares firstname is more expensive than a sort that compares on the lastname key first and the firstname key second.
|
# ? Jan 18, 2015 20:06 |
|
uh, because it isn't. it's only better if you are doing an expensive thing cmp_to_key is a way of back porting comparators to the new code. of course it isn't going to be better than doing it the old way since it's still doing it the old way with a goofy wrapper
|
# ? Jan 18, 2015 20:31 |
|
it makes it more convenient to write a custom comparator in the common case where the custom comparator is just comparing one or more properties. it also enables a cute library hack where sort maps the whole list to its keys once and then sorts the two lists in parallel, which is a major optimization if projecting keys is expensive but it's significantly less general, and the cute hack means that sort will frequently take waaaay more memory than it otherwise would, even if your keys are trivial to derive
|
# ? Jan 18, 2015 20:59 |
|
please don't use or talk about backward compatability hacks also the 2 are practically the same for 99.999% of use cases, please don't trigger
|
# ? Jan 18, 2015 22:10 |
|
To see why it might be desirable, imagine you want to sort a list by the bcrypt12 of each item in the list. If you use a comparing function that hashes each item, you're gonna have to calculate two hashes per comparison (unless you memoize, which costs memory obviously), and if you use the function you're gonna have precisely one hashing per element of the list. It's somewhat comparable to sorting [(hash(x), x) for x in list] except you also have to make a pass to remove the first element of the tuple.
|
# ? Jan 18, 2015 22:24 |
|
yes, but who the gently caress wants to sort by the bcrypt of their data that's not just a theoretical question, there's a real trade off here, and Python chose to prioritize the performance of a tiny corner case (super-expensive sort keys) over the performance of the common case (easily derived sort keys)
|
# ? Jan 18, 2015 23:00 |
|
rjmccall posted:yes, but who the gently caress wants to sort by the bcrypt of their data bcrypt is the absurd example. You could want to sort a bunch of items by the average values of whatever subcomponents they have and then it gets more expensive, or you may just want to sort them by a precomputed value. Both cases have their place as far as I can see.
|
# ? Jan 18, 2015 23:07 |
|
If only there were some way to choose to sort using either a key function or a comparator function depending on your needs.
|
# ? Jan 18, 2015 23:09 |
|
MononcQc posted:Both cases have their place as far as I can see. great, so the way we make design decisions here in pythonland is we try really hard to imagine worst possible cases that would justify doing something expensive instead of trying to make a balanced decision about what's likely to happen and how reasonable programmers could work around this expected behavior
|
# ? Jan 18, 2015 23:21 |
|
rjmccall posted:great, so the way we make design decisions here in pythonland is we try really hard to imagine worst possible cases that would justify doing something expensive instead of trying to make a balanced decision about what's likely to happen and how reasonable programmers could work around this expected behavior
|
# ? Jan 18, 2015 23:23 |
|
thank goodness this put to rest all their complaints about performance, then
|
# ? Jan 18, 2015 23:39 |
|
rjmccall posted:yes, but who the gently caress wants to sort by the bcrypt of their data i use key more than i ever used cmp. most of the python types already have comparisons, and for user types, implementing <, >, >=, <= is usually enough. i do a lot of sorting by property, but not as much as sorting in default order, and very rarely sorting with a custom ordering
|
# ? Jan 19, 2015 00:42 |
|
tef posted:i use key more than i ever used cmp. most of the python types already have comparisons, and for user types, implementing <, >, >=, <= is usually enough. there are three separable questions here should python make it convenient to sort by a projection? sure, that's an important case to make convenient should python have made this promise about the projection only being called once? depends on how you answer the tradeoff should python have removed the ability to conveniently sort by a custom comparator? because that seems completely unnecessary
|
# ? Jan 19, 2015 00:57 |
|
how do you sort in reverse on one of the keys in that model?
|
# ? Jan 19, 2015 02:54 |
|
tef posted:i do a lot of sorting by property, but not as much as sorting in default order, and very rarely sorting with a custom ordering i constantly find myself sorting by unusual orderings i assume the reason you do not is that your favorite language requires you to implement a four method interface for each custom ordering you might desire sapir whorf for compilers
|
# ? Jan 19, 2015 03:38 |
if I were concerned about performance why would I be using python?
|
|
# ? Jan 19, 2015 03:57 |
|
Notorious b.s.d. posted:i constantly find myself sorting by unusual orderings it was dropped in python 3 a language i have not yet been paid to write in
|
# ? Jan 19, 2015 04:02 |
|
Subjunctive posted:how do you sort in reverse on one of the keys in that model? .sort(reversed=True)
|
# ? Jan 19, 2015 04:02 |
|
i am really curious what cmp stuff people are doing because i really don't ever write them since i stopped doing java
|
# ? Jan 19, 2015 04:04 |
|
|
# ? Jun 12, 2024 15:02 |
|
rjmccall posted:there are three separable questions here as mentioned, you can still lift a cmp into a key, using a built in function, if you must. otoh if you only have one then you can just implement >= methods on the class and just use sort()
|
# ? Jan 19, 2015 04:08 |