Gabriel Fair
Gabriel Fair

Reputation: 4264

Is it possible to create a switch group in Netlogo?

Sorry if this question is weird, I'm still switching over my programming brain from OOP.

Problem

I have a collection of switches. And I want to create the same amount of patch colors as switches that are currently on.

How I would solve it

For example in python I would use the following code to get the number of switches with value 1:

sum(switch_hashmap.values())

Question

So my question stems from my neophyte approach to solving this problem. By thinking of switches as an object that has elementary functions built into it. I'm avoiding writing a dozen if blocks as that smells like bad design.

Is this possible in netlogo? What is the best practice here?

Upvotes: 0

Views: 73

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

Suppose you have switches a, b and c on you interface tab. To get the number of switches that are currently on, you can simply do:

sum (map [ v -> ifelse-value v [ 1 ] [ 0 ] ] (list a b c))

The (list a b c) part uses the state of your switches to build a list of booleans, which we then map to a list of ones and zeros that we can sum. Except for the non-object-orientedness and the fact that you have to explicitly convert booleans to 1 or 0, it's not that far from your Python code.

Still, I don't believe that "thinking of switches as an object that has elementary functions built into it" is the right conceptual framework. It is true that interface widgets are internally represented as objects, but from the point of view of a NetLogo programmer, they should just be thought of as a bunch of global variables that happen to be modifiable by the user.

Upvotes: 4

Related Questions