Skip to content

Commit 001e112

Browse files
Merge pull request #380 from ardanlabs/cheikh/slices
Cheikh/slices
2 parents 5b1b707 + 72da75f commit 001e112

File tree

18 files changed

+1884
-0
lines changed

18 files changed

+1884
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the BinarySearch APIs from the
5+
// slices package.
6+
package main
7+
8+
import (
9+
"fmt"
10+
11+
"golang.org/x/exp/slices"
12+
)
13+
14+
// BinarySearch searches for target in a sorted slice and returns the
15+
// position where target is found, or the position where target would
16+
// appear in the sort order.
17+
18+
func main() {
19+
list := []int{1, 2, 3, 4, 5, 6}
20+
fmt.Println("Slice", list)
21+
22+
// -------------------------------------------------------------------------
23+
// BinarySearch
24+
25+
index, found := slices.BinarySearch(list, 9)
26+
fmt.Printf("Looking for 9, idx[%d], found[%v]\n", index, found)
27+
28+
index, found = slices.BinarySearch(list, 5)
29+
fmt.Printf("Looking for 5, idx[%d], found[%v]\n", index, found)
30+
31+
// -------------------------------------------------------------------------
32+
// BinarySearchFunc
33+
34+
index, found = slices.BinarySearchFunc(list, 7, compare)
35+
fmt.Printf("Looking for 7, idx[%d], found[%v]\n", index, found)
36+
37+
index, found = slices.BinarySearchFunc(list, 2, compare)
38+
fmt.Printf("Looking for 2, idx[%d], found[%v]\n", index, found)
39+
}
40+
41+
// Compare needs to return 0 if the two values are the same, a positive
42+
// number of a > b, and a negative number of a < b.
43+
func compare(a int, b int) int {
44+
return a - b
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Index API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Index returns the index of the first occurrence or -1 if not present.
14+
15+
func main() {
16+
list := []int{1, 1, 2, 2, 1, 1, 3, 3, 4, 5}
17+
fmt.Println("Slice", list)
18+
19+
// -------------------------------------------------------------------------
20+
// Index
21+
22+
fmt.Printf("Looking for 5, idx[%d]\n", slices.Index(list, 5))
23+
fmt.Printf("Looking for 0, idx[%d]\n", slices.Index(list, 0))
24+
fmt.Printf("Looking for 2, idx[%d]\n", slices.Index(list, 2))
25+
26+
// -------------------------------------------------------------------------
27+
// IndexFunc
28+
29+
fmt.Printf("Looking for 5, idx[%d]\n", slices.IndexFunc(list, compare(5)))
30+
fmt.Printf("Looking for 0, idx[%d]\n", slices.IndexFunc(list, compare(0)))
31+
fmt.Printf("Looking for 2, idx[%d]\n", slices.IndexFunc(list, compare(2)))
32+
}
33+
34+
func compare(a int) func(int) bool {
35+
return func(b int) bool {
36+
return a == b
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Insert API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Insert adds values into the slices at the specified index and
14+
// returns the modified slice.
15+
16+
func main() {
17+
list := []int{1, 2, 3, 4, 5, 6}
18+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
19+
20+
// -------------------------------------------------------------------------
21+
// Insert - add 7, 8, 9 to the end of the list
22+
23+
list = slices.Insert(list, 6, 7, 8, 9)
24+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
25+
26+
// -------------------------------------------------------------------------
27+
// Insert - add 0 to the beginning of the list
28+
29+
list = slices.Insert(list, 0, 0)
30+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
31+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Sort APIs from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Sort sorts a slice of any ordered type in ascending order.
14+
// IsSorted reports whether x is sorted in ascending order.
15+
16+
func main() {
17+
18+
// -------------------------------------------------------------------------
19+
// Sort
20+
21+
list := []int{1, 4, 5, 2, 8, 3, 6, 9, 7}
22+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
23+
24+
slices.Sort(list)
25+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
26+
27+
is := slices.IsSorted(list)
28+
fmt.Println("Is list sorted:", is)
29+
30+
// -------------------------------------------------------------------------
31+
// SortFunc
32+
33+
list = []int{1, 4, 5, 2, 8, 3, 6, 9, 7}
34+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
35+
36+
slices.SortFunc(list, compare)
37+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
38+
39+
is = slices.IsSortedFunc(list, compare)
40+
fmt.Println("Is list sorted:", is)
41+
42+
// -------------------------------------------------------------------------
43+
// SortStableFunc
44+
45+
list = []int{1, 4, 5, 2, 8, 3, 6, 9, 7}
46+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
47+
48+
slices.SortStableFunc(list, compare)
49+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
50+
51+
is = slices.IsSortedFunc(list, compare)
52+
fmt.Println("Is list sorted:", is)
53+
}
54+
55+
func compare(a, b int) bool {
56+
return a < b
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Replace API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Replace replaces the elements specified for a given range and returns
14+
// the modified slice.
15+
16+
func main() {
17+
list := []int{1, 2, 3, 4, 5, 6}
18+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
19+
20+
// -------------------------------------------------------------------------
21+
// Replace - Change 3 to 7
22+
23+
list = slices.Replace(list, 2, 3, 7)
24+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
25+
26+
// -------------------------------------------------------------------------
27+
// Replace - Change 4, 5, 6 to 8, 9
28+
29+
list = slices.Replace(list, 3, 6, 8, 9)
30+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Clip API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Clip removes the unused capacity from the slice.
14+
15+
func main() {
16+
list := make([]string, 0, 10)
17+
fmt.Printf("Len(%d), Cap(%d)\n", len(list), cap(list))
18+
19+
// -------------------------------------------------------------------------
20+
// Append a string to the slice
21+
22+
list = append(list, "A")
23+
fmt.Printf("Len(%d), Cap(%d)\n", len(list), cap(list))
24+
25+
// -------------------------------------------------------------------------
26+
// Clip
27+
28+
list = slices.Clip(list)
29+
fmt.Printf("Len(%d), Cap(%d)\n", len(list), cap(list))
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Clone API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Clone creates a new slice value and underlying array with a shallow
14+
// copy of the elements.
15+
16+
func main() {
17+
list := []int{1, 2, 3, 4, 5, 6}
18+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
19+
20+
// -------------------------------------------------------------------------
21+
// Clone
22+
23+
list = slices.Clone(list)
24+
fmt.Printf("Copy: Addr(%x), %v\n", &list[0], list)
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Compact APIs from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// Compact replaces consecutive runs of equal elements with a single copy.
14+
// Compact modifies the contents of the slice and does not create a new slice.
15+
16+
func main() {
17+
18+
// -------------------------------------------------------------------------
19+
// Compact
20+
21+
list := []int{1, 1, 2, 2, 1, 1, 3, 3, 4, 5}
22+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
23+
24+
list = slices.Compact(list)
25+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
26+
27+
// -------------------------------------------------------------------------
28+
// CompactFunc
29+
30+
list = []int{1, 1, 2, 2, 1, 1, 3, 3, 4, 5}
31+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
32+
33+
list = slices.CompactFunc(list, compare)
34+
fmt.Printf("List: Addr(%x), %v\n", &list[0], list)
35+
}
36+
37+
// compare needs to return true if the two values are the same.
38+
func compare(a int, b int) bool {
39+
return a == b
40+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// All material is licensed under the Apache License Version 2.0, January 2004
2+
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
// Sample program shows how to use the Compare API from the slices package.
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"golang.org/x/exp/slices"
11+
)
12+
13+
// result translates the result of the compare API.
14+
var result = map[int]string{
15+
-1: "First slice is shorter",
16+
0: "Both slices are equal",
17+
1: "Second slice is shorter",
18+
}
19+
20+
// Compare compares the elements between two slices. The elements are compared
21+
// sequentially, starting at index 0, until one element is not equal to the
22+
// other. The result of comparing the first non-matching elements is returned.
23+
24+
func main() {
25+
list1 := []int{1, 2, 3, 4, 5}
26+
list2 := []int{1, 2, 6, 4, 5, 6}
27+
list3 := []int{1, 2, 3, 4}
28+
list4 := []int{1, 2, 3, 4}
29+
30+
fmt.Println("Slice1", list1)
31+
fmt.Println("Slice2", list2)
32+
fmt.Println("Slice3", list3)
33+
fmt.Println("Slice4", list4)
34+
35+
// -------------------------------------------------------------------------
36+
// Compare list1 and list2
37+
38+
fmt.Printf("list1 vs list2: Compare(%s), Func(%s)\n",
39+
result[slices.Compare(list1, list2)],
40+
result[slices.CompareFunc(list1, list2, compare)],
41+
)
42+
43+
// -------------------------------------------------------------------------
44+
// Compare list1 and list3
45+
46+
fmt.Printf("list1 vs list3: Compare(%s), Func(%s)\n",
47+
result[slices.Compare(list1, list3)],
48+
result[slices.CompareFunc(list1, list3, compare)],
49+
)
50+
51+
// -------------------------------------------------------------------------
52+
// Compare list3 and list4
53+
54+
fmt.Printf("list3 vs list4: Compare(%s), Func(%s)\n",
55+
result[slices.Compare(list3, list4)],
56+
result[slices.CompareFunc(list3, list4, compare)],
57+
)
58+
}
59+
60+
// compare evaluates values in increasing index order, and the comparisons stop
61+
// after the first time the function returns non-zero. Return 0 is the two
62+
// values match, return -1 if a < b, and 1 if a > b.
63+
func compare(a int, b int) int {
64+
if a < b {
65+
return -1
66+
}
67+
68+
if a > b {
69+
return 1
70+
}
71+
72+
return 0
73+
}

0 commit comments

Comments
 (0)