[ 
https://issues.apache.org/jira/browse/GROOVY-11649?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17949580#comment-17949580
 ] 

ASF GitHub Bot commented on GROOVY-11649:
-----------------------------------------

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.




> Create partitionPoint extension method variants
> -----------------------------------------------
>
>                 Key: GROOVY-11649
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11649
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 5.x
>
>
> See: https://github.com/apache/groovy/pull/2210
> * rust: 
> https://doc.rust-lang.org/std/primitive.slice.html#method.partition_point
> * c++: https://en.cppreference.com/w/cpp/algorithm/ranges/partition_point
> * python: https://docs.python.org/3/library/bisect.html#module-bisect



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to