-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
117 lines (93 loc) · 3.92 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package r9e_test
import (
"fmt"
"github.com/slashdevops/r9e"
)
func ExampleMapKeyValue_basic() {
type MathematicalConstants struct {
Name string
Value float64
}
// With Capacity allocated
// kv := r9e.NewMapKeyValue[string, MathematicalConstants](r9e.WithCapacity(5))
kv := r9e.NewMapKeyValue[string, MathematicalConstants]()
kv.Set("pi", MathematicalConstants{"Archimedes' constant", 3.141592})
kv.Set("e", MathematicalConstants{"Euler number, Napier's constant", 2.718281})
kv.Set("γ", MathematicalConstants{"Euler number, Napier's constant", 0.577215})
kv.Set("Φ", MathematicalConstants{"Golden ratio constant", 1.618033})
kv.Set("ρ", MathematicalConstants{"Plastic number ρ (or silver constant)", 2.414213})
kvFilteredValues := kv.FilterValue(func(value MathematicalConstants) bool {
return value.Value > 2.0
})
fmt.Println("Mathematical Constants:")
kvFilteredValues.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
fmt.Printf("\n")
fmt.Printf("The most famous mathematical constant:\n")
fmt.Printf("Name: %v, Value: %v\n", kv.Get("pi").Name, kv.Get("pi").Value)
lst := kv.SortValues(func(value1, value2 MathematicalConstants) bool {
return value1.Value > value2.Value
})
fmt.Printf("\n")
fmt.Printf("The most famous mathematical constant sorted by value:\n")
for i, value := range lst {
fmt.Printf("i: %v, Name: %v, Value: %v\n", i, value.Name, value.Value)
}
kvHigh, kvLow := kv.Partition(func(key string, value MathematicalConstants) bool {
return value.Value > 2.5
})
fmt.Printf("\n")
fmt.Printf("Mathematical constants which value is greater than 2.5:\n")
kvHigh.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
fmt.Printf("\n")
fmt.Printf("Mathematical constants which value is less than 2.5:\n")
kvLow.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
}
func ExampleSMapKeyValue_basic() {
type MathematicalConstants struct {
Name string
Value float64
}
kv := r9e.NewSMapKeyValue[string, MathematicalConstants]()
kv.Set("pi", MathematicalConstants{"Archimedes' constant", 3.141592})
kv.Set("e", MathematicalConstants{"Euler number, Napier's constant", 2.718281})
kv.Set("γ", MathematicalConstants{"Euler number, Napier's constant", 0.577215})
kv.Set("Φ", MathematicalConstants{"Golden ratio constant", 1.618033})
kv.Set("ρ", MathematicalConstants{"Plastic number ρ (or silver constant)", 2.414213})
kvFilteredValues := kv.FilterValue(func(value MathematicalConstants) bool {
return value.Value > 2.0
})
fmt.Println("Mathematical Constants:")
kvFilteredValues.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
fmt.Printf("\n")
fmt.Printf("The most famous mathematical constant:\n")
fmt.Printf("Name: %v, Value: %v\n", kv.Get("pi").Name, kv.Get("pi").Value)
lst := kv.SortValues(func(value1, value2 MathematicalConstants) bool {
return value1.Value > value2.Value
})
fmt.Printf("\n")
fmt.Printf("The most famous mathematical constant sorted by value:\n")
for i, value := range lst {
fmt.Printf("i: %v, Name: %v, Value: %v\n", i, value.Name, value.Value)
}
kvHigh, kvLow := kv.Partition(func(key string, value MathematicalConstants) bool {
return value.Value > 2.5
})
fmt.Printf("\n")
fmt.Printf("Mathematical constants which value is greater than 2.5:\n")
kvHigh.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
fmt.Printf("\n")
fmt.Printf("Mathematical constants which value is less than 2.5:\n")
kvLow.ForEach(func(key string, value MathematicalConstants) {
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
})
}