Pass list to imperative block syntax; needs loop?

I can’t figure out if my code blocks require a loop or not to evaluate the list I send them since they iterate fine with a much simpler script (e.g. n+100) but do not iterate when using an imperative block to run some if/else conditions. Examples I’ve come across always hard-code a list for the example and do not involve a node input so they didn’t seem of use in my situation. The input numbers are being output unchanged and I’m confused as to why; I must be missing something about the functionality of the imperative block. The routine performs fine if I pass it a single angle instead of a list–aside from the fact that the output angle is rotating my object by twice the value for unknown reasons, but that’s another topic for later.

Hey,

I think you might need an ‘elseif’ like in this example…

Hope that helps,

Mark

Nope, outputs stay the same. Finding it a bit strange that the code can receive 90, explicitly be told to return 0 if 90 is received, and instead return 90 with no errors. I must be missing something obvious.

You need to wrap all of it in a For loop, otherwise the question is ‘does the list itself equal 90…’

Personally I prefer to wrap the whole thing in a definition though. Just make sure to declare ‘a’ as a list.

You’ll need to grasp the difference between Associative and Imperative programming

When you use n + 100 in the code block you are coding in the default Associative mode (similar to the way nodes behave)
While in Associative mode you operate on lists as a whole, while in Imperative mode you operate on elements of a list

I’ve provided below Imperative and Associative versions of your workflow

ang;
b;
c = [Imperative]
{
	i = 0;
	d = [];
	for (a in ang)
	{
		if (a < 90)
			d[i] = -(b-a);
		elseif (a < 180)
			d[i] = a - b;
		elseif (a < 270)
			d[i] = -(b - (a -180));
		elseif (a < 360)
			d[i] = (a - 180) - b;
		else
			d[i] = a;
		i = i + 1;
	}
	return d;
};
a < 90 ? -(b-a) :
a < 180 ? a - b :
a < 270 ? -(b - (a - 180)) :
a < 360 ? (a - 180) - b :
a;
2 Likes

I’m currently new to Dynamo (if it isn’t obvious already) and thus there are a lot of “unknown unknowns” and this is the first time I’ve heard mention of associativity. Looking up the difference between the two in the Design Script Guide pdf I found doesn’t really clear things up at least with respect to how they handle lists but your examples do a much better job so thanks for that. I’d never have figured out why a list input would stop being evaluated as a list simply because I switched to imperative–the distinction between imperative/associative appearing nowhere in the threads on how to make if/else statements that I found.

Just on its face it looks like the associative example is more concise and easier to grok at a glance; is there a situation where the imperative example would be preferred?

1 Like

Generally speaking imperative code is a direct tie to what you get with formal text based coding languages. Everything is an object, and if you want the code to iterate over parts of the object then it is imperative that you leverage a FOR loop.

Because associative (which as far as I know is a unique term for Design Script by the way) handles your for loops without issue, and gives access to replication guides, you typically don’t need to switch to imperative for 90% of code tasks.

But when you want to perform a task while a condition is met… well associative doesn’t work there as it doesn’t have a while loop equivalent.

An example of when you might want to do this is adjusting the dimensions of a shape until a given volume is met. Or changing the height of a shading object until the amount of solar gain on a site is met. These can be very intensive to calculate, and often aren’t as clear cut as I make them out to be here, so other solutions (ie: Generative Design) might be a better fit.

1 Like