diff --git a/container/garray/garray_z_example_test.go b/container/garray/garray_z_example_test.go index cf234071e..2477dd723 100644 --- a/container/garray/garray_z_example_test.go +++ b/container/garray/garray_z_example_test.go @@ -72,8 +72,50 @@ func Example_basic() { // [] } +func Example_iterate() { + array := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) + // Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order + // with given callback function . + // If returns true, then it continues iterating; or false to stop. + array.Iterator(func(k int, v string) bool { + fmt.Println(k, v) + return true + }) + // IteratorDesc iterates the array readonly in descending order with given callback function . + // If returns true, then it continues iterating; or false to stop. + array.IteratorDesc(func(k int, v string) bool { + fmt.Println(k, v) + return true + }) + + // Output: + // 0 a + // 1 b + // 2 c + // 2 c + // 1 b + // 0 a +} + +func Example_reverse() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Reverse makes array with elements in reverse order. + fmt.Println(array.Reverse().Slice()) + + // Output: + // [9 8 7 6 5 4 3 2 1] +} + +func Example_shuffle() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Shuffle randomly shuffles the array. + fmt.Println(array.Shuffle().Slice()) +} + func Example_rand() { - array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Randomly retrieve and return 2 items from the array. // It does not delete the items from array. @@ -84,6 +126,18 @@ func Example_rand() { fmt.Println(array.PopRand()) } +func Example_chunk() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Chunk splits an array into multiple arrays, + // the size of each array is determined by . + // The last chunk may contain less than size elements. + fmt.Println(array.Chunk(2)) + + // Output: + // [[1 2] [3 4] [5 6] [7 8] [9]] +} + func Example_popItem() { array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) @@ -130,9 +184,9 @@ func Example_contains() { } func Example_mergeArray() { - array1 := garray.NewFrom([]interface{}{1, 2}) - array2 := garray.NewFrom([]interface{}{3, 4}) - slice1 := []interface{}{5, 6} + array1 := garray.NewFrom(g.Slice{1, 2}) + array2 := garray.NewFrom(g.Slice{3, 4}) + slice1 := g.Slice{5, 6} slice2 := []int{7, 8} slice3 := []string{"9", "0"} fmt.Println(array1.Slice())