Skip to content

Commit

Permalink
remove private compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Apr 20, 2024
1 parent 0cd3a66 commit f2559f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
10 changes: 3 additions & 7 deletions compare.go
Expand Up @@ -9,10 +9,6 @@ import (
// The result will be 0 if l == r, -1 if l < r, and +1 if l > r.
// This comparison is used by built-in operators and functions.
func Compare(l, r any) int {
return compare(l, r)
}

func compare(l, r any) int {
return binopTypeSwitch(l, r,
compareInt,
func(l, r float64) any {
Expand Down Expand Up @@ -44,19 +40,19 @@ func compare(l, r any) int {
n = len(r)
}
for i := 0; i < n; i++ {
if cmp := compare(l[i], r[i]); cmp != 0 {
if cmp := Compare(l[i], r[i]); cmp != 0 {
return cmp
}
}
return compareInt(len(l), len(r))
},
func(l, r map[string]any) any {
lk, rk := funcKeys(l), funcKeys(r)
if cmp := compare(lk, rk); cmp != 0 {
if cmp := Compare(lk, rk); cmp != 0 {
return cmp
}
for _, k := range lk.([]any) {
if cmp := compare(l[k.(string)], r[k.(string)]); cmp != 0 {
if cmp := Compare(l[k.(string)], r[k.(string)]); cmp != 0 {
return cmp
}
}
Expand Down
20 changes: 10 additions & 10 deletions func.go
Expand Up @@ -602,7 +602,7 @@ func indices(vs, xs []any) any {
return rs
}
for i := 0; i <= len(vs)-len(xs); i++ {
if compare(vs[i:i+len(xs)], xs) == 0 {
if Compare(vs[i:i+len(xs)], xs) == 0 {
rs = append(rs, i)
}
}
Expand All @@ -615,7 +615,7 @@ func funcIndex(v, x any) any {
return nil
}
for i := 0; i <= len(vs)-len(xs); i++ {
if compare(vs[i:i+len(xs)], xs) == 0 {
if Compare(vs[i:i+len(xs)], xs) == 0 {
return i
}
}
Expand All @@ -629,7 +629,7 @@ func funcRindex(v, x any) any {
return nil
}
for i := len(vs) - len(xs); i >= 0; i-- {
if compare(vs[i:i+len(xs)], xs) == 0 {
if Compare(vs[i:i+len(xs)], xs) == 0 {
return i
}
}
Expand Down Expand Up @@ -1188,7 +1188,7 @@ type rangeIter struct {
}

func (iter *rangeIter) Next() (any, bool) {
if compare(iter.step, 0)*compare(iter.value, iter.end) >= 0 {
if Compare(iter.step, 0)*Compare(iter.value, iter.end) >= 0 {
return nil, false
}
v := iter.value
Expand Down Expand Up @@ -1259,7 +1259,7 @@ func minMaxBy(vs, xs []any, isMin bool) any {
}
i, j, x := 0, 0, xs[0]
for i++; i < len(xs); i++ {
if compare(x, xs[i]) > 0 == isMin {
if Compare(x, xs[i]) > 0 == isMin {
j, x = i, xs[i]
}
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func sortItems(name string, v, x any) ([]*sortItem, error) {
items[i] = &sortItem{v, xs[i]}
}
sort.SliceStable(items, func(i, j int) bool {
return compare(items[i].key, items[j].key) < 0
return Compare(items[i].key, items[j].key) < 0
})
return items, nil
}
Expand Down Expand Up @@ -1323,7 +1323,7 @@ func funcGroupBy(v, x any) any {
rs := []any{}
var last any
for i, r := range items {
if i == 0 || compare(last, r.key) != 0 {
if i == 0 || Compare(last, r.key) != 0 {
rs, last = append(rs, []any{r.value}), r.key
} else {
rs[len(rs)-1] = append(rs[len(rs)-1].([]any), r.value)
Expand All @@ -1348,7 +1348,7 @@ func uniqueBy(name string, v, x any) any {
rs := []any{}
var last any
for i, r := range items {
if i == 0 || compare(last, r.key) != 0 {
if i == 0 || Compare(last, r.key) != 0 {
rs, last = append(rs, r.value), r.key
}
}
Expand Down Expand Up @@ -1826,9 +1826,9 @@ func funcBsearch(v, t any) any {
return &func1TypeError{"bsearch", v, t}
}
i := sort.Search(len(vs), func(i int) bool {
return compare(vs[i], t) >= 0
return Compare(vs[i], t) >= 0
})
if i < len(vs) && compare(vs[i], t) == 0 {
if i < len(vs) && Compare(vs[i], t) == 0 {
return i
}
return -i - 1
Expand Down
14 changes: 7 additions & 7 deletions operator.go
Expand Up @@ -371,7 +371,7 @@ func funcOpSub(_, l, r any) any {
L:
for _, l := range l {
for _, r := range r {
if compare(l, r) == 0 {
if Compare(l, r) == 0 {
continue L
}
}
Expand Down Expand Up @@ -525,25 +525,25 @@ func funcOpAlt(_, l, r any) any {
}

func funcOpEq(_, l, r any) any {
return compare(l, r) == 0
return Compare(l, r) == 0
}

func funcOpNe(_, l, r any) any {
return compare(l, r) != 0
return Compare(l, r) != 0
}

func funcOpGt(_, l, r any) any {
return compare(l, r) > 0
return Compare(l, r) > 0
}

func funcOpLt(_, l, r any) any {
return compare(l, r) < 0
return Compare(l, r) < 0
}

func funcOpGe(_, l, r any) any {
return compare(l, r) >= 0
return Compare(l, r) >= 0
}

func funcOpLe(_, l, r any) any {
return compare(l, r) <= 0
return Compare(l, r) <= 0
}

0 comments on commit f2559f6

Please sign in to comment.