-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexamples_test.go
96 lines (87 loc) · 2.33 KB
/
examples_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
package iter_test
import (
"fmt"
"github.com/disksing/iter/v2/algo"
"github.com/disksing/iter/v2/slices"
"github.com/disksing/iter/v2/strs"
)
// // Print all list items to console.
// func ExampleIOWriter() {
// l := list.New()
// GenerateN(ListBackInserter(l), 5, IotaGenerator(1))
// Copy(lBegin(l), lEnd(l), IOWriter(os.Stdout, "->"))
// // Output:
// // 1->2->3->4->5
// }
// Reverse a string.
func ExampleMakeString() {
s := "!dlrow olleH"
fmt.Println(strs.MakeString[byte](strs.RBegin(s), strs.REnd(s)))
b := []byte(s)
algo.Reverse[byte](slices.Begin(b), slices.End(b))
fmt.Println(string(b))
// Output:
// Hello world!
// Hello world!
}
// Deduplicate elements.
func ExampleUnique() {
in := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}
algo.Sort[int](slices.Begin(in), slices.End(in))
in = in[:slices.Begin(in).Distance(algo.Unique[int](slices.Begin(in), slices.End(in)))]
fmt.Println(in)
// Output:
// [1 2 3 4]
}
// // Sum all integers received from a channel.
// func ExampleChanReader() {
// ch := make(chan int)
// go func() {
// CopyN(IotaReader(1), 100, ChanWriter(ch))
// close(ch)
// }()
// fmt.Println(Accumulate(ChanReader(ch), ChanEOF, 0))
// // Output:
// // 5050
// }
// Remove consecutive spaces in a string.
func ExampleUniqueCopyIf() {
str := " a quick brown fox "
var sb strs.StringBuilderInserter[byte]
algo.UniqueCopyIf(strs.Begin(str), strs.End(str), &sb,
func(x, y byte) bool { return x == ' ' && y == ' ' })
fmt.Println(sb.String())
// Output:
// a quick brown fox
}
// // Collect N maximum elements from a channel.
// func ExamplePartialSortCopyBy() {
// ch := make(chan int)
// go func() {
// n := make([]int, 100)
// Iota(begin(n), end(n), 1)
// Shuffle(begin(n), end(n), r)
// Copy(begin(n), end(n), ChanWriter(ch))
// close(ch)
// }()
// top := make([]int, 5)
// PartialSortCopyBy(ChanReader(ch), ChanEOF, begin(top), end(top),
// func(x, y any) bool { return x.(int) > y.(int) })
// Copy(begin(top), end(top), IOWriter(os.Stdout, ", "))
// // Output:
// // 100, 99, 98, 97, 96
// }
// Print all permutations of ["a", "b", "c"].
func ExampleNextPermutation() {
s := []string{"a", "b", "c"}
for ok := true; ok; ok = algo.NextPermutation[string](slices.Begin(s), slices.End(s)) {
fmt.Println(s)
}
// Output:
// [a b c]
// [a c b]
// [b a c]
// [b c a]
// [c a b]
// [c b a]
}