discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Proposal for range ( [i:j:k] ) improvement

R
runsun
Thu, Jul 16, 2015 7:13 AM

Current range syntax:

 [ i: j ]
 [ i: j: k ]

Proposed additional syntax:

 a) [ :j ] ---- i can be skipped if i==0

 b) [ i: array ]  ---- j can be an array or string . For example: 
    
     arr = ["a","b","c","d"];
     [ for ( i=

[ :arr ]

) i ]; ==> [0,1,2,3]
[ for ( i=

[ :2:arr ]

) i ]; ==> [0,2]

 c)  ^ array  (or some other syntax like [arr;] [ %arr ] etc ... )
     to give both index and item:

     [ for 

( i,x = ^arr )

str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ]

Compare the proposed

*for ( i=[:a] ) *

to the current usage

for ( i=[0:len(a)-1] )

it is much quicker and easier to write and read.


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Current range syntax: > [ i: j ] > [ i: j: k ] Proposed additional syntax: > a) [ :j ] ---- i can be skipped if i==0 > > b) [ i: array ] ---- j can be an array or string . For example: > > arr = ["a","b","c","d"]; > [ for ( i= * > [ :arr ] * > ) i ]; ==> [0,1,2,3] > [ for ( i= * > [ :2:arr ] * > ) i ]; ==> [0,2] > > c) ^ array (or some other syntax like [arr;] [ %arr ] etc ... ) > to give both index and item: > > [ for * > ( i,x = ^arr ) * > str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ] Compare the proposed *for ( i=[:a] ) * to the current usage *for ( i=[0:len(a)-1] )* it is much quicker and easier to write and read. ----- $ Runsun Pan, PhD $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner $ -- hash parameter model: here , here $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184.html Sent from the OpenSCAD mailing list archive at Nabble.com.
Q
QuackingPlums
Thu, Jul 16, 2015 7:35 AM

Sounds like we could do with a foreach statement:

foreach (i in array)
doSomething();

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13185.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Sounds like we could do with a foreach statement: *foreach* (i *in* array) doSomething(); -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13185.html Sent from the OpenSCAD mailing list archive at Nabble.com.
CL
Chow Loong Jin
Thu, Jul 16, 2015 7:38 AM

On Thu, Jul 16, 2015 at 12:35:04AM -0700, QuackingPlums wrote:

Sounds like we could do with a foreach statement:

foreach (i in array)
doSomething();

We already have such a thing:

for (v = array)
doSomething ();

You can't use this when you actually need the array index though.

--
Kind regards,
Loong Jin

On Thu, Jul 16, 2015 at 12:35:04AM -0700, QuackingPlums wrote: > Sounds like we could do with a foreach statement: > > *foreach* (i *in* array) > doSomething(); We already have such a thing: for (v = array) doSomething (); You can't use this when you actually need the array index though. -- Kind regards, Loong Jin
Q
QuackingPlums
Thu, Jul 16, 2015 8:25 AM

Chow Loong Jin wrote

We already have such a thing:

for (v = array)
doSomething ();

I did not know that! How long has this form been supported?

Chow Loong Jin wrote

You can't use this when you actually need the array index though.

True. I can't resist the temptation to slip into programming good practice
at this point, despite (I know, I know!) the fact that OpenSCAD isn't a
programming language.

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13188.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Chow Loong Jin wrote > We already have such a thing: > > for (v = array) > doSomething (); I did not know that! How long has this form been supported? Chow Loong Jin wrote > You can't use this when you actually need the array index though. True. I can't resist the temptation to slip into programming good practice at this point, despite (I know, I know!) the fact that OpenSCAD isn't a programming language. -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13188.html Sent from the OpenSCAD mailing list archive at Nabble.com.
CL
Chow Loong Jin
Thu, Jul 16, 2015 8:41 AM

On Thu, Jul 16, 2015 at 01:25:48AM -0700, QuackingPlums wrote:

Chow Loong Jin wrote

We already have such a thing:

for (v = array)
doSomething ();

I did not know that! How long has this form been supported?

I dunno, I first started using OpenSCAD with 2014.04, and it already had that
feature.

Chow Loong Jin wrote

You can't use this when you actually need the array index though.

True. I can't resist the temptation to slip into programming good practice
at this point, despite (I know, I know!) the fact that OpenSCAD isn't a
programming language.

Well, there are still some good practices to follow, such as not hardcoding
precalculated values everywhere.

--
Kind regards,
Loong Jin

On Thu, Jul 16, 2015 at 01:25:48AM -0700, QuackingPlums wrote: > Chow Loong Jin wrote > > We already have such a thing: > > > > for (v = array) > > doSomething (); > > I did not know that! How long has this form been supported? I dunno, I first started using OpenSCAD with 2014.04, and it already had that feature. > Chow Loong Jin wrote > > You can't use this when you actually need the array index though. > > True. I can't resist the temptation to slip into programming good practice > at this point, despite (I know, I know!) the fact that OpenSCAD isn't a > programming language. Well, there are still some good practices to follow, such as not hardcoding precalculated values everywhere. -- Kind regards, Loong Jin
N
Neon22
Thu, Jul 16, 2015 12:00 PM

we can get both:

Going back to @runsun proposal:
Current range syntax:

    [ i: j ]
    [ i: j: k ]

Proposed additional syntax:

    a) [ :j ] ---- i can be skipped if i==0

    b) [ i: array ]  ---- j can be an array or string . For example:
       
        arr = ["a","b","c","d"];
        [ for ( i=[ :arr ] ) i ]; ==> [0,1,2,3]
        [ for ( i=[ :2:arr ] ) i ]; ==> [0,2]    

    c)  ^ array  (or some other syntax like [arr;] [ %arr ] etc ... )
        to give both index and item:

        [ for ( i,x = ^arr ) str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ]
... [show rest of quote]

Proposed to return just the array indices. Not the values.

current:

Proposed to return two values index and value. (i=index, x=value).

current (where the value is a new variable in this scope and not  reused).

I note that in Python there is a special function "enumerate" that returns a
tuple of (index, value). Of course this works in Python because tuples are
consts of arrays and can be accessed in a loop like this:

which visually indicates that the enumerate will return two values and how
they will be mapped to i and x.
Personally I find Python's slicing, range, tuple, array, dictionary design
to be very clean, clear and well designed.

For OpenSCAD I liekd someone's proposal to use "in" to explicitly indicate
that the argument was an array.
E.g.

Maybe this variant coudl be used to differntiate between indices or values
from the array ?

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13194.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

we can get both: Going back to @runsun proposal: Current range syntax: [ i: j ] [ i: j: k ] Proposed additional syntax: a) [ :j ] ---- i can be skipped if i==0 b) [ i: array ] ---- j can be an array or string . For example: arr = ["a","b","c","d"]; [ for ( i=[ :arr ] ) i ]; ==> [0,1,2,3] [ for ( i=[ :2:arr ] ) i ]; ==> [0,2] c) ^ array (or some other syntax like [arr;] [ %arr ] etc ... ) to give both index and item: [ for ( i,x = ^arr ) str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ] ... [show rest of quote] Proposed to return just the array indices. Not the values. current: Proposed to return two values index and value. (i=index, x=value). current (where the value is a new variable in this scope and not reused). I note that in Python there is a special function "enumerate" that returns a tuple of (index, value). Of course this works in Python because tuples are consts of arrays and can be accessed in a loop like this: which visually indicates that the enumerate will return two values and how they will be mapped to i and x. Personally I find Python's slicing, range, tuple, array, dictionary design to be very clean, clear and well designed. For OpenSCAD I liekd someone's proposal to use "in" to explicitly indicate that the argument was an array. E.g. Maybe this variant coudl be used to differntiate between indices or values from the array ? -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13194.html Sent from the OpenSCAD mailing list archive at Nabble.com.
R
runsun
Thu, Jul 16, 2015 12:50 PM

Neon22 wrote

I note that in Python there is a special function "enumerate" that returns
a tuple of (index, value). Of course this works in Python because tuples
are consts of arrays and can be accessed in a loop like this:

which visually indicates that the enumerate will return two values and how
they will be mapped to i and x.
Personally I find Python's slicing, range, tuple, array, dictionary design
to be very clean, clear and well designed.

Part of my idea is indeed inspired by Python. Another slicing feature of
python that I like is the reversed indexing:

IMHO It's one of the features that earn Python the reputation of "elegant".
It'd certainly be nice if we can adopt this, too.

For OpenSCAD I liekd someone's proposal to use "in" to explicitly indicate
that the argument was an array.
E.g.

Maybe this variant coudl be used to differntiate between indices or values
from the array ?

Wouldn't it be a little confusing, 'cos the index doesn't actually "in" the
arr ?


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13196.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Neon22 wrote > I note that in Python there is a special function "enumerate" that returns > a tuple of (index, value). Of course this works in Python because tuples > are consts of arrays and can be accessed in a loop like this: > which visually indicates that the enumerate will return two values and how > they will be mapped to i and x. > Personally I find Python's slicing, range, tuple, array, dictionary design > to be very clean, clear and well designed. Part of my idea is indeed inspired by Python. Another slicing feature of python that I like is the reversed indexing: IMHO It's one of the features that earn Python the reputation of "elegant". It'd certainly be nice if we can adopt this, too. > For OpenSCAD I liekd someone's proposal to use "in" to explicitly indicate > that the argument was an array. > E.g. > Maybe this variant coudl be used to differntiate between indices or values > from the array ? Wouldn't it be a little confusing, 'cos the index doesn't actually "in" the arr ? ----- $ Runsun Pan, PhD $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner $ -- hash parameter model: here , here $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13196.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Jul 17, 2015 1:22 AM

Proposed additional syntax:

 a) [ :j ] ---- i can be skipped if i==0

Just lazy. ;)

Compare the proposed

for ( i=[:a] )

to the current usage

for ( i=[0:len(a)-1] )

it is much quicker and easier to write and read.

I don't mind that, where you still use 0:, so where 'a' is a string or
vector, its len()-1 is used.

for (i=[0:vector]) ...


Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/

View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13204.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Proposed additional syntax: > a) [ :j ] ---- i can be skipped if i==0 Just lazy. ;) > Compare the proposed > > * > for ( i=[:a] ) * > > to the current usage > > * > for ( i=[0:len(a)-1] ) * > > it is much quicker and easier to write and read. I don't mind that, where you still use 0:, so where 'a' is a string or vector, its len()-1 is used. for (i=[0:vector]) ... ----- Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13204.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Fri, Jul 17, 2015 5:49 PM

Runsun said:
Compare the proposed

*for ( i=[:a] ) *

to the current usage

for ( i=[0:len(a)-1] )

it is much quicker and easier to write and read.

I don't mind if an operation for returning the indices of an array is added
to OpenSCAD.
However, the syntax [:a] is pretty inscrutable, and I'm bit disturbed that
the array a is used in an integer context and interpreted as meaning
'len(a)-1'. That's weird.

How about an ordinary function instead?
In Perl, this is 'keys(a)'.
I can't remember seeing this operation in other languages.
However, 'indices(a)' or 'indexes(a)' would also make sense.

for( i = indices(a) ) ...

Runsun also said:

     [ for ( i,x = ^arr ) str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ]

I'm not seeing the value of this. I think it's clearer to write
[ for ( i = arr ) str( i,"=",arr[i]) ]

In the case where 'arr' is a really long expression, you can do this:
[ let(a=arr) for(i=a) str(i,"=",a[i]) ]

Doug.

On 16 July 2015 at 03:13, runsun runsun@gmail.com wrote:

Current range syntax:

 [ i: j ]
 [ i: j: k ]

Proposed additional syntax:

 a) [ :j ] ---- i can be skipped if i==0

 b) [ i: array ]  ---- j can be an array or string . For example:

     arr = ["a","b","c","d"];
     [ for ( i=

[ :arr ]

) i ]; ==> [0,1,2,3]
[ for ( i=

[ :2:arr ]

) i ]; ==> [0,2]

 c)  ^ array  (or some other syntax like [arr;] [ %arr ] etc ... )
     to give both index and item:

     [ for

( i,x = ^arr )

str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ]

Compare the proposed

*for ( i=[:a] ) *

to the current usage

*for ( i=[0:len(a)-1] )*

it is much quicker and easier to write and read.


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context:
http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Runsun said: Compare the proposed *for ( i=[:a] ) * to the current usage *for ( i=[0:len(a)-1] )* it is much quicker and easier to write and read. I don't mind if an operation for returning the indices of an array is added to OpenSCAD. However, the syntax [:a] is pretty inscrutable, and I'm bit disturbed that the array a is used in an integer context and interpreted as meaning 'len(a)-1'. That's weird. How about an ordinary function instead? In Perl, this is 'keys(a)'. I can't remember seeing this operation in other languages. However, 'indices(a)' or 'indexes(a)' would also make sense. for( i = indices(a) ) ... Runsun also said: > [ for ( i,x = ^arr ) str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ] I'm not seeing the value of this. I think it's clearer to write [ for ( i = arr ) str( i,"=",arr[i]) ] In the case where 'arr' is a really long expression, you can do this: [ let(a=arr) for(i=a) str(i,"=",a[i]) ] Doug. On 16 July 2015 at 03:13, runsun <runsun@gmail.com> wrote: > Current range syntax: > > > > [ i: j ] > > [ i: j: k ] > > Proposed additional syntax: > > > > a) [ :j ] ---- i can be skipped if i==0 > > > > b) [ i: array ] ---- j can be an array or string . For example: > > > > arr = ["a","b","c","d"]; > > [ for ( i= > * > > [ :arr ] > * > > ) i ]; ==> [0,1,2,3] > > [ for ( i= > * > > [ :2:arr ] > * > > ) i ]; ==> [0,2] > > > > c) ^ array (or some other syntax like [arr;] [ %arr ] etc ... ) > > to give both index and item: > > > > [ for > * > > ( i,x = ^arr ) > * > > str( i,"=",x) ] ==> [ "0=a", "1=b", "2=c" ] > > Compare the proposed > > *for ( i=[:a] ) * > > to the current usage > > *for ( i=[0:len(a)-1] )* > > it is much quicker and easier to write and read. > > > > > > > ----- > > $ Runsun Pan, PhD > > $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner > > $ -- hash parameter model: here , here > > $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly > > > > > -- > View this message in context: > http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
R
runsun
Fri, Jul 17, 2015 6:24 PM

doug.moen wrote

How about an ordinary function instead?
In Perl, this is 'keys(a)'.
I can't remember seeing this operation in other languages.
However, 'indices(a)' or 'indexes(a)' would also make sense.

for( i = indices(a) ) ...

In fact, since we don't have this [:arr] yet, a function is exactly what
I do in my own lib. Here is the test output of my range(...) function:

range ( i,j,k,cycle,returnitems )=array ( tested:54 ) (mode:12)
| // 1 arg: starting from 0
|
| 0> range(5)= [0, 1, 2, 3, 4]
| 1> range(-5)= [0, -1, -2, -3, -4]
| 2> range(0)= []
| 3> range([])= []
|
| // 2 args:
|
| 4> range(2,5)= [2, 3, 4]
| 5> range(5,2)= [5, 4, 3]
| 6> range(-2,1)= [-2, -1, 0]
| 7> range(-5,-1)= [-5, -4, -3, -2]
|
| // Note these are the same:
|
| 8> range(3)= [0, 1, 2]
| 9> range(0,3)= [0, 1, 2]
| 10> range(-3)= [0, -1, -2]
| 11> range(0,-3)= [0, -1, -2]
| 12> range(1)= [0]
| 13> range(-1)= [0]
|
| // 3 args, the middle one is interval. Its sign has no effect
|
| 14> range(2,0.5,5)= [2, 2.5, 3, 3.5, 4, 4.5]
| 15> range(2,-0.5,5)= [2, 2.5, 3, 3.5, 4, 4.5]
| 16> range(5,-1,0)= [5, 4, 3, 2, 1]
| 17> range(5,1,2)= [5, 4, 3]
| 18> range(8,2,0)= [8, 6, 4, 2]
| 19> range(0,2,8)= [0, 2, 4, 6]
| 20> range(0,3,8)= [0, 3, 6]
|
| // Extreme cases:
|
| 21> range()= [] // Give nothing, get nothing
| 22> range(0)= [] // Count from 0 to 0 gets you nothing
| 23> range(2,2)= [] // 2 to 2 gets you nothing, either
| 24> range(2,0,4)= [] // No intv gets you nothing, too
| 25> range(0,1)= [0]
| 26> range(0,1,1)= [0]
| // When interval > range, count only the first:
| 27> range(2,5,4)= [2]
| 28> range(2,5,-1)= [2]
| 29> range(-2,5,-4)= [-2]
| 30> range(-2,5,1)= [-2]
|
|  range( obj )
|
| // range( arr ) or range( str ) to return a range of an array
| // so you don"t have to do : range( len(arr) )
|
| > obj = [10, 11, 12, 13]
|
| 31> range(obj)= [0, 1, 2, 3]
| 32> range([3,4,5])= [0, 1, 2]
| 33> range("345")= [0, 1, 2]
| 34> range([])= []
| 35> range(")= []
|
|  range( ... cycle= i,-i,true...)
|
| // New 2014.9.7:
| // cycle= +i: extend the index on the right by +i count
| // cycle= -i: extend the index on the right by -i count
| // cycle= true: same as cycle=1
|
| 36> range(4,cycle=1)= [0, 1, 2, 3, 0] // Add first to the right
| 37> range(4,cycle=-1)= [3, 0, 1, 2, 3] // Add last to the left
| 38> range(4,cycle=true)= [0, 1, 2, 3, 0] // true=1
|
| 39> range(2,5,cycle=1)= [2, 3, 4, 2] // Add first to the right
| 40> range(2,5,cycle=2)= [2, 3, 4, 2, 3] // Add 1st,2nd to the right
| 41> range(2,5,cycle=-1)= [4, 2, 3, 4] // Add last to the left
| 42> range(2,5,cycle=-2)= [3, 4, 2, 3, 4]
| 43> range(2,5,cycle=true)= [2, 3, 4, 2] // true=1
|
| 44> range(2,1.5,8,cycle=2)= [2, 3.5, 5, 6.5, 2, 3.5]
| 45> range(2,1.5,8,cycle=-2)= [5, 6.5, 2, 3.5, 5, 6.5]
| 46> range(2,1.5,8,cycle=true)= [2, 3.5, 5, 6.5, 2]
|
|  range(obj,cycle=i,-1,[-1,1],true...)
|
| > obj = [10, 11, 12, 13]
| 47> range(obj,cycle=1)= [0, 1, 2, 3, 0]
| 48> range(obj,cycle=[-1,1])= [3, 0, 1, 2, 3, 0]
| 49> range(obj,cycle=2)= [0, 1, 2, 3, 0, 1]
| 50> range(obj,cycle=-1)= [3, 0, 1, 2, 3]
| 51> range(obj,cycle=-2)= [2, 3, 0, 1, 2, 3]
|
|  range( obj, returnitems=true,false )
|
| // When using obj, can set returnitems=true to return items.
|
| 52> range([3,4,5],returnitems=true)= [3, 4, 5]
| 53> range(obj,cycle=[-1,1],returnitems=true)= [13, 10, 11, 12, 13, 10]


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13212.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

doug.moen wrote > How about an ordinary function instead? > In Perl, this is 'keys(a)'. > I can't remember seeing this operation in other languages. > However, 'indices(a)' or 'indexes(a)' would also make sense. > > for( i = indices(a) ) ... In fact, since we don't have this [:arr] yet, a function is exactly what I do in my own lib. Here is the test output of my *range(...)* function: > range ( i,j,k,cycle,returnitems )=array ( tested:54 ) (mode:12) > | // 1 arg: starting from 0 > | > | 0> range(5)= [0, 1, 2, 3, 4] > | 1> range(-5)= [0, -1, -2, -3, -4] > | 2> range(0)= [] > | 3> range([])= [] > | > | // 2 args: > | > | 4> range(2,5)= [2, 3, 4] > | 5> range(5,2)= [5, 4, 3] > | 6> range(-2,1)= [-2, -1, 0] > | 7> range(-5,-1)= [-5, -4, -3, -2] > | > | // Note these are the same: > | > | 8> range(3)= [0, 1, 2] > | 9> range(0,3)= [0, 1, 2] > | 10> range(-3)= [0, -1, -2] > | 11> range(0,-3)= [0, -1, -2] > | 12> range(1)= [0] > | 13> range(-1)= [0] > | > | // 3 args, the middle one is interval. Its sign has no effect > | > | 14> range(2,0.5,5)= [2, 2.5, 3, 3.5, 4, 4.5] > | 15> range(2,-0.5,5)= [2, 2.5, 3, 3.5, 4, 4.5] > | 16> range(5,-1,0)= [5, 4, 3, 2, 1] > | 17> range(5,1,2)= [5, 4, 3] > | 18> range(8,2,0)= [8, 6, 4, 2] > | 19> range(0,2,8)= [0, 2, 4, 6] > | 20> range(0,3,8)= [0, 3, 6] > | > | // Extreme cases: > | > | 21> range()= [] // Give nothing, get nothing > | 22> range(0)= [] // Count from 0 to 0 gets you nothing > | 23> range(2,2)= [] // 2 to 2 gets you nothing, either > | 24> range(2,0,4)= [] // No intv gets you nothing, too > | 25> range(0,1)= [0] > | 26> range(0,1,1)= [0] > | // When interval > range, count only the first: > | 27> range(2,5,4)= [2] > | 28> range(2,5,-1)= [2] > | 29> range(-2,5,-4)= [-2] > | 30> range(-2,5,1)= [-2] > | > | range( obj ) > | > | // range( arr ) or range( str ) to return a range of an array > | // so you don"t have to do : range( len(arr) ) > | > | > obj = [10, 11, 12, 13] > | > | 31> range(obj)= [0, 1, 2, 3] > | 32> range([3,4,5])= [0, 1, 2] > | 33> range("345")= [0, 1, 2] > | 34> range([])= [] > | 35> range(")= [] > | > | range( ... cycle= i,-i,true...) > | > | // New 2014.9.7: > | // cycle= +i: extend the index on the right by +i count > | // cycle= -i: extend the index on the right by -i count > | // cycle= true: same as cycle=1 > | > | 36> range(4,cycle=1)= [0, 1, 2, 3, 0] // Add first to the right > | 37> range(4,cycle=-1)= [3, 0, 1, 2, 3] // Add last to the left > | 38> range(4,cycle=true)= [0, 1, 2, 3, 0] // true=1 > | > | 39> range(2,5,cycle=1)= [2, 3, 4, 2] // Add first to the right > | 40> range(2,5,cycle=2)= [2, 3, 4, 2, 3] // Add 1st,2nd to the right > | 41> range(2,5,cycle=-1)= [4, 2, 3, 4] // Add last to the left > | 42> range(2,5,cycle=-2)= [3, 4, 2, 3, 4] > | 43> range(2,5,cycle=true)= [2, 3, 4, 2] // true=1 > | > | 44> range(2,1.5,8,cycle=2)= [2, 3.5, 5, 6.5, 2, 3.5] > | 45> range(2,1.5,8,cycle=-2)= [5, 6.5, 2, 3.5, 5, 6.5] > | 46> range(2,1.5,8,cycle=true)= [2, 3.5, 5, 6.5, 2] > | > | range(obj,cycle=i,-1,[-1,1],true...) > | > | > obj = [10, 11, 12, 13] > | 47> range(obj,cycle=1)= [0, 1, 2, 3, 0] > | 48> range(obj,cycle=[-1,1])= [3, 0, 1, 2, 3, 0] > | 49> range(obj,cycle=2)= [0, 1, 2, 3, 0, 1] > | 50> range(obj,cycle=-1)= [3, 0, 1, 2, 3] > | 51> range(obj,cycle=-2)= [2, 3, 0, 1, 2, 3] > | > | range( obj, returnitems=true,false ) > | > | // When using obj, can set returnitems=true to return items. > | > | 52> range([3,4,5],returnitems=true)= [3, 4, 5] > | 53> range(obj,cycle=[-1,1],returnitems=true)= [13, 10, 11, 12, 13, 10] ----- $ Runsun Pan, PhD $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner $ -- hash parameter model: here , here $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13212.html Sent from the OpenSCAD mailing list archive at Nabble.com.