Converting an anonymous reporter in NL-5 to NL6
I am brand new to NetLogo, and trying to use a model written for v5 on v6, and hit some conversion challenges.
From reading the docs, the replacement of ? with anonymous functions seems easy enough to understand, however most examples I have seen seem focused on foreach loops, and not enough mention of sort-by. When I apply the conversion that I think ought to work, I hit an error that suggests a literal is expected.
I have a challenge with these 2 loops in particular
#loop-1
foreach sort-by [[who] of ?1 < [who] of ?2] aset[ ]
#loop-2
foreach sort-by [ ?1 < ?2 ] bset [ ]
If I rewrite both as follows, I get an error that a literal is expected
#loop-1
foreach sort-by [[?1 ?2] -> [[who] of ?1 > [who] of ?2]] aset [ ]
#loop-2
foreach sort-by [[?1 ?2] -> [ ?1 < ?2 ]] bset [ ]
Would appreciate any suggestions.
I don't know all of the context (what is the purpose of the loops?).
I assume, they should sort turtles by their who
number.
You don't have to (but can) get rid of the '?'. The error comes from writing the code in brackets after the ->
.
If there's another context, the foreach
might be usefull, but as far as I understood the purpose, it can be deleted.
This worked:
to go
crt 5
let aset turtles with [who <= 3]
let bset turtles with [who >= 3]
;loop-1
show sort-by [[?1 ?2] -> ?1 > ?2] aset
;loop-2
show sort-by [[?1 ?2] -> ?1 < ?2 ] bset
end
I think I figured this out now. These work ... I needed to get rid of the '?'
#loop-1
foreach sort-by [[a b] -> [[who] of a > [who] of b]] aset [ ]
#loop-2
foreach sort-by [[x y] -> x < y ] bset [ ]