--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/990cd9ac-fdb2-4d05-acac-128559523984%40disroot.org.
Hello guys,
I've just found unobvious thing about table.sort:
-- ! invalid order function for sorting
table.sort({ 1, 2, 3, 1 }, function(a, b) return (a <= b) end)
Lua manual says:
The comp function must define a consistent order; more formally, the
function must define a strict weak order. (A weak order is similar to
a total order, but it can equate different elements for comparison
purposes.)
and for me <= operator is weak order.
Practically it means that table.sort() never passes same item
to comparator and when writing comparisons explicitly,
default is "false":
local compare =
function(a, b)
if (a < b) then return true end
if (a > b) then return false end
return false
end
This error is not stable for my use case with comparing tables.
"return false" default eliminates it but sometimes "return true"
works too.
Bug?
-- Martin
Would it be possible to make table.sort stable via this trick?
local t = ...
local orig = {}
for i,v in ipairs(t) do orig[v] = i end
table.sort(t, function(a, b)
if a == b then return orig[a] < orig[b] end
return a < b
end)
assume t is a table of objects with valid __le and __eq meta fields.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CA%2BbJJbxnrtdZ5YYX%3DDOEk_NsXxCLeef7Car4S9MirEfzVx36bw%40mail.gmail.com.
--You received this message because you are subscribed to the Google Groups "lua-l" group. To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/lua-l/a832ad81-b9e5-47ac-af82-48c666412809%40disroot.org.
I've thought about current Lua's approach. Requiring strict ordering
(with inherited irreflection) makes some sense if we want to minimize
number of calls of comparison function.
But for me it's schizophrenic: trying to fit tertiary value into binary.
For me linear order comparison has three values: less, equal, greater.
You can't fit them into boolean and build sorting algorithm on that
without discovered side effects.
If someone wants to use boolean comparators - he still needs two of them.
For example is_less() and is_equal(). Of course I won't be happy
to provide two almost identical blocks of code for them.
For me requirement of comparison function to return one of three values
is more natural and acceptable than providing fragile sorting algorithm
for binary comparator.
You can't even easily invert sorting like:
-- Ascending sort
table.sort(Orders, function(a, b) return DateIsLess(a, b) end)
-- Descending sort? Nah! Wandering runtime error!
table.sort(Orders, function(a, b) return not DateIsLess(a, b) end)
That's right. Came to that after 40 seconds since I sent email.
But we can't say it's obvious, right? Besides, in real life there will
be several lines of code instead of "a, b".
Time spent in comparator is not limited. So sorting time may consist
mostly of time in that function. Imagine we're doing web request for
each comparison.. Imagine two times longer line of people in every
shop you visit..
> sense though.
> I want to be able to just write something like table.sort(t,
> function(a, b) return a > b end).
>
> Introducing a kind of "spaceship operator" seems like too much for a
> simple scripting language like Lua.
> What would the type of the result be? You could make it a number, of
> course, but that's really a hack.
Boolean "less than" is perfectly fine. Problem that comparisons are costly.
So we spent like 24800 microseconds just to get data. And then
compare numbers and return 0 or 1. Which takes like 20 micros.
Ideally comparator should provide as much information about relation of
given items as it can. At least three output values if there is linear
order.
What sorting algorithm will do with them is his own business.
If passing NaNs may cause a sorting algorithm to produce an error, that's a feature. You must not try to sort the unsortable (and if you do, you should be explicit about how you want it sorted). -Lars
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/9f0e3d7f-574f-4825-bb0c-642823cf60d0%40disroot.org.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAGa7JC0DZUOMFTr1a0WVa24itOtws9zTgtRGq-0ULe%2BqMYtRrw%40mail.gmail.com.
--You received this message because you are subscribed to the Google Groups "lua-l" group. To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/lua-l/90534326-894a-4993-97b4-b6d4ad80e291%40disroot.org.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/3b8f83f3-8a7a-44f9-8934-ef416f505988%40grouse.com.au.