foreach in Netlogo 5 vs 6

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

foreach in Netlogo 5 vs 6

mijujoel-2
I was looking at the Garbage Can Model of Organizational Choice.  It is from an older version of Netlogo, so it doesn't work under the latest version.  In particular, this code does not work:
 

 foreach [who] of turtles-here with [breed != participant and not (member? who [encountered] of myself)]
  [
        set encountered fput ? encountered
 ]
 

 In the latest version of Netlogo, what replaces the "?" - in other words, how do I reference each item in the foreach loop.
 

 Does anyone know of other models of organizational behavior?  
 

 Thanks.
 

 Michael Samuels
Reply | Threaded
Open this post in threaded view
|

Re: foreach in Netlogo 5 vs 6

francois.guerrin
Hi, this is an example looking very much with yours, which works with NL 6.02:
 

 globals my-list
 

 set my-list []

 

 ask turtles [
 foreach [who] of turtles-here with [who > 100] [? -> set my-list fput ? my-list ]
  ]
 

 So in your case you should put :
 ...

 foreach [who] of turtles-here with…  ;; verify that this is a list
 [ ? -> set encountered fput ? encountered ]
 ...

 

 Any letter or word may replace "?" but you can still continue using it as you wish.

 The trick is the "->" mapping of your anonymous variable (here each starting list element) to what you want it to become (here making a new list).
 For more complete explanations, you should read the release notes for NL 6.02 and the transition guide :
 https://ccl.northwestern.edu/netlogo/transition.html https://ccl.northwestern.edu/netlogo/transition.html

 

 Cheers
 FG

 

Reply | Threaded
Open this post in threaded view
|

Re: foreach in Netlogo 5 vs 6

francois.guerrin
In reply to this post by mijujoel-2
A little example looking like yours:
 globals my-list
 set my-list []
 ask turtles [
 foreach [who] of turtles-here with [who > 100] [? -> set my-list fput ? my-list ]
  ]
 

 So in your case you should put :
 foreach [who] of turtles-here with…  ;; verify that this is a list
 [ ? ->   set encountered fput ? encountered ]
 
You can continue to use ?, ?1 etc or any letter or name for these anonymous variables (foo is an option).
The trick is the "->" mapping tour anonymous variable to the procedure you want it to execute (here building a list).
See for more the transition guide from v. 5 to v.6.0 on the NL site
Cheers
FG
 

---In [hidden email], <mijujoel@...> wrote :

 I was looking at the Garbage Can Model of Organizational Choice.  It is from an older version of Netlogo, so it doesn't work under the latest version.  In particular, this code does not work:
 

 foreach [who] of turtles-here with [breed != participant and not (member? who [encountered] of myself)]
  [
        set encountered fput ? encountered
 ]
 

 In the latest version of Netlogo, what replaces the "?" - in other words, how do I reference each item in the foreach loop.
 

 Does anyone know of other models of organizational behavior?  
 

 Thanks.
 

 Michael Samuels


Reply | Threaded
Open this post in threaded view
|

Re: foreach in Netlogo 5 vs 6

Simone Gabbriellini-3
In reply to this post by mijujoel-2
try:


foreach [who] of turtles-here with [breed != participant and not (member? who [encountered] of myself)]


 [  [id] ->
        set encountered fput id encountered
 ]


depending on your NL version you might not need the brackets for a single param (6.0 yes, >6.0 no)


HTH,
Simo


------------------------


Dr. Simone Gabbriellini, PhD


On 25 ott 2017, 17:39 +0200, wrote:
>
> foreach [who] of turtles-here with [breed != participant and not (member? who [encountered] of myself)]
>
>  [
>         set encountered fput ? encountered
>  ]
Reply | Threaded
Open this post in threaded view
|

Re: foreach in Netlogo 5 vs 6

Bryan Head
In reply to this post by mijujoel-2
Hi Michael,


In NetLogo 6, tasks were replaced by the much more flexible and powerful
anonymous procedure syntax. See the transition guide on the change
<https://ccl.northwestern.edu/netlogo/docs/transition.html> and the anonymous
procedure section of the programming guide
<https://ccl.northwestern.edu/netlogo/docs/programming.html#anonymous-procedures>.
In short, for your example, you’d do this:


foreach [who] of turtles-here with [breed != participant and not
(member? who [encountered] of myself)]
[ id ->
  set encountered fput id encountered
]


That is, rather than using the special variables ? and ?1, ?2, etc, you now
name the variables, separating the list of variables from the code to run
with ->. For more than one variable, you surround the list of variables
with []. For instance, (foreach list1 list2 [ [a b] -> print a + b ]).


This is much nicer as you can now, for instance, use multiple nested foreachs
without the special variables colliding:


foreach range 5 [ x ->
  foreach range 5 [ y ->
    crt 1 [ setxy x y ]
  ]
]


Furthermore, you can now do, eg, nested maps, which was not possible before:


map [ x -> map y [ list x y ] range 5 ] range 5


Hope that helps!

Bryan