Skip to content

Commit 0119b38

Browse files
committed
Add readme
1 parent 9dd02d8 commit 0119b38

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Extra
2+
3+
[![Latest Stable Version][ico-release]][link-release]
4+
[![Build Status][ico-workflow]][link-workflow]
5+
[![Coverage Status][ico-coverage]][link-coverage]
6+
[![Go Report Card][ico-go-report-card]][link-go-report-card]
7+
[![Go Dev Reference][ico-go-dev-reference]][link-go-dev-reference]
8+
[![Software License][ico-license]][link-licence]
9+
10+
Collection of extra packages that implement some useful features that are missing in the standard library.
11+
12+
13+
## Installation
14+
15+
```bash
16+
go get github.com/gravitton/x
17+
```
18+
19+
20+
## Usage
21+
22+
### Heap
23+
24+
Generic Heap backed by slice implementation using `container/heap` package from existing [proposal](https://github.com/golang/go/issues/47632).
25+
26+
```go
27+
package main
28+
29+
import (
30+
"cmp"
31+
"fmt"
32+
33+
"github.com/gravitton/x/container/heap"
34+
)
35+
36+
37+
type priorityItem struct {
38+
value string
39+
priority int
40+
index int
41+
}
42+
43+
func (i *priorityItem) Compare(item *priorityItem) int {
44+
return cmp.Compare(i.priority, item.priority)
45+
}
46+
47+
func (i *priorityItem) setIndex(index int) {
48+
i.index = index
49+
}
50+
51+
func main() {
52+
pq := heap.NewComparable[priorityItem]()
53+
pq.SetIndex((*priorityItem).setIndex)
54+
55+
pq.Push(&priorityItem{value: "banana", priority: 3})
56+
pq.Push(&priorityItem{value: "apple", priority: 2})
57+
pq.Push(&priorityItem{value: "pear", priority: 4})
58+
59+
orange := &priorityItem{value: "orange", priority: 1}
60+
pq.Push(orange)
61+
orange.priority = 5
62+
pq.Fix(orange.index)
63+
64+
for pq.Len() > 0 {
65+
item := pq.Pop()
66+
fmt.Printf("%.2d:%s\n", item.priority, item.value)
67+
}
68+
69+
// 02:apple 03:banana 04:pear 05:orange
70+
}
71+
```
72+
73+
### Queue
74+
75+
Generic queue implementation. It is just a wrapper around `[]T` with some extra methods.
76+
77+
```go
78+
package main
79+
80+
import (
81+
"fmt"
82+
83+
"github.com/gravitton/x/container/queue"
84+
)
85+
86+
func main() {
87+
q := queue.New[int]()
88+
q.Push(2)
89+
q.Push(3)
90+
q.Push(1)
91+
92+
for q.Len() > 0 {
93+
val := q.Pop()
94+
fmt.Println(val)
95+
}
96+
97+
// 2 3 1
98+
}
99+
```
100+
101+
### Map
102+
103+
Generic Map method for slices.
104+
105+
```go
106+
package main
107+
108+
import (
109+
"fmt"
110+
"math"
111+
112+
"github.com/gravitton/x/slices"
113+
)
114+
115+
func main() {
116+
sf := slices.Map([]float64{1.1, 2.6, 3.4}, math.Round)
117+
si := slices.Map(sf, func(x float64) int {
118+
return 10 + int(x)
119+
})
120+
121+
fmt.Println(si)
122+
// [11, 13, 13]
123+
}
124+
```
125+
## Credits
126+
127+
- [Tomáš Novotný](https://github.com/tomas-novotny)
128+
- [All Contributors][link-contributors]
129+
130+
131+
## License
132+
133+
The MIT License (MIT). Please see [License File][link-licence] for more information.
134+
135+
136+
[ico-license]: https://img.shields.io/github/license/gravitton/x.svg?style=flat-square&colorB=blue
137+
[ico-workflow]: https://img.shields.io/github/actions/workflow/status/gravitton/x/main.yml?branch=main&style=flat-square
138+
[ico-release]: https://img.shields.io/github/v/release/gravitton/x?style=flat-square&colorB=blue
139+
[ico-go-dev-reference]: https://img.shields.io/badge/go.dev-reference-blue?style=flat-square
140+
[ico-go-report-card]: https://goreportcard.com/badge/github.com/gravitton/x?style=flat-square
141+
[ico-coverage]: https://img.shields.io/coverallsCoverage/github/gravitton/x?style=flat-square
142+
143+
[link-author]: https://github.com/gravitton
144+
[link-release]: https://github.com/gravitton/x/releases
145+
[link-contributors]: https://github.com/gravitton/x/contributors
146+
[link-licence]: ./LICENSE.md
147+
[link-changelog]: ./CHANGELOG.md
148+
[link-workflow]: https://github.com/gravitton/x/actions
149+
[link-go-dev-reference]: https://pkg.go.dev/github.com/gravitton/x
150+
[link-go-report-card]: https://goreportcard.com/report/github.com/gravitton/x
151+
[link-coverage]: https://coveralls.io/github/gravitton/x

container/heap/heap_example_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ func ExampleHeap_priorityQueue() {
3434

3535
for pq.Len() > 0 {
3636
item := pq.Pop()
37-
fmt.Printf("%.2d:%s ", item.priority, item.value)
37+
fmt.Printf("%.2d:%s\n", item.priority, item.value)
3838
}
3939

4040
// Output:
41-
// 02:apple 03:banana 04:pear 05:orange
41+
// 02:apple
42+
// 03:banana
43+
// 04:pear
44+
// 05:orange
4245
}

0 commit comments

Comments
 (0)