imagine you are working in a large codebase. you need to fetch different kinds of data and transforming them, grouping them or sorting them.
lets take a closer look at Sorting and Heaps in golang Specifically. we already familiar with heaps and its important interface. the Heap.Interface. a very performant and impressive implementation of heaps and its sorting functionality.
type Interface interface {
sort.Interface
Push(x any) // add x as element Len()
Pop() any // remove and return element Len() - 1.
}
as a programming language, it couldnt be done better than what it is today. but most of the time we might not need to implement all the interface items. dont get me wrong the functionalities should exist but mostly all that matters for us is that how the sorting will be done.
ZenQL's Implementation
In the latest version take advantage of sorting and heaps functionality. in a fast and agile way!
result := From(personList).Where(func(person Person) bool {
return person.Active == true
}).CollectSorted(func(person Person, person2 Person) bool {
return person.Identifier < person2.Identifier
}, true)
In the code snippet above we perform a sort on our collections using the thor engine very easily. we just express our desire about how the sorting needs to be done and wether its ascending or descending. and other functionalities are implemented as below:
type Sortable[T any] struct {
Items []T
less func(a, b T) bool
desc bool
}
func (h Sortable[T]) Len() int {
return len(h.Items)
}
func (h Sortable[T]) Swap(i, j int) {
h.Items[i], h.Items[j] = h.Items[j], h.Items[i]
}
func (h *Sortable[T]) Push(x any) {
h.Items = append(h.Items, x.(T))
}
func (h *Sortable[T]) Pop() any {
old := h.Items
n := len(old)
item := old[n-1]
h.Items = old[:n-1]
return item
}
be faster and more agile with the Golang ZenQL.
Click To Visit ZenQLRepository
Top comments (0)