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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  	"github.com/enetx/g/cmp"
     8  )
     9  
    10  func main() {
    11  	mini := cmp.MinBy(cmp.Cmp, 3, 1, 4, 2, 5)
    12  	fmt.Println("minimum int:", mini)
    13  
    14  	mins := cmp.MinBy(cmp.Cmp, "banana", "apple", "orange")
    15  	fmt.Println("minimum string:", mins)
    16  
    17  	maxi := cmp.MaxBy(cmp.Cmp, 3, 1, 4, 2, 5)
    18  	fmt.Println("maximum integer:", maxi)
    19  
    20  	maxs := cmp.MaxBy(cmp.Cmp, "banana", "apple", "orange")
    21  	fmt.Println("maximum string:", maxs)
    22  
    23  	// cmp function
    24  	ord := func(a, b []g.Int) cmp.Ordering { return a[0].Cmp(b[0]) }
    25  
    26  	maxsis := cmp.MaxBy(ord, [][]g.Int{{1, 2, 3, 4}, {5, 6, 7, 8}}...)
    27  	fmt.Printf("maximum []g.Int: %v\n", maxsis)
    28  
    29  	maxgis := g.SliceOf([][]g.Int{{1, 2, 3, 4}, {5, 6, 7, 8}}...).MaxBy(ord)
    30  	fmt.Printf("maximum []g.Int: %v\n", maxgis)
    31  
    32  	maxgsi := g.SliceOf(1, 2, 3, 4, 5).MaxBy(cmp.Cmp)
    33  	fmt.Printf("maximum int: %v\n", maxgsi)
    34  }