doug-moen / openscad2

better abstraction mechanisms for OpenSCAD
Boost Software License 1.0
25 stars 3 forks source link

Ranges #16

Open Neon22 opened 9 years ago

Neon22 commented 9 years ago

I'd like to suggest you consider using Python's approach to range instead of having an explicit range syntax.

In Python they use the range(start, end, step) function to return a list which can then be iterated on or sliced. This has the advantage of specifically returning a sequence.

Python's range function also has an optional start. so range(5) returns [0,1,2,3,4] Python's range is integer only. However the OpenSCAD2 version probably should not be.

The reason for the suggestion is readability. Primarlily for the step case. Having the step at the end, as an optional third argument, makes it more readable than an optional second argument with a new comma instead of the .. syntax.

doug-moen commented 9 years ago

The current OpenSCAD range syntax is [start:step:end]. You propose to replace this with range(start,end,step), changing the order of the arguments. You claim that this is more readable for the step case. I disagree. The problem with both syntaxes is that it is not obvious which of the three arguments is the step, and supporting two syntaxes with the arguments in different orders just makes things worse. The inconsistency would be very confusing for existing OpenSCAD users.

If we change your proposal to use range(start,step,end), then the syntax will be very confusing to Python users, because the arguments would be in an unexpected order.

doug-moen commented 9 years ago

One more comment. With my proposed range syntax, [2,4..10] is the sequence of all even integers from 2 to 10. It's very similar to set builder notation from high school math, where you would write {2,4,...,10}. I guess what I don't understand is why you think this notation is unclear, and why you think that range(2,10,2) is clearer. Can you explain your reasoning, other than "that's the way Python works"? Would my proposed range syntax be clearer if the notation was [2,4,...,10]?

Neon22 commented 9 years ago

The confusion (to me) lies in which is clearer. I can't find any set builder notation that matches your sequence example (see link in other issue). I don't mean anything personal by this. its an unknown notation to me and I'm totally unfamiliar with it.

so [2,10] or [2..10] seems clear [2,10,2] given the order was [start, stop] for a pair. implies the last number means something new whereas [2,2,10] has changed the meaning of teh second argument. but of course [2,4..10] is clear even if it means I have to do the math in my head to work out what the step is. [13,27..158] means I have to work out the step size is 14 and whether it'll hit 158 exactly or not. As before - I'm not being argumentative - I'd just like you to consider if you need a range syntax vs a function that returns a list.