This content originally appeared on Level Up Coding - Medium and was authored by nwillc
Okay, I can’t put down the genfuncs repo that I spoke about here! I’ve been adding more generic functions, cleaning, documenting, and…
Heaps O’ Sorts!
I pulled in my own generics based Heap and added insertion sort, heap sort, and quicksort.
To achieve all of these, and avoid the mess that is sorting in Go today I obviously employed generics. Rather than have a bunch of type variations as sort package does, I started with a Comparator:
type Comparator[T any] func(a, b T) ComparedOrder
Where ComparedOrder is a traditional:
var (
LessThan ComparedOrder = -1
EqualTo ComparedOrder = 0
GreaterThan ComparedOrder = 1
)
So given a Comparator function as an argument the Heap and sorts become totally genericized:
func NewHeap[T any](comparator Comparator[T]) *Heap[T]
func HeapSort[T any](slice []T, comparator Comparator[T])
func InsertionSort[T any](slice []T, comparator Comparator[T])
func QuickSort[T any](slice []T, comparator Comparator[T])
Now, wait you ask, do you always have to implement a Comparator even for known types Go says are constrants.Ordered? No I got you!
var numericOrdered = genfuncs.OrderedComparator[int]()
That again uses generics and the constraints.Ordered constraint to create a Comparator for float types, int types, and strings.
So a basic sort, in ascending or descending order would look like:
alphaOrder := genfuncs.OrderedComparator[string]()
letters := strings.Split("example", "")
genfuncs.QuickSort(letters, alphaOrder)
fmt.Println(letters) // [a e e l m p x]
genfuncs.QuickSort(letters, genfuncs.ReverseComparator(alphaOrder))
fmt.Println(letters) // [x p m l e e a]
But if you have a complex type with crazy lexical rules just implement a specific Comparator for it.
Take a look at Go’s sort package and you’ll frown at the gymnastics it does to make it useful for different types. Go also does some magic about picking which sort is appropriate, and its sort implementations a touch more optimized, but I bet if I added those features to this it would be like 50% of the size and more flexible and comprehensible.
I’m Really Enjoying This
So far I’m really liking the 1.18 generics. Writing and using the functions, and banishing the boilerplate type specific code is heaven. I’ve even got requests to use the package commercially from folks, which is fine, but I cautioned them, as I put right in the docs, that I’m pretty sure Go will get an official form of this real soon. It’s just too easy and too big a win for them to leave out. But until them enjoy!
Go 1.18 Beta 1: More Generic Gifts for The Holidays was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by nwillc
nwillc | Sciencx (2021-12-24T11:44:05+00:00) Go 1.18 Beta 1: More Generic Gifts for The Holidays. Retrieved from https://www.scien.cx/2021/12/24/go-1-18-beta-1-more-generic-gifts-for-the-holidays/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.