-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_example_test.go
More file actions
64 lines (58 loc) · 1.18 KB
/
map_example_test.go
File metadata and controls
64 lines (58 loc) · 1.18 KB
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
package itu_test
import (
"fmt"
"slices"
"strings"
"github.com/lymar/itu"
)
func ExampleMap() {
input := slices.Values([]int{0, 1, 2})
result := itu.Map(input, func(v int) string {
return fmt.Sprintf("[ %d ]", v+1)
})
for v := range result {
fmt.Println(v)
}
// Output:
// [ 1 ]
// [ 2 ]
// [ 3 ]
}
func ExampleMap2() {
// slices.All returns an iter.Seq2 over index/value pairs.
input := slices.All([]string{"a", "bb"})
result := itu.Map2(input, func(i int, s string) (string, int) {
return strings.ToUpper(s), i
})
for s, i := range result {
fmt.Printf("%s:%d\n", s, i)
}
// Output:
// A:0
// BB:1
}
func ExampleMapTo2() {
input := slices.Values([]int{2, 3})
result := itu.MapTo2(input, func(v int) (string, int) {
return fmt.Sprintf("key=%d", v), v * v
})
for label, sq := range result {
fmt.Printf("%s:%d\n", label, sq)
}
// Output:
// key=2:4
// key=3:9
}
func ExampleMap2To() {
// slices.All returns an iter.Seq2 over index/value pairs.
input := slices.All([]string{"x", "y"})
result := itu.Map2To(input, func(i int, s string) string {
return fmt.Sprintf("%d=%s", i, s)
})
for v := range result {
fmt.Println(v)
}
// Output:
// 0=x
// 1=y
}