discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

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

GW
G. Wade Johnson
Fri, Jul 17, 2015 8:34 PM

On Fri, 17 Jul 2015 11:24:48 -0700 (MST)
runsun runsun@gmail.com wrote:

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:

I like range(arr) better than [:arr].

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.


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

--
Do unto others 20% better than you would expect them to do unto you, to
correct for subjective error.                      -- Linus Pauling

On Fri, 17 Jul 2015 11:24:48 -0700 (MST) runsun <runsun@gmail.com> wrote: > 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: I like range(arr) better than [:arr]. > > 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. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Do unto others 20% better than you would expect them to do unto you, to correct for subjective error. -- Linus Pauling
N
Neon22
Sat, Jul 18, 2015 4:17 AM

in python you can access a dictionaries items, values, and keys like so:
arr = {1: 11, 2:22, 3:33}
arr.items()  returns  [(1, 11), (2, 22), (3, 33)]
arr.values() returns    [11, 22, 33]
arr.keys()    returns  [1, 2, 3]

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

in python you can access a dictionaries items, values, and keys like so: arr = {1: 11, 2:22, 3:33} arr.items() returns [(1, 11), (2, 22), (3, 33)] arr.values() returns [11, 22, 33] arr.keys() returns [1, 2, 3] -- View this message in context: http://forum.openscad.org/Proposal-for-range-i-j-k-improvement-tp13184p13223.html Sent from the OpenSCAD mailing list archive at Nabble.com.