[Microbit-Python] Interval boundaries... what did we decide?

Larry Hastings larry at hastings.org
Mon Oct 12 10:30:49 CEST 2015


On 10/12/2015 01:12 AM, Mark Shannon wrote:
>
> There is another way to express a range: (start, length).
> For example, I have followed the DAL API for Image.crop() which is 
> Image.crop(x, y, width, height).
> I think that API is easier to understand (if a little clumsier in 
> practice) than Image.crop(xstart, ystart, xend, yend).
>
> If I recall correctly, we also decided to avoid using square brackets 
> in the API, which makes consistency with list[], a bit less important.
>
> If you do need a range, however, it should definitely be left-closed, 
> right-open.

Consistency with Python slicing is not important, perhaps, but 
microbit.random(x) returns a number [0, x), and of course we have 
range(x) iterating over the interval [0, x).  It's all over the place in 
Python, and it's snuck into MicroPython too, and I just wanted to 
confirm that we had our story straight and we weren't trying to hide 
this detail from the kids for fear of confusing them.

I thought I remembered a conversation where we said "ranges should 
include both endpoints as that's less confusing for kids".  But it 
sounds like we're all in agreement: it should all be left-closed 
right-open intervals.  (Anyway, surely it'd be /more/ confusing if some 
functions took left-closed right-open intervals and others took closed 
intervals?)

My accelerator-scaler device takes two ranges: the start & end for the 
input (from the accelerometer), and the start & end for the output (what 
will be returned).  Therefore this:
     scaler = microbit.game.Scaler(-512, 512, 0, 5)
should say "take the input from [512, 512) and map it to the range [0, 
5)".  As in, it will ignore inputs less than -512 and greater than 511, 
and will return 0, 1, 2, 3, or 4, but never -1 or 5.


Yes?  Your lack of reply will be interpreted as silent assent.


//arry/

p.s. (x, y, width, height) is pretty common for image APIs.  You see 
(xstart, ystart, xend, yend) now and then too but it's less common. And 
yes I generally prefer the former.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/mailman/private/microbit/attachments/20151012/82b66605/attachment-0001.html>


More information about the Microbit mailing list