“Tasks” in NetLogo 5.0beta1<br />March 21, 2011<br />
What are tasks?<br />chunks of code to be run once,run repeatedly, passed around, stored in variables, etc.<br />
Known elsewhere as<br />closures<br />first-class functions<br />lambda<br />
Tasks in NetLogo 5.0<br />2 new data types<br />3 new primitives<br />8 primitives altered<br />
2 new data types<br />Command tasks<br />Reporter tasks<br />
3 new primitives<br />task<br />is-command-task?<br />is-reporter-task?<br />
6 altered primitives<br />map, filter, reduce,n-values, sort-by<br />foreach<br />
2 more altered primitives<br />run<br />runresult<br />
NOT altered (for now)<br />ifelse, while, repeat, loop, ...<br />
NOT altered (for now)<br />ask, of, with, all?, ...<br />
Backward compatible syntax<br />square brackets<br />inputs: ?1 (aka ?), ?2, ?3, ...<br />
Implicit task syntax<br />map [? * ?] [1 2 3] => [1 4 9]<br />filter [? mod 2 = 0] [1 2 3 4] => [2 4]<br />foreach sort tu...
Explicit task syntax<br />set next-task task [ fd 1 ]<br />set coloring task [ ifelse-value (wealth > 100) [ red ] [ blue ...
Disambiguation of task type<br />command or reporter?look at first token after the opening bracket (skipping left parens)<...
No tasks “escape”<br />unless you use the explicit syntax<br />
Running tasks<br />run task [ print 5 ]<br />=> 5<br />print runresult task [5]<br />=> 5<br />
Running tasks (variadic)<br />let printer task [ print ?1 + ?2 ]<br />(run print 2 3)<br />=> 5<br />let square task [? * ...
Concise task syntax<br />task die  short for     task [ die ]<br />task fd   short for     task [ fd ? ]<br />task +    sh...
Concise + implicit syntax<br />map abs [1 -2 3 -4] => [1 2 3 4]<br />reduce + [1 2 3 4] => 10<br />filter is-number? [1 “x...
Extra inputs are ignored<br />(runresult task [? * 2] 1 10 100)=> 2<br />
Tasks are closures<br />close over procedure inputs<br />close over local variables<br />
Closing over procedure inputs<br />to-report printer [x]  report task [ print x ]<br />end<br />run printer 5<br />=> 5<br />
Closing over local variables<br />let x 5<br />let y task [ print x ]<br />run y<br />=> 5<br />
Close over bindings, not values<br />let x 5<br />let y task [x]<br />set x 10<br />print runresult y<br />=> 10<br />
Current agent not closed over<br />globals [g]<br />ask turtle 0 [ set g task [ print who ] ]<br />ask turtle 1 [ run g ]<...
Nonlocal exits<br />stop and report refer to dynamically enclosing procedure, not enclosing task(backwards compatible)<br />
It’s fast<br />100x faster than compiling a string<br />
Models using explicit tasks<br />State Machine Example<br />Termites 3D<br />(afraid that’s all at the moment)<br />
Use tasks, not strings!<br />but, still need strings with run/runresult for running code entered by end user<br />
Possible future work:named inputs<br />[x => x * x]<br />(or something like that?not sure yet)<br />
Possible future work:complex reporter tasks<br />task [<br /> let x ...<br /> ...<br /> report ...<br />]<br />
Wouldn’t it be cool if?:automatic updating<br />set label task [energy]<br />set color task [  ifelse-value (wealth < 100)...
Wouldn’t it be cool if?:scheduling<br />in-ticks 5 task [ fd 1 ]<br />
Questions?<br />http://groups.yahoo.com/group/netlogo-users<br />
Upcoming SlideShare
Loading in …5
×

“Tasks” in NetLogo 5.0beta1

1,711 views

Published on

1 Comment
3 Likes
Statistics
Notes
  • Very nice, the content of this slide should be put into the documentation, it was very enlightening!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,711
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

“Tasks” in NetLogo 5.0beta1

  1. 1. “Tasks” in NetLogo 5.0beta1<br />March 21, 2011<br />
  2. 2. What are tasks?<br />chunks of code to be run once,run repeatedly, passed around, stored in variables, etc.<br />
  3. 3. Known elsewhere as<br />closures<br />first-class functions<br />lambda<br />
  4. 4. Tasks in NetLogo 5.0<br />2 new data types<br />3 new primitives<br />8 primitives altered<br />
  5. 5. 2 new data types<br />Command tasks<br />Reporter tasks<br />
  6. 6. 3 new primitives<br />task<br />is-command-task?<br />is-reporter-task?<br />
  7. 7. 6 altered primitives<br />map, filter, reduce,n-values, sort-by<br />foreach<br />
  8. 8. 2 more altered primitives<br />run<br />runresult<br />
  9. 9. NOT altered (for now)<br />ifelse, while, repeat, loop, ...<br />
  10. 10. NOT altered (for now)<br />ask, of, with, all?, ...<br />
  11. 11. Backward compatible syntax<br />square brackets<br />inputs: ?1 (aka ?), ?2, ?3, ...<br />
  12. 12. Implicit task syntax<br />map [? * ?] [1 2 3] => [1 4 9]<br />filter [? mod 2 = 0] [1 2 3 4] => [2 4]<br />foreach sort turtles [ print ? ]<br />
  13. 13. Explicit task syntax<br />set next-task task [ fd 1 ]<br />set coloring task [ ifelse-value (wealth > 100) [ red ] [ blue ] ]<br />
  14. 14. Disambiguation of task type<br />command or reporter?look at first token after the opening bracket (skipping left parens)<br />
  15. 15. No tasks “escape”<br />unless you use the explicit syntax<br />
  16. 16. Running tasks<br />run task [ print 5 ]<br />=> 5<br />print runresult task [5]<br />=> 5<br />
  17. 17. Running tasks (variadic)<br />let printer task [ print ?1 + ?2 ]<br />(run print 2 3)<br />=> 5<br />let square task [? * ?]<br />print (runresult square 5)<br />=> 25<br />
  18. 18. Concise task syntax<br />task die short for task [ die ]<br />task fd short for task [ fd ? ]<br />task + short for task [?1 + ?2]<br />
  19. 19. Concise + implicit syntax<br />map abs [1 -2 3 -4] => [1 2 3 4]<br />reduce + [1 2 3 4] => 10<br />filter is-number? [1 “x” 3] => [1 3]<br />
  20. 20. Extra inputs are ignored<br />(runresult task [? * 2] 1 10 100)=> 2<br />
  21. 21. Tasks are closures<br />close over procedure inputs<br />close over local variables<br />
  22. 22. Closing over procedure inputs<br />to-report printer [x] report task [ print x ]<br />end<br />run printer 5<br />=> 5<br />
  23. 23. Closing over local variables<br />let x 5<br />let y task [ print x ]<br />run y<br />=> 5<br />
  24. 24. Close over bindings, not values<br />let x 5<br />let y task [x]<br />set x 10<br />print runresult y<br />=> 10<br />
  25. 25. Current agent not closed over<br />globals [g]<br />ask turtle 0 [ set g task [ print who ] ]<br />ask turtle 1 [ run g ]<br />=> 1<br />
  26. 26. Nonlocal exits<br />stop and report refer to dynamically enclosing procedure, not enclosing task(backwards compatible)<br />
  27. 27. It’s fast<br />100x faster than compiling a string<br />
  28. 28. Models using explicit tasks<br />State Machine Example<br />Termites 3D<br />(afraid that’s all at the moment)<br />
  29. 29. Use tasks, not strings!<br />but, still need strings with run/runresult for running code entered by end user<br />
  30. 30. Possible future work:named inputs<br />[x => x * x]<br />(or something like that?not sure yet)<br />
  31. 31. Possible future work:complex reporter tasks<br />task [<br /> let x ...<br /> ...<br /> report ...<br />]<br />
  32. 32. Wouldn’t it be cool if?:automatic updating<br />set label task [energy]<br />set color task [ ifelse-value (wealth < 100) [red] [blue] ]<br />
  33. 33. Wouldn’t it be cool if?:scheduling<br />in-ticks 5 task [ fd 1 ]<br />
  34. 34. Questions?<br />http://groups.yahoo.com/group/netlogo-users<br />

×