Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C++ STL-like algorithms as built in internal standard library for Go 2.0 #42972

Closed
kokizzu opened this issue Dec 3, 2020 · 3 comments
Closed

Comments

@kokizzu
Copy link

kokizzu commented Dec 3, 2020

lately I've been using golang a lot for competitive programming, sometimes uses ruby (because of their awesome built-in combinatorial/permutation/prime/automatic-bignumber library). It would be nice if I could fully ditch C++ (especially the lower_bound/upper_bound/next_permutation function) by having Go implement those functions inside standard library as go 2.0 released, something like this:

// same as C++ STL's lower_bound, upper_bound
pos := slice.LowerBound(arr interface{},cmpFunc func(a,b int)int) // cmpFunc returns -1, 0 or +1
pos := slice.UpperBound(arr interface{},cmpFunc func(a,b int)int)

// similar to lower_bound, upper_bound but return nearest value from sorted slice, eg.
// for example: [1,4,5,6,10] 
// when finding 3 NearestLowerBound = 0 (value=1) NearestUpperBound = 1 (value=4)
// when finding 9 NearestLowerBound = 4 (value=6) NearestUpperBound = 5 (value=10)
pos := slice.NearestLowerBound(arr interface{},cmpFunc func(a,b int)int)
pos := slice.NearestUpperBound(arr interface{},cmpFunc func(a,b int)int)

// same as C++ STL's next_permutation
haveNext := slice.NextPermutation(arr sort.Interface) 

this surely will increase Go's adoption and attract more competitive programmers.
Why as Go 2.0, because some of the competitive programming/online judges still using old version of Go, so when they decided to upgrade to Go 2.0, batteries already included:

@ianlancetaylor
Copy link
Contributor

Currently we are hoping to add some form of generics to Go, in which case people will be able to write these functions themselves. For the current design draft, see https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md. I'm going to close this issue in favor of adding generics. We can revisit if generics wind up not being adopted.

@randall77
Copy link
Contributor

Have you looked at sort.Search? Why does that not work for you for your Nearest* functions?

@kokizzu
Copy link
Author

kokizzu commented Dec 3, 2020

Have you looked at sort.Search? Why does that not work for you for your Nearest* functions?

Whoa nice, never knew this function before, thanks a lot
https://golang.org/pkg/sort/#Search

@golang golang locked and limited conversation to collaborators Dec 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants