Bring back slices.BinarySearchFunc with a predicate

81 views
Skip to first unread message

Sudhir Jonathan

unread,
Apr 4, 2022, 7:08:36 PM4/4/22
to golang-nuts
The earlier method signature for slices.BinarySearchFunc was very useful:

It was of the form 
```
slices.BinarySearchFunc(array, func(e T) bool)
```
which returned the smallest index at which predicate would return true. 

This was incredibly useful to do inequality operation binary searches  - like find the smallest index in a sorted slice where the element was greater than this (helps figure out the index of the last repeat of an element in the sorted slice). 

I understand the new method is true-er to its name, but can we bring the old one back as well, maybe as slices.BinarySearchPredicate ?

Axel Wagner

unread,
Apr 5, 2022, 1:27:46 AM4/5/22
to golang-nuts
Hm I must say, I also don't really understand removing the predicate-based one. It is easy, when you have a comparison function, to get to a predicate:
pred := func(a, b T) bool { return cmp(a, b) >= 0 }
But not the other way around, so a predicate is more general.

I used the predicate version of `sort.Search` to look for prefix-matches in a sorted slice of paths. i.e. I was not looking for an equality match of the needle, but also something of which the needle is a prefix.

When I saw #50340 fly by, I assumed this would be about adding a simpler version of `sort.Search` to make the common use-case simpler, which was very much fine with me, so I didn't follow that discussion. I would've spoken up sooner, if I expected the more general version to disappear.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f5f8c4a8-56da-4f00-a43b-20015828fdb5n%40googlegroups.com.

Axel Wagner

unread,
Apr 5, 2022, 1:32:08 AM4/5/22
to golang-nuts
Sorry, the predicate should be

var b T
pred := func(a T) bool { return cmp(a, b) >= 0 }
Reply all
Reply to author
Forward
0 new messages