|
6 | 6 | "github.com/slashdevops/r9e" |
7 | 7 | ) |
8 | 8 |
|
9 | | -func Example_basic() { |
| 9 | +func ExampleMapKeyValue_basic() { |
10 | 10 | type MathematicalConstants struct { |
11 | 11 | Name string |
12 | 12 | Value float64 |
@@ -61,3 +61,57 @@ func Example_basic() { |
61 | 61 | fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value) |
62 | 62 | }) |
63 | 63 | } |
| 64 | + |
| 65 | +func ExampleSMapKeyValue_basic() { |
| 66 | + type MathematicalConstants struct { |
| 67 | + Name string |
| 68 | + Value float64 |
| 69 | + } |
| 70 | + |
| 71 | + kv := r9e.NewSMapKeyValue[string, MathematicalConstants]() |
| 72 | + |
| 73 | + kv.Set("pi", MathematicalConstants{"Archimedes' constant", 3.141592}) |
| 74 | + kv.Set("e", MathematicalConstants{"Euler number, Napier's constant", 2.718281}) |
| 75 | + kv.Set("γ", MathematicalConstants{"Euler number, Napier's constant", 0.577215}) |
| 76 | + kv.Set("Φ", MathematicalConstants{"Golden ratio constant", 1.618033}) |
| 77 | + kv.Set("ρ", MathematicalConstants{"Plastic number ρ (or silver constant)", 2.414213}) |
| 78 | + |
| 79 | + kvFilteredValues := kv.FilterValue(func(value MathematicalConstants) bool { |
| 80 | + return value.Value > 2.0 |
| 81 | + }) |
| 82 | + |
| 83 | + fmt.Println("Mathematical Constants:") |
| 84 | + kvFilteredValues.ForEach(func(key string, value MathematicalConstants) { |
| 85 | + fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value) |
| 86 | + }) |
| 87 | + |
| 88 | + fmt.Printf("\n") |
| 89 | + fmt.Printf("The most famous mathematical constant:\n") |
| 90 | + fmt.Printf("Name: %v, Value: %v\n", kv.Get("pi").Name, kv.Get("pi").Value) |
| 91 | + |
| 92 | + lst := kv.SortValues(func(value1, value2 MathematicalConstants) bool { |
| 93 | + return value1.Value > value2.Value |
| 94 | + }) |
| 95 | + |
| 96 | + fmt.Printf("\n") |
| 97 | + fmt.Printf("The most famous mathematical constant sorted by value:\n") |
| 98 | + for i, value := range lst { |
| 99 | + fmt.Printf("i: %v, Name: %v, Value: %v\n", i, value.Name, value.Value) |
| 100 | + } |
| 101 | + |
| 102 | + kvHigh, kvLow := kv.Partition(func(key string, value MathematicalConstants) bool { |
| 103 | + return value.Value > 2.5 |
| 104 | + }) |
| 105 | + |
| 106 | + fmt.Printf("\n") |
| 107 | + fmt.Printf("Mathematical constants which value is greater than 2.5:\n") |
| 108 | + kvHigh.ForEach(func(key string, value MathematicalConstants) { |
| 109 | + fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value) |
| 110 | + }) |
| 111 | + |
| 112 | + fmt.Printf("\n") |
| 113 | + fmt.Printf("Mathematical constants which value is less than 2.5:\n") |
| 114 | + kvLow.ForEach(func(key string, value MathematicalConstants) { |
| 115 | + fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value) |
| 116 | + }) |
| 117 | +} |
0 commit comments