Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,6 @@
def sort(xs: List[Int]): List[Int] = xs match {
case Nil => Nil
case head :: tail =>
val (less, notLess) = tail.partition(_ < head) // Arbitrarily partition list in two
sort(less) ++ (head :: sort(notLess)) // Sort each half
}

View file

@ -0,0 +1,6 @@
def sort[T](xs: List[T], lessThan: (T, T) => Boolean): List[T] = xs match {
case Nil => Nil
case x :: xx =>
val (lo, hi) = xx.partition(lessThan(_, x))
sort(lo, lessThan) ++ (x :: sort(hi, lessThan))
}

View file

@ -0,0 +1,6 @@
def sort[T](xs: List[T])(implicit ord: Ordering[T]): List[T] = xs match {
case Nil => Nil
case x :: xx =>
val (lo, hi) = xx.partition(ord.lt(_, x))
sort[T](lo) ++ (x :: sort[T](hi))
}

View file

@ -0,0 +1,6 @@
def sort[T <: Ordered[T]](xs: List[T]): List[T] = xs match {
case Nil => Nil
case x :: xx =>
val (lo, hi) = xx.partition(_ < x)
sort(lo) ++ (x :: sort(hi))
}

View file

@ -0,0 +1,17 @@
def sort[T, C[T] <: scala.collection.TraversableLike[T, C[T]]]
(xs: C[T])
(implicit ord: scala.math.Ordering[T],
cbf: scala.collection.generic.CanBuildFrom[C[T], T, C[T]]): C[T] = {
// Some collection types can't pattern match
if (xs.isEmpty) {
xs
} else {
val (lo, hi) = xs.tail.partition(ord.lt(_, xs.head))
val b = cbf()
b.sizeHint(xs.size)
b ++= sort(lo)
b += xs.head
b ++= sort(hi)
b.result()
}
}