Landei
unread,Dec 31, 2010, 5:25:18 AM12/31/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sgine-dev
Hi!
I suggest to introduce a little bit more abstraction in Sort by using
a trait encapsulating a sort algorithm, e.g.
trait Sort {
def apply[A](array: ResizableArray[A], sortFunction: (A, A) =>
Boolean): ResizableArray[A]
def apply[A](array: ResizableArray[A])(implicit ord : Ordering[A]):
ResizableArray[A] =
apply(array, ord.lteq)
}
As you can see, this would allow to share some behavior (as shown here
by providing an alternative implementation using Orderings) between
instances. Of course we don't want to break any existing code, but we
don't have to:
object Sort {
val bubbleSort = new Sort {
@scala.annotation.tailrec
def apply[A](array: ResizableArray[A], sortFunction: (A, A) =>
Boolean): ResizableArray[A] = {
//the existing sort code goes here
}
}
}
With this solution, all existing code will work as before, but it's
easier to share functionality between different sort algorithms, and
to pass a certain Sort implementation around. Of course this change
makes only sense if other algorithms will be implemented as well, but
the comments in Sort suggest that this is the plan. I'd translate an
in-place mergesort, which seems to be the most promising choice here.
What do you think?
Happy New Year!
Daniel