[squeak-dev] The Inbox: Tools-ul.378.mcz

Juan Vuletich juan at jvuletich.org
Thu Sep 8 22:23:48 UTC 2011


Eliot Miranda wrote:
>
>
> On Thu, Sep 8, 2011 at 2:18 PM, Levente Uzonyi <leves at elte.hu 
> <mailto:leves at elte.hu>> wrote:
>
>     On Thu, 8 Sep 2011, Eliot Miranda wrote:
>
>         Levente,  I object :)  I find the findBinary:do:ifNone:
>         extremely ugly and
>         much prefer the findNearbyBinaryIndex: usage.  Would you mind
>         if we reverted
>         to that?
>
>
>     I pushed it to the inbox, so there's nothing to revert. 
>
>
> Ah, sorry.  Saw the commit notice and assumed.  Apologies.
>  
>
>     I prefer to have as few implementations of binary search as possible.
>
>
> Yes, but findNearbyBinaryIndex: pays its way.  It saved 11 lines and 
> is way more comprehensible.
>  
>
>     Did you check the other changes? Are those acceptable?
>
>
> I'm mulling them over :)  I like a variable for the context pc (which 
> Henrik didn't suggest) and I like using an at:ifAbsent: block.  So 
> I'll hopefully submit a new version sometime soon.
>
>
>
>     Levente
>
>     P.S.: I just saw that Henrik wrote a mail about this method on the
>     Pharo list with some other simplifications.
>
>
> -- 
> best,
> Eliot

You can also write something like

    sortedSourceMap
        findBinaryIndex: [ :assoc | pc - assoc key ]
        do: [ :i | ^(sortedSourceMap at: i) value ]
        ifNone: [ :lower :upper |
            lower = 0 ifTrue: [ ^1 to: 0 ].
            upper <= sortedSourceMap size ifTrue: [
                ^(sortedSourceMap at: upper) value ].    "No match, but 
a nearby element."
            end := sortedSourceMap detectMax: [ :each |
                each value last ].
            ^end + 1 to: end ]

that is only 3 lines longer, doesn't require a new binary search method, 
and looks clearer than the other 2 options to me.

Cheers,
Juan Vuletich



More information about the Squeak-dev mailing list