You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The NetLogo array extension is in the public domain. To the extent possible under law, Uri Wilensky has waived all copyright and related or neighboring rights.
Copy file name to clipboardExpand all lines: README.md
+91-6Lines changed: 91 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,5 @@
1
-
# NetLogo array extension
2
1
3
-
This package contains the NetLogo array extension.
4
-
5
-
## Using
6
-
7
-
This extension is pre-installed in NetLogo. For instructions on using it, or for more information about NetLogo extensions, see the NetLogo User Manual.
2
+
# NetLogo Array Extension
8
3
9
4
## Building
10
5
@@ -14,6 +9,96 @@ Use the netlogo.jar.url environment variable to tell sbt which NetLogo.jar to co
14
9
15
10
If compilation succeeds, `array.jar` will be created.
16
11
12
+
## Using
13
+
14
+
The array extension is pre-installed in NetLogo.
15
+
16
+
To use the array extension in your model, add a line to the top of your Code tab:
17
+
18
+
```
19
+
extensions [array]
20
+
```
21
+
22
+
If your model already uses other extensions, then it already has an
23
+
`extensions` line in it, so just add `array` to the list.
24
+
25
+
For more information on using NetLogo extensions,
26
+
see the [Extensions Guide](http://ccl.northwestern.edu/netlogo/docs/extensions.html)
27
+
28
+
### When to Use
29
+
30
+
In general, anything you can do with an array in NetLogo, you could
31
+
also just use a list for. But you may want to consider using an array
32
+
instead for speed reasons. Lists and arrays have different performance
33
+
characteristics, so you may be able to make your model run faster by
34
+
selecting the appropriate data structure.
35
+
36
+
Arrays are useful when you need a collection of values whose size is
37
+
fixed. You can quickly access or alter any item in an array if you
38
+
know its position.
39
+
40
+
Unlike NetLogo's lists and strings, arrays are "mutable".
41
+
That means that you can actually modify them directly,
42
+
rather than constructing an altered copy as with lists. If
43
+
the array is used in more than one place in your code, any
44
+
changes you make will show up everywhere. It's tricky to write
45
+
code involving mutable structures and it's easy to make subtle
46
+
errors or get surprising results, so we suggest sticking with lists
47
+
and strings unless you're certain you want and need mutability.
48
+
49
+
### Example use of Array Extension
50
+
51
+
```NetLogo
52
+
let a array:from-list n-values 5 [0]
53
+
print a
54
+
=> {{array: 0 0 0 0 0}}
55
+
print array:length a
56
+
=> 5
57
+
foreach n-values 5 [ [i] -> i ] [ [i] -> array:set a i i * i ]
58
+
print a
59
+
=> {{array: 0 1 4 9 16}}
60
+
print array:item a 0
61
+
=> 0
62
+
print array:item a 3
63
+
=> 9
64
+
array:set a 3 50
65
+
print a
66
+
=> {{array: 0 1 4 50 16}}
67
+
```
68
+
69
+
## Primitives
70
+
71
+
72
+
### `array:from-list`
73
+
74
+
Reports a new array containing the same items in the same order as the input list.
75
+
76
+
77
+
### `array:item`
78
+
79
+
Reports the item in the given array with the given index (ranging from zero to the length of the array minus one).
80
+
81
+
82
+
### `array:set`
83
+
84
+
85
+
Sets the item in the given array with the given index (ranging from zero to the length of the array minus one) to the given value.
86
+
87
+
Note that unlike the `replace-item` primitive for lists, a new array is not created.
88
+
The given array is actually modified.
89
+
90
+
91
+
92
+
### `array:length`
93
+
94
+
Reports the length of the given array, that is, the number of items in the array.
95
+
96
+
97
+
### `array:to-list`
98
+
99
+
Reports a new list containing the same items in the same order as the given array.
0 commit comments