-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
77 lines (61 loc) · 1.85 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
package rangechain
import (
"fmt"
"github.com/halprin/rangechain/keyvalue"
"testing"
)
func TestFunStuff(t *testing.T) {
stringSlice := []string{"DogCows", "goes", "Moof", "Do", "you", "like", "Clarus", "the", "DogCow"}
chain := FromSlice(stringSlice)
outputSlice, _ := chain.
Map(func(value interface{}) (interface{}, error) {
stringValue := value.(string)
return stringValue + " not", nil
}).
Filter(func(value interface{}) (bool, error) {
stringValue := value.(string)
return len(stringValue) % 2 == 0, nil
}).
Skip(1).
Slice()
fmt.Println(outputSlice)
}
func TestReduceToMapExample(t *testing.T) {
stringSlice := []string{"DogCows", "goes", "Moof", "Do", "you", "like", "Clarus", "the", "DogCow"}
chain := FromSlice(stringSlice)
outputMap, _ := chain.
ReduceWithInitialValue(func(firstItem interface{}, secondItem interface{}) (interface{}, error) {
reductionMap := firstItem.(map[string]int)
stringItem := secondItem.(string)
reductionMap[stringItem] = len(stringItem)
return reductionMap, nil
}, map[string]int{})
fmt.Println(outputMap)
}
func TestSortingMaps(t *testing.T) {
aMap := map[string]int{
"DogCow": 10,
"System 7": 7,
"Mac OS 8": 8,
"Mac OS 9": 9,
"Mac OS X": 10,
"QuickTime": 3,
"Exposé": 7,
"Control Strip": 6,
"Finder": 5,
}
chain := FromMap(aMap)
sortedAppleStuff, _ := chain.Sort(func(mapValuesToSort []interface{}) func(int, int) bool {
return func(index1 int, index2 int) bool {
mapValue1 := mapValuesToSort[index1].(keyvalue.KeyValuer)
mapValue2 := mapValuesToSort[index2].(keyvalue.KeyValuer)
rating1 := mapValue1.Value().(int)
rating2 := mapValue2.Value().(int)
return rating1 > rating2
}
}).Map(func(value interface{}) (interface{}, error) {
mapValue := value.(keyvalue.KeyValuer)
return mapValue.Key(), nil
}).Slice()
fmt.Println(sortedAppleStuff)
}