I need to implement quicksort with several ways to select pivot, so I've implemented a routine that takes a pivot chooser as a parameter. But definitions of concrete implementations contain lots of boilerplate, is there a more concise way to define them?
private def qsort[a <% Ordered[a]](xs: Stream[a])(choosePivot:Stream[a] => a): Stream[a] = { if(xs.lengthCompare(1) <= 0) xs else { val pivot = choosePivot(xs) val l = xs.filter(_ < pivot) val r = xs.filter(_ > pivot) qsort(l)(choosePivot) ++ pivot#::qsort(r)(choosePivot) } } def qsortHead[a <% Ordered[a]](xs: Stream[a]) = qsort(xs)(ys => ys.head) def qsortLast[a <% Ordered[a]](xs: Stream[a]) = qsort(xs)(ys => ys.last) def qsortRandom[a <% Ordered[a]](xs: Stream[a]) = qsort(xs)(ys => ys(rng.nextInt(ys.length))) In Haskell I could just write something like qsortHead = qsort head if the choose pivot function is the first parameter or qsortHead xs = qsort xs (\ys -> head ys) if it's the second. Is there something similar in Scala?
(ys => xs(rng.nextInt(xs.length)))should read(ys => ys(rng.nextInt(ys.length)))qsortHead xs = qsort xs head