Function loop via CodeBlock

function
loop
codeblock
package

#1

For a Long time i was looking for a way to make this possible:
I had to duplicate many times the same node to make the function “remesh” for a larger amount of times, because it belongs to the MeshToolkit package.

So i didn’t know how to make it run inside python. Anyhow finally figured out a way, so i wanted to share it for those who may have the same problem, and it also works with other functions.

I leave here the code for those interested:

def FuncLoop(eq,func,elem)
{
return = [Imperative]
{
list2 = {};
ind = 0;
for (l in 1…eq)
{
if (ind >=1){
list2[ind] = __Apply(func,list2[ind-1]);
ind = ind + 1;
}
else{
list2[ind] = __Apply(func,elem);
ind = ind + 1;
}
}
return = list2[eq-1];
}
};
FuncLoop(loops,functioN,element);


#2

Exceedingly helpful :+1:t2:


#3

In most cases, you should be able to shorten that a bit:

def fnLoop(fn, x, N){
return = [Imperative]{
for (_ in 1..N)
{
	x = __Apply(fn, x);
}
return = x;
}};

#4

Indeed! i am beginner into this, so i did it based on another ‘for’ loop that i found, yours is better and simple, thank you!