github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/comparer/api.go (about)

     1  // Package comparer provides builders of slices.CompareFunc comparsion functions
     2  package comparer
     3  
     4  import (
     5  	"golang.org/x/exp/constraints"
     6  
     7  	"github.com/m4gshm/gollections/op"
     8  )
     9  
    10  // Of creates a comparer for orderable values obtained by the converter
    11  func Of[T any, O constraints.Ordered](converter func(T) O) func(T, T) int {
    12  	return func(e1, e2 T) int {
    13  		return op.Compare(converter(e1), converter(e2))
    14  	}
    15  }
    16  
    17  // Reverse creates a descending comparer for orderable values obtained by the converter
    18  func Reverse[T any, O constraints.Ordered](converter func(T) O) func(T, T) int {
    19  	return func(e1, e2 T) int {
    20  		return -op.Compare(converter(e1), converter(e2))
    21  	}
    22  }