Maptitude GISDK Help |
GISDK arrays now have several methods that can be called on them which use a block parameter to allow various transformations and other operations.
map(block)
Invokes the given block once for each element of the array and returns a new array containing the values returned by the block. For example:
values = {1, 2, 3}
result = values.map(do (v) Return(v * 3) end)
//result == {3, 6, 9}
map(property_name)
Fetches the given property value for each element of the array and returns a new array containing these values.
values = {{Id: 1, Value: 25},
{Id: 2, Value: 14},
{Id: 3, Value: 36}}
result = values.map("Value")
//result == {25, 14, 36}
select(block)
Returns a new array containing all elements of the original array for which the given block returns a true value. For example:
words = ParseString("the cat sat on the mat", " ")
result = words.select(do (word) Return(word contains "a") end)
//result == {"cat", "sat", "mat"}
find(block)
Passes each element in array to block. Returns the first for which block returns True, or null if block returns False for every element. For example:
link_ids = {25, 89, 436, 503}
node_ids = {9, 45, 301}
result = link_ids.find(do (link)
nodes = GetEndpoints(link)
if ArrayPosition(node_ids, {nodes[1]},) > 0 then Return(True)
if ArrayPosition(node_ids, {nodes[2]},) > 0 then Return(True)
end)
//result is the first link in link_ids which has a node from node_ids
position(block)
Equivalent to the 'find' method but returns the index in the array of the first element for which the block returns True, or 0 if block returns False for every element.
reduce(initial_value, block)
For each element in the array the block is passed an accumulator value (memo) and the element. For the first element of the array the memo is initial_value. The return value from the block becomes the new value for memo passed with the next item. At the end of the iteration, the final value of memo is the return value for the method. For example:
values = {1,2,3,4,5}
sum = values.reduce(0, do (accum, val) Return(accum + val) end)
//sum == 15
each(block)
Calls the block once for each element in the array, passing that element as a parameter. It is equivalent to:
for item in array do
statements...
end
sort(block)
The block will be called with 2 elements of the array as parameters and should return a negative number if the first element should come before the second, a positive number if the first should come after the second, or 0 if they are equivalent. Returns the sorted array of the elements. For example:
names = {"Fred", "Liz", "Peter",
"Amy", "John", "George"}
sorted = name.sort(do (a, b)
if a < b then Return(-1)
else if a > b then Return(1)
else Return(0)
end)
//returns array of names sorted in alphabetical order
sortby(block)
Invokes the given block once for each element of the array and returns a new array containing the elements sorted in ascending order of the values returned by the block. For example:
names = {"Fred", "Liz", "Peter",
"Amy", "John", "George"}
sorted = name.sortby(do (name)
Return(Len(name))
end)
//returns array of names sorted order of length
sortby(property_name)
Returns the array sorted by the values returned by fetching the given property value for each element. For example:
names = {"Fred", "Liz", "Peter", "Amy", "John", "George"}
sorted = names.sortby("length")
//returns array of names sorted order of length
flatten()
Returns a new array that is a one-dimensional flattening of the original array. That is, for every element that is an array, extract its elements into the new array. For example:
values = {1, {2, 3}, {4, 5, 6}, 7, {8, {9,10}}}
result = values.flatten()
//result = {1, 2, 3, 4, 5, 6, 7, 8, {9,10}}
zip(array1, array2, ...)
Returns a new array with the same number of elements as the original array where each element is itself an array containing the elements at that position from the original array and each passed-in array. If any of the passed-in arrays are shorter then the original array then the corresponding elements in the resulting array will contain a null value at those positions. For example:
values = {"A", "B", "C", "D"}
result = values.zip({1, 2, 3})
//result = {{"A", 1}, {"B", 2}, {"C", 3}, {"D", null}}
Please note that you can specify an additional optional parameter in the blocks for all of these methods which when called will contain the index of that element in the containing array. For example:
result = values.map(do (value,index) ... end)
result = link_ids.find( do(link,index) ... end)
©2025 Caliper Corporation | www.caliper.com |