Nabble has removed Mailing-list integration.
Posts created here DO NOT GET SENT TO THE MAILING LIST.
Mailing-list emails DO NOT GET POSTED TO THE FORUM.
So basically the Forum is now out of date, we are looking into migrating the history.

For now you should send emails, people will see them, discuss@lists.openscad.org.

Reusing function result in recursive function

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

Reusing function result in recursive function

GeoffHorton
Sorry for the vague title, but I couldn't come up with a better one.

I have a recursive function like this:

function build_vec(n, x)  =
    n = 0
    ? []
    : concat([[n, f(x)], build_vec(n - 1, f(x)));

where f(x) is a multi-level iteration over vectors that takes a long time to run. Life would be a lot more efficient if there were a way only to call f(x) once, but I can't figure out how to do it. Is there a way?

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Reusing function result in recursive function

adrianv
If you mean you don't want to call it twice at each iteration:

function build_vec(n,x) =
   n== 0 ? []    // assume you meant ==
  : let(fx=f(x))
   concat([[n,fx], build_vec(n-1,fx)]);   // assume close square bracket on the right side

If you mean you want to call it once over all then

function build_vec(n,fx)

and pass f(x) in as an argument.  You can use a caller function to calculate f(x).  

I may have inserted the brackets in the wrong place to make your code valid.  As I did above it makes ever deeper nested vectors.  If what you actually want is a table all at the same nesting depth, you might be able to make it using a list comprehension.  

GeoffHorton wrote
Sorry for the vague title, but I couldn't come up with a better one.

I have a recursive function like this:

function build_vec(n, x)  =
    n = 0
    ? []
    : concat([[n, f(x)], build_vec(n - 1, f(x)));

where f(x) is a multi-level iteration over vectors that takes a long time to
run. Life would be a lot more efficient if there were a way only to call
f(x) once, but I can't figure out how to do it. Is there a way?



--
Sent from: http://forum.openscad.org/
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Reusing function result in recursive function

GeoffHorton
That works. Thank you!

I learned to program in BASIC 40 years ago and that double == still gets me if I'm not paying attention.

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]