discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

for loop in a function

M
Mint86
Tue, Apr 10, 2018 11:34 PM

Hi

I haven't been able to get a for loop working in a function. I'm trying to
add the values in a vector from a start point to an end point.

Something like this:

AVector = [2,5,3,11,4];
start = 1;
end = 3;

//addVector would return 19

function addVector (aVector,start,end)

Any help would be great. Thanks!

--
Sent from: http://forum.openscad.org/

Hi I haven't been able to get a for loop working in a function. I'm trying to add the values in a vector from a start point to an end point. Something like this: AVector = [2,5,3,11,4]; start = 1; end = 3; //addVector would return 19 function addVector (aVector,start,end) Any help would be great. Thanks! -- Sent from: http://forum.openscad.org/
FV
Frank van der Hulst
Wed, Apr 11, 2018 12:17 AM

From the manual:

// recursion - find the sum of the values in a vector (array) by calling itself
// from the start (or s'th element) to the i'th element - remember
elements are zero based

function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s));

vec=[ 10, 20, 30, 40 ];
echo("sum vec=", sumv(vec,2,1)); // calculates 20+30=50

On Wed, Apr 11, 2018 at 11:34 AM, Mint86 eric23b@gmail.com wrote:

Hi

I haven't been able to get a for loop working in a function. I'm trying to
add the values in a vector from a start point to an end point.

Something like this:

AVector = [2,5,3,11,4];
start = 1;
end = 3;

//addVector would return 19

function addVector (aVector,start,end)

Any help would be great. Thanks!

--
Sent from: http://forum.openscad.org/


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

>From the manual: // recursion - find the sum of the values in a vector (array) by calling itself // from the start (or s'th element) to the i'th element - remember elements are zero based function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s)); vec=[ 10, 20, 30, 40 ]; echo("sum vec=", sumv(vec,2,1)); // calculates 20+30=50 On Wed, Apr 11, 2018 at 11:34 AM, Mint86 <eric23b@gmail.com> wrote: > Hi > > I haven't been able to get a for loop working in a function. I'm trying to > add the values in a vector from a start point to an end point. > > Something like this: > > AVector = [2,5,3,11,4]; > start = 1; > end = 3; > > //addVector would return 19 > > function addVector (aVector,start,end) > > Any help would be great. Thanks! > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
TP
Torsten Paul
Wed, Apr 11, 2018 12:26 AM

And just for fun, a non-recursive version using the c-style
for loop (needs to be enabled as experimental feature in
the dev snapshots)...

function addVector(aVector, start, end) = [ for (i = end, a = aVector[i];i >= start;i = i - 1, a = a + aVector[i]) a ][end - start];

I guess it's more a puzzle kind of solution as I think the
recursive one is more obvious and due to the tail-recursion
elimination maybe even faster than the one above :).

ciao,
Torsten.

And just for fun, a non-recursive version using the c-style for loop (needs to be enabled as experimental feature in the dev snapshots)... function addVector(aVector, start, end) = [ for (i = end, a = aVector[i];i >= start;i = i - 1, a = a + aVector[i]) a ][end - start]; I guess it's more a puzzle kind of solution as I think the recursive one is more obvious and due to the tail-recursion elimination maybe even faster than the one above :). ciao, Torsten.
N
NateTG
Wed, Apr 11, 2018 12:32 AM

function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s));Depending on

the application, you might want something like:
function sumv(list,start,end) =  start>=end?    0  :
list[start]+sumv(list,start+1,end);
so that it terminates on bad input.  Of course that version doesn't take
advantage of the overloading on + the way that the one above does.

--
Sent from: http://forum.openscad.org/

> function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s));Depending on the application, you might want something like: function sumv(list,start,end) = start>=end? 0 : list[start]+sumv(list,start+1,end); so that it terminates on bad input. Of course that version doesn't take advantage of the overloading on + the way that the one above does. -- Sent from: http://forum.openscad.org/
M
Mint86
Wed, Apr 11, 2018 1:06 AM

Awesome! Thanks for the quick replies.

They both are what I was looking for.

Good tip NateTG.

--
Sent from: http://forum.openscad.org/

Awesome! Thanks for the quick replies. They both are what I was looking for. Good tip NateTG. -- Sent from: http://forum.openscad.org/
JE
John Eron
Wed, Apr 11, 2018 2:35 AM

Thank you so much. I'm going to take a look at them.

On Apr 10, 2018, at 9:06 PM, Mint86 eric23b@gmail.com wrote:

Awesome! Thanks for the quick replies.

They both are what I was looking for.

Good tip NateTG.

--
Sent from: http://forum.openscad.org/


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

Thank you so much. I'm going to take a look at them. > On Apr 10, 2018, at 9:06 PM, Mint86 <eric23b@gmail.com> wrote: > > Awesome! Thanks for the quick replies. > > They both are what I was looking for. > > Good tip NateTG. > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
RW
Rogier Wolff
Wed, Apr 11, 2018 6:01 AM

On Wed, Apr 11, 2018 at 12:17:22PM +1200, Frank van der Hulst wrote:

From the manual:

// recursion - find the sum of the values in a vector (array) by calling itself
// from the start (or s'th element) to the i'th element - remember
elements are zero based

function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s));

I'd write
function sumv(v,i,s=0) = (i<s ? 0 : v[i] + sumv(v,i-1,s));

this won't infinitely recurse and just return zero if you accidentally
reverse the arguments. (it'd make sense to think of it as taking
the sum of elements 5 to 8, and write sumv (vect, 5, 8); )

This also works better when you hit the corner case:

// Take the sum from n elements starting at s.
function sumve(v,s,n=0) = sumv (v, s+n-1, s);

Now the "n=0" case really SHOULD return a value...

Roger. 

--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
-- BitWizard writes Linux device drivers for any device you may have! --
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

On Wed, Apr 11, 2018 at 12:17:22PM +1200, Frank van der Hulst wrote: > From the manual: > > // recursion - find the sum of the values in a vector (array) by calling itself > // from the start (or s'th element) to the i'th element - remember > elements are zero based > > function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s)); I'd write function sumv(v,i,s=0) = (i<s ? 0 : v[i] + sumv(v,i-1,s)); this won't infinitely recurse and just return zero if you accidentally reverse the arguments. (it'd make sense to think of it as taking the sum of elements 5 to 8, and write sumv (vect, 5, 8); ) This also works better when you hit the corner case: // Take the sum from n elements starting at s. function sumve(v,s,n=0) = sumv (v, s+n-1, s); Now the "n=0" case really SHOULD return a value... Roger. -- ** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2600998 ** ** Delftechpark 26 2628 XH Delft, The Netherlands. KVK: 27239233 ** *-- BitWizard writes Linux device drivers for any device you may have! --* The plan was simple, like my brother-in-law Phil. But unlike Phil, this plan just might work.