paulk-asert commented on PR #2210:
URL: https://github.com/apache/groovy/pull/2210#issuecomment-2852386998

   I was thinking a bit more about usage overnight.
   
   For arrays, the "clean insertion" point argument is less compelling but 
somewhat useful none-the-less.
   
   Given that it isn't easy to partition primitive arrays, I think 
partitionPoint is slightly less compelling. We could get by with both 
bisectLeft and bisectRight for instance. But even for natural ordering 
scenarios, the convenience of a single method is nicer than the two separate 
methods in my view.
   
   I looked at the grades example from Python bisect: 
https://docs.python.org/3/library/bisect.html#examples
   
   ```
   def scores = [33, 99, 77, 70, 89, 90, 100] // convert these to A grade (90 
and above),
                                              // B grade (80 up to but not 
including 90), etc.
   ```
   
   With binary search we have something like this:
   
   ```
   def grade(score) {
     int[] breakpoints = [59, 69, 79, 89]
     def idx = Arrays.binarySearch(breakpoints, score)
     if (idx < 0) {
       idx = (idx + 1).abs()
     }
     'FDCBA'[idx]
   }
   assert scores.collect(this.&grade) == ['F', 'A', 'C', 'C', 'B', 'A', 'A']
   ```
   
   We have to artificially subtract one from each of the breakpoints compared 
to the Python bisect example.
    
   With partitionPoint it would become:
   
   ```
   int[] breakpoints = [60, 70, 80, 90]
   assert scores2.collect { sc ->
       'FDCBA'[breakpoints.partitionPoint{ it <= sc }]
   } == ['F', 'A', 'C', 'C', 'B', 'A', 'A']
   
   ```
   This is much cleaner.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@groovy.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to