@@ -15,7 +15,7 @@ import "github.com/dashjay/xiter/pkg/xcmp"
1515
1616
1717<a name =" Compare " ></a >
18- ## func [ Compare] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/cmp .go#L14 > )
18+ ## func [ Compare] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp .go#L14 > )
1919
2020``` go
2121func Compare [T Ordered ](x, y T ) int
@@ -29,15 +29,21 @@ func Compare[T Ordered](x, y T) int
2929
3030
3131``` go
32- fmt.Println (xcmp.Compare (1 , 2 ))
33- fmt.Println (xcmp.Compare (" a" , " aa" ))
34- fmt.Println (xcmp.Compare (1.5 , 1.5 ))
35- fmt.Println (xcmp.Compare (math.NaN (), 1.0 ))
36- // Output:
37- // -1
38- // -1
39- // 0
40- // -1
32+ package main
33+
34+ import (
35+ " fmt"
36+ " math"
37+
38+ " github.com/dashjay/xiter/pkg/xcmp"
39+ )
40+
41+ func main () {
42+ fmt.Println (xcmp.Compare (1 , 2 ))
43+ fmt.Println (xcmp.Compare (" a" , " aa" ))
44+ fmt.Println (xcmp.Compare (1.5 , 1.5 ))
45+ fmt.Println (xcmp.Compare (math.NaN (), 1.0 ))
46+ }
4147```
4248
4349#### Output
@@ -53,7 +59,7 @@ fmt.Println(xcmp.Compare(math.NaN(), 1.0))
5359</details >
5460
5561<a name =" Less " ></a >
56- ## func [ Less] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/cmp .go#L10 > )
62+ ## func [ Less] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp .go#L10 > )
5763
5864``` go
5965func Less [T Ordered ](x, y T ) bool
@@ -67,15 +73,21 @@ func Less[T Ordered](x, y T) bool
6773
6874
6975``` go
70- fmt.Println (xcmp.Less (1 , 2 ))
71- fmt.Println (xcmp.Less (" a" , " aa" ))
72- fmt.Println (xcmp.Less (1.0 , math.NaN ()))
73- fmt.Println (xcmp.Less (math.NaN (), 1.0 ))
74- // Output:
75- // true
76- // true
77- // false
78- // true
76+ package main
77+
78+ import (
79+ " fmt"
80+ " math"
81+
82+ " github.com/dashjay/xiter/pkg/xcmp"
83+ )
84+
85+ func main () {
86+ fmt.Println (xcmp.Less (1 , 2 ))
87+ fmt.Println (xcmp.Less (" a" , " aa" ))
88+ fmt.Println (xcmp.Less (1.0 , math.NaN ()))
89+ fmt.Println (xcmp.Less (math.NaN (), 1.0 ))
90+ }
7991```
8092
8193#### Output
91103</details >
92104
93105<a name =" Or " ></a >
94- ## func [ Or] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/cmp .go#L18 > )
106+ ## func [ Or] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp .go#L18 > )
95107
96108``` go
97109func Or [T comparable](vals ...T ) T
@@ -105,18 +117,24 @@ func Or[T comparable](vals ...T) T
105117
106118
107119``` go
108- // Suppose we have some user input
109- // that may or may not be an empty string
110- userInput1 := " "
111- userInput2 := " some text"
120+ package main
121+
122+ import (
123+ " fmt"
124+
125+ " github.com/dashjay/xiter/pkg/xcmp"
126+ )
127+
128+ func main () {
129+ // Suppose we have some user input
130+ // that may or may not be an empty string
131+ userInput1 := " "
132+ userInput2 := " some text"
112133
113- fmt.Println (xcmp.Or (userInput1, " default" ))
114- fmt.Println (xcmp.Or (userInput2, " default" ))
115- fmt.Println (xcmp.Or (userInput1, userInput2, " default" ))
116- // Output:
117- // default
118- // some text
119- // some text
134+ fmt.Println (xcmp.Or (userInput1, " default" ))
135+ fmt.Println (xcmp.Or (userInput2, " default" ))
136+ fmt.Println (xcmp.Or (userInput1, userInput2, " default" ))
137+ }
120138```
121139
122140#### Output
@@ -136,36 +154,68 @@ some text
136154
137155
138156``` go
139- orders := []Order {
140- {" foo" , " alice" , 1.00 },
141- {" bar" , " bob" , 3.00 },
142- {" baz" , " carol" , 4.00 },
143- {" foo" , " alice" , 2.00 },
144- {" bar" , " carol" , 1.00 },
145- {" foo" , " bob" , 4.00 },
157+ package main
158+
159+ import (
160+ " fmt"
161+ " sort"
162+ " strings"
163+
164+ " github.com/dashjay/xiter/pkg/xcmp"
165+ )
166+
167+ type Order struct {
168+ Product string
169+ Customer string
170+ Price float64
146171}
147- // Sort by customer first, product second, and last by higher price
148- sort.Sort (Orders (orders))
149-
150- // wait for the implement of slices.SortFunc
151- // SortFunc(orders, func(a, b Order) int {
152- // return cmp.Or(
153- // strings.Compare(a.Customer, b.Customer),
154- // strings.Compare(a.Product, b.Product),
155- // cmp.Compare(b.Price, a.Price),
156- // )
157- // })
158- for _ , order := range orders {
159- fmt.Printf (" %s %s %.2f \n " , order.Product , order.Customer , order.Price )
172+
173+ type Orders []Order
174+
175+ func (o Orders ) Len () int {
176+ return len (o)
177+ }
178+ func (o Orders ) Less (i , j int ) bool {
179+ a , b := o[i], o[j]
180+ if xcmp.Or (
181+ strings.Compare (a.Customer , b.Customer ),
182+ strings.Compare (a.Product , b.Product ),
183+ xcmp.Compare (b.Price , a.Price )) < 0 {
184+ return true
185+ } else {
186+ return false
187+ }
188+ }
189+
190+ func (o Orders ) Swap (i , j int ) {
191+ o[i], o[j] = o[j], o[i]
160192}
161193
162- // Output:
163- // foo alice 2.00
164- // foo alice 1.00
165- // bar bob 3.00
166- // foo bob 4.00
167- // bar carol 1.00
168- // baz carol 4.00
194+ func main () {
195+ orders := []Order{
196+ {" foo" , " alice" , 1.00 },
197+ {" bar" , " bob" , 3.00 },
198+ {" baz" , " carol" , 4.00 },
199+ {" foo" , " alice" , 2.00 },
200+ {" bar" , " carol" , 1.00 },
201+ {" foo" , " bob" , 4.00 },
202+ }
203+ // Sort by customer first, product second, and last by higher price
204+ sort.Sort (Orders (orders))
205+
206+ // wait for the implement of slices.SortFunc
207+ // SortFunc(orders, func(a, b Order) int {
208+ // return cmp.Or(
209+ // strings.Compare(a.Customer, b.Customer),
210+ // strings.Compare(a.Product, b.Product),
211+ // cmp.Compare(b.Price, a.Price),
212+ // )
213+ // })
214+ for _ , order := range orders {
215+ fmt.Printf (" %s %s %.2f \n " , order.Product , order.Customer , order.Price )
216+ }
217+
218+ }
169219```
170220
171221#### Output
@@ -183,7 +233,7 @@ baz carol 4.00
183233</details >
184234
185235<a name =" Ordered " ></a >
186- ## type [ Ordered] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/cmp .go#L8 > )
236+ ## type [ Ordered] ( < https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp .go#L8 > )
187237
188238
189239
0 commit comments