github.com/enetx/g@v1.0.80/examples/slices/slices.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/enetx/g"
     8  	"github.com/enetx/g/cmp"
     9  	"github.com/enetx/g/f"
    10  )
    11  
    12  func main() {
    13  	ws := g.SliceOf[g.String]("d", "b", "c", "a")
    14  
    15  	// ws.SortBy(g.String.Cmp)
    16  	// or
    17  	ws.SortBy(cmp.Cmp)
    18  
    19  	ws.Print()
    20  
    21  	wsl := g.SliceOf[g.String](
    22  		"aa a",
    23  		"b b",
    24  		"o o oo oooo",
    25  		"aa a a aa a",
    26  		"aaa aaaa",
    27  		"a a a aaaa",
    28  		"aaa aaa",
    29  		"three three",
    30  		"one",
    31  		"four",
    32  	)
    33  
    34  	// wsl.SortBy(func(a, b g.String) cmp.Ordering { return a.Cmp(b) }) // or
    35  	// wsl.SortBy(g.String.Cmp) // or
    36  	wsl.SortBy(cmp.Cmp)
    37  	wsl.Print() // Slice[a a a aaaa, aa a, aa a a aa a, aaa aaa, aaa aaaa, b b, four, o o oo oooo, one, three three]
    38  
    39  	wsl.SortBy(func(a, b g.String) cmp.Ordering { return a.Cmp(b).Reverse() })
    40  	wsl.Print() // Slice[three three, one, o o oo oooo, four, b b, aaa aaaa, aaa aaa, aa a a aa a, aa a, a a a aaaa]
    41  
    42  	wsl.SortBy(func(a, b g.String) cmp.Ordering {
    43  		return a.Fields().Collect().Len().Cmp(b.Fields().Collect().Len()).
    44  			Then(a.Len().Cmp(b.Len()))
    45  	})
    46  
    47  	wsl.Print() // Slice[one, four, b b, aa a, aaa aaa, aaa aaaa, three three, a a a aaaa, o o oo oooo, aa a a aa a]
    48  
    49  	slice := g.Slice[int]{1, 2, 3, 4}
    50  
    51  	slice.Iter().Filter(f.Gt(2)).Collect().Print() // Slice[3, 4]
    52  	slice.Iter().Filter(f.Eq(2)).Collect().Print() // Slice[2]
    53  	slice.Iter().Filter(f.Ne(2)).Collect().Print() // Slice[1, 3, 4]
    54  
    55  	fmt.Println(slice.Iter().All(func(i int) bool { return i != 5 }))
    56  	fmt.Println(slice.Iter().Any(func(i int) bool { return i == 5 }))
    57  
    58  	slice.Iter().Range(func(val int) bool {
    59  		fmt.Println(val)
    60  		return val != 3
    61  	})
    62  
    63  	result := g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    64  
    65  	result.Delete(1).Print()  // Slice[1, 3, 4, 5, 6, 7, 8, 9, 10]
    66  	result.Delete(-9).Print() // Slice[1, 3, 4, 5, 6, 7, 8, 9, 10]
    67  
    68  	result.SubSlice(1, -3).Print()            // Slice[2, 3, 4, 5, 6, 7]
    69  	result.SubSlice(-3, result.Len()).Print() // Slice[8, 9, 10]
    70  	result.SubSlice(-3, -1).Print()           // Slice[8, 9]
    71  	result.SubSlice(-1, result.Len()).Print() // Slice[10]
    72  
    73  	result = result.RandomSample(5)
    74  
    75  	result.Clone().Append(999).Print()
    76  	result.Print()
    77  	fmt.Printf("%#v\n", result.Std())
    78  
    79  	filled := g.NewSlice[int](10)
    80  	filled.Fill(88)
    81  
    82  	filled.Print()
    83  
    84  	slice = g.Slice[int]{1, 2, 3, 4, 5}.Print()
    85  
    86  	slice.Cut(1, 3).Print()  // Slice[1, 4, 5]
    87  	slice.Cut(-4, 3).Print() // Slice[1, 4, 5]
    88  	slice.Cut(-3, 4).Print() // Slice[1, 2, 5]
    89  
    90  	// InPlace Methods
    91  	sipl := g.NewSlice[int]()
    92  
    93  	sipl.AppendInPlace(1)
    94  	sipl.AppendInPlace(2)
    95  	sipl.AppendInPlace(3)
    96  
    97  	sipl.DeleteInPlace(1)
    98  	sipl.Fill(999999)
    99  
   100  	sipl.InsertInPlace(0, 22, 33, 44)
   101  	sipl.AddUniqueInPlace(22, 22, 22, 33, 44, 55)
   102  
   103  	sipl.Print()
   104  
   105  	slicea := g.Slice[string]{"a", "b", "c", "d"}
   106  	slicea.InsertInPlace(2, "e", "f")
   107  	slicea.Print()
   108  
   109  	slice = g.Slice[int]{1, 2, 0, 4, 0, 3, 0, 0, 0, 0}
   110  	slice = slice.Iter().Exclude(f.Zero).Collect()
   111  
   112  	slice.DeleteInPlace(0)
   113  	slice.Print()
   114  
   115  	sll := g.NewSlice[int](0, 100000)
   116  	sll = sll.Append(1).Clip()
   117  
   118  	fmt.Println(sll.Cap())
   119  
   120  	g.SliceMap([]string{"AAA", "BBB"}, g.NewString).Iter().Map(g.String.Lower).Collect().Print()
   121  	g.SliceOf([]string{"AAA", "BBB"}...).Iter().Map(strings.ToLower).Collect().Print()
   122  
   123  	g.SliceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).Iter().
   124  		Filter(isPrime).
   125  		ForEach(func(n int) { fmt.Printf("%d is a prime number\n", n) })
   126  }
   127  
   128  func isPrime(n int) bool {
   129  	for i := 2; i < n/2; i++ {
   130  		if n%i == 0 {
   131  			return false
   132  		}
   133  	}
   134  
   135  	return true
   136  }