discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Reusing function result in recursive function

G
GeoffHorton
Thu, May 20, 2021 2:19 AM

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/

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/
A
adrianv
Thu, May 20, 2021 2:39 AM

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

discuss-leave@.openscad

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 > discuss-leave@.openscad -- Sent from: http://forum.openscad.org/
G
GeoffHorton
Fri, May 21, 2021 5:50 PM

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: http://forum.openscad.org/

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: http://forum.openscad.org/