for loop in a function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

for loop in a function

Mint86
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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

frankv
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 <[hidden email]> 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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

tp3
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.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

NateTG
In reply to this post by frankv
> 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 the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

Mint86
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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

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

> On Apr 10, 2018, at 9:06 PM, Mint86 <[hidden email]> 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
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
rew
Reply | Threaded
Open this post in threaded view
|

Re: for loop in a function

rew
In reply to this post by frankv
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.

--
** [hidden email] ** 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.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org