“Tasks” in NetLogo 5.0beta1March 21, 2011
What are tasks?chunks of code to be run once,run repeatedly, passed around, stored in variables, etc.
Known elsewhere asclosuresfirst-class functionslambda
Tasks in NetLogo 5.02 new data types3 new primitives8 primitives altered
2 new data typesCommand tasksReporter tasks
3 new primitivestaskis-command-task?is-reporter-task?
6 altered primitivesmap, filter, reduce,n-values, sort-byforeach
2 more altered primitivesrunrunresult
NOT altered (for now)ifelse, while, repeat, loop, ...
NOT altered (for now)ask, of, with, all?, ...
Backward compatible syntaxsquare bracketsinputs: ?1 (aka ?), ?2, ?3, ...
Implicit task syntaxmap [? * ?] [1 2 3] => [1 4 9]filter [? mod 2 = 0] [1 2 3 4] => [2 4]foreach sort turtles [ print ? ]
Explicit task syntaxset next-task task [ fd 1 ]set coloring task [ ifelse-value (wealth > 100) [ red ] [ blue ] ]
Disambiguation of task typecommand or reporter?look at first token after the opening bracket (skipping left parens)
No tasks “escape”unless you use the explicit syntax
Running tasksrun task [ print 5 ]=> 5print runresult task [5]=> 5
Running tasks (variadic)let printer task [ print ?1 + ?2 ](run print 2 3)=> 5let square task [? * ?]print (runresult square 5)=> 25
Concise task syntaxtask die  short for     task [ die ]task fd   short for     task [ fd ? ]task +    short for    task [?1 + ?2]
Concise + implicit syntaxmap abs [1 -2 3 -4] => [1 2 3 4]reduce + [1 2 3 4] => 10filter is-number? [1 “x” 3] => [1 3]
Extra inputs are ignored(runresult task [? * 2] 1 10 100)=> 2
Tasks are closuresclose over procedure inputsclose over local variables
Closing over procedure inputsto-report printer [x]  report task [ print x ]endrun printer 5=> 5
Closing over local variableslet x 5let y task [ print x ]run y=> 5
Close over bindings, not valueslet x 5let y task [x]set x 10print runresult y=> 10
Current agent not closed overglobals [g]ask turtle 0 [ set g task [ print who ] ]ask turtle 1 [ run g ]=> 1
Nonlocal exitsstop and report refer to dynamically enclosing procedure, not enclosing task(backwards compatible)
It’s fast100x faster than compiling a string
Models using explicit tasksState Machine ExampleTermites 3D(afraid that’s all at the moment)
Use tasks, not strings!but, still need strings with run/runresult for running code entered by end user
Possible future work:named inputs[x => x * x](or something like that?not sure yet)
Possible future work:complex reporter taskstask [ let x ... ... report ...]
Wouldn’t it be cool if?:automatic updatingset label task [energy]set color task [  ifelse-value (wealth < 100)    [red] [blue] ]
Wouldn’t it be cool if?:schedulingin-ticks 5 task [ fd 1 ]
Questions?http://groups.yahoo.com/group/netlogo-users

“Tasks” in NetLogo 5.0beta1