Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
/ x Public

Collection of extra packages that implement some useful features that are missing in the standard library

License

Notifications You must be signed in to change notification settings

gravitton/x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extra

Latest Stable Version Build Status Coverage Status Go Report Card Go Dev Reference Software License

Collection of extra packages that implement some useful features that are missing in the standard library.

Installation

go get github.com/gravitton/x

Usage

Heap

Generic Heap backed by slice implementation using container/heap package from existing proposal.

package main

import (
	"cmp"
	"fmt"

	"github.com/gravitton/x/container/heap"
)

type priorityItem struct {
	value    string
	priority int
	index    int
}

func (i *priorityItem) Compare(item *priorityItem) int {
	return cmp.Compare(i.priority, item.priority)
}

func (i *priorityItem) setIndex(index int) {
	i.index = index
}

func main() {
	pq := heap.NewComparable[priorityItem]()
	pq.SetIndex((*priorityItem).setIndex)

	pq.Push(&priorityItem{value: "banana", priority: 3})
	pq.Push(&priorityItem{value: "apple", priority: 2})
	pq.Push(&priorityItem{value: "pear", priority: 4})

	orange := &priorityItem{value: "orange", priority: 1}
	pq.Push(orange)
	orange.priority = 5
	pq.Fix(orange.index)

	for pq.Len() > 0 {
		item := pq.Pop()
		fmt.Printf("%.2d:%s\n", item.priority, item.value)
	}

	// 02:apple 03:banana 04:pear 05:orange
}

Queue

Generic queue implementation. It is just a wrapper around []T with some extra methods.

package main

import (
	"fmt"

	"github.com/gravitton/x/container/queue"
)

func main() {
	q := queue.New[int]()
	q.Push(2)
	q.Push(3)
	q.Push(1)

	for q.Len() > 0 {
		val := q.Pop()
		fmt.Println(val)
	}

	// 2 3 1
}

Map

Generic Map method for slices.

package main

import (
	"fmt"
	"math"

	"github.com/gravitton/x/slices"
)

func main() {
	sf := slices.Map([]float64{1.1, 2.6, 3.4}, math.Round)
	si := slices.Map(sf, func(x float64) int {
		return 10 + int(x)
	})

	fmt.Println(si)

	// [11, 13, 13]
}

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Collection of extra packages that implement some useful features that are missing in the standard library

Topics

Resources

License

Stars

Watchers

Forks

Languages