I’ve developed a bunch of list utilities in a list-utils.nls file which you can check out in this repository. I found myself using many of these operations so often that it made sense for me to make these. As a regular user of R and Python I was finding NetLogo’s list operations a little bit limited and much as I enjoy figuring out map and reduceoperations, it made sense to replicate many R and Python list functions in this way.
I note that @danielvartan has also made utility functions — I don’t know if we should compare notes or join forces, or…?
I wonder if any of the procedures are worthy of inclusion as native reporters? Somewhere along the way range became a primitive, how about adding some more?!
For quick reference, here’s what my .nlsfile provides:
cumulative-proportion [0 1 2 3 4] --> [0 0.1 0.3 0.6 1]
cumulative-sum [0 0 4 6 9] --> [0 0 4 10 19]
join-list [0 2 6 6 7 9] "," --> "0,2,6,6,7,9"
last-position 6 [6 0 9 6 7 2] --> 3
matches 6 [6 0 9 6 7 2] --> [true false false true false false]
matching-positions 6 [6 0 9 6 7 2] --> [0 3]
pairs [1 2 3] [4 5] --> [[1 4] [1 5] [2 4] [2 5] [3 4] [3 5]]
range-from-to-by 0 6 2 --> [0 2 4]
range-from-to-by 1 6 2 --> [1 3 5]
range-from-to-by 0 -6 -2 --> [0 -2 -4 -6]
range-by 10 2 --> [0 2 4 6 8]
range-from-to 3 7 --> [3 4 5 6]
ranks [7 1 4 3] --> [3 0 2 1]
rep-list [1 2 3] 3 true --> [1 2 3 1 2 3 1 2 3]
rep-list [1 2 3] 3 false --> [1 1 1 2 2 2 3 3 3]
rep-list-each [1 2 3] --> [1 1 1 2 2 2 3 3 3]
rep-list-inline [1 2 3] 3 --> [1 2 3 1 2 3 1 2 3]
slice [1 2 3 4] [0 3] --> [1 4]
transpose [[0 1 2] [3 4 5]] --> [[0 3] [1 4] [2 5]]
transpose [[0 3] [1 4] [2 5]] --> [[0 1 2] [3 4 5]]
unzip _list of lists_ ;; an alias for `transpose`
zip [0 2 3 7 8] [0 2 5 12 20] --> [[0 0] [2 2] [3 5] [7 12] [8 20]]
And these, which assume ordered lists, which can be maintained by only using these reporters to maintain lists:
insert-value-in-order [0 0 4 9] 6 --> [0 0 4 6 9]
insert-values-in-order [0 2 4] [1 7 9] --> [0 1 2 4 7 9]
split-list-at-value [0 2 3 7 8] 6 --> [[0 2 3] [7 8]]
split-list-at-value [0 2 3 7 8] 3 --> [[0 2] [3 7 8]]
Most of these should be self-explanatory. Many of them are trivial one or two liners using map and reduce but the latter in particular is not for the faint-hearted, and I think many of these might be nice to have!
I also have utilities for probability distributions not native in NetLogo. Any of these could be generated using R extensions, but it’s handy to have them native. I won’t clutter this post further with the details. See the documentation for details. random-binomial n p in particular would be nice to have native.