No, but at least that makes it theoretically possi...
# announcements
k
No, but at least that makes it theoretically possible 😏
h
Are you good at lambdas? I'm not the best at them, and I'm wondering if something I want to do is possible using them
k
I don't think this is possible unfortunately, the main distinguishing part of lambda sis that they're enclosed in
{}
.
h
oh, it was something else
k
Yeah I meant as it "at least it's now theoretically possible to add it as a language feature".
h
i'm studying for my algorithms final (and practicing kotlin) by writing all the algorithms in kotlin, and anywho: There are a couple sorting algorithms that make use of other types of sorting algorithms: namely bucket sort and radix sort (which requires an in-order sorting algorithm of your choice) So I'm trying to find the best way to make a nice radix sort function that you pass your desired sort method into as a parameter
maybe a sealed class is my best bet...
k
Does it absolutely need to be
infix
etc? Why can't you just take a normal block like
sorter: (list: MutableList<T>) -> Unit
?
h
sorry this is unrelated to the infix thing!
how might i put restrictions on that sorter to functions that do stable sorts? I guess the only way is to use classes
k
You can have an interface
Sorter
, and then another interface
StableSorter: Sorter
with stricter requirements for the
fun <T> sort(list: MutableList<T>)
function.
h
haha, yeah, that's what i did
however it seems ugly to pass a whole instantiated class into the function
k
Well you can make your
Sorter
implementations `object`s.
Copy code
object MergeSorter: StableSorter {
    fun <T> sort(list: MutableList<T>) { ...} 
}
(I don't remember if mergesort is stable 😛 )
h
it is!
the two fastest sorting methods, quick sort and heap sort, are both unstable, with pretty much everything being stable
which makes sense, tbh
stability has a cost
k
Didn't mergesort have the same
O(log(n))
as the others?
h
yeah, but then you gotta get into average cases
of course, many sorting methods are better than others if you know the data you're already working with
like insertion sort takes just n time if the data is already sorted....
i might actually be wrong about merge and quick sorts actually. it's easy to mix them up sometimes 🤔
d
I think it's O(n log(n)) @karelpeeters
k
My bad of course it can't be faster than
O(n)
simple smile
d
Also, I learned today you could do this:
Copy code
interface Sorter : (List<Any>, Comparator<in Any>) -> Unit
or something like that Actually, I guess there aren't lamba functions with type parameters. I was trying to introduce a constraint there... I guess it doesn't work then.
Someone mentioned it in this channel I think
k
Huh that's unfortunate.
h
Yeah, that would have been great