Mixin: ABM.util.array

Defined in: src/util_array.coffee
Included in: ABM.Array ABM.Util.Array

Overview

Array utility functions. Are added to ABM.Array.

TODO allow be used in user models through an ABM.noArray() function.

Method Summary

Method Details

~ (void) from(array, arrayType)

The static ABM.Array.from as a method. Used by methods creating new arrays.

~ (void) toString(array)

Return string representative of agentset.

~ (void) toFixed(array, precision = 2)

Return an array of floating pt numbers as strings at given precision; useful for printing.

~ (void) any(array)

Does the array have any elements? Is the array empty?

~ (void) empty(array)

Checks for emptyness.

~ (void) clear(array)

Clears the array.

~ (void) clone(array, begin = null, end = null)

Make a copy of the array. Needed when you don't want to modify the given array with mutator methods like sort, splice or your own functions. By giving begin/arguments, retrieve a subset of the array. Works with TypedArrays too.

~ (void) first(array)

Return first element of array.

~ (void) last(array)

Return last element of array.

~ (void) select(array, condition)

Return all elements of array that match condition.

~ (void) reject(array, condition)

Return all elements of array that don't match condition.

~ (void) sample(array, options)

Return random element of array or number random elements of array.

Note: Objects are presumed unique, and the same object will never be included twice if more than one random object is requested.

Note: clone, shuffle then first number has poor performance.

Options: size: Size of the requested sample (defaults to one if null). condition: Function that elements should return true for elements.

~ (void) contains(array, object)

True if object is in array.

~ (void) remove(array, objects)

Remove one or more objects from an array. Error if an object not in array.

Error if object not in array.

~ (void) removeItem(array, object)

Removes a single object from an array. Even if it is an array itself.

Error if object not in array.

~ (void) shuffle(array)

Randomize the elements of this array.

~ (void) min(array, call = u.identityFunction, valueToo = false)

Return object when call(object) min/max in array. Error if array empty. If f is a string, return element with max value of that property. If "valueToo" then return a 2-array of the element and the value; used for cases where f is costly function.

array = [{x: 1, y: 2}, {x: 3, y: 4}]
array.min()
# returns {x: 1, y: 2} 5

[min, dist2] = array.min(((o) -> o.x * o.x + o.y * o.y), true)
# returns {x: 3, y: 4}

~ (void) max(array, call = u.identityFunction, valueToo = false)

See min.

~ (void) sum(array, call = u.identityFunction)

Sums up the contents of the array.

~ (void) average(array, call = u.identityFunction)

Calculates the average of the array.

~ (void) median(array)

Returns the median for the array.

~ (void) histogram(array, binSize = 1, call = u.identityFunction)

Return histogram of o when f(o) is a numeric value in array. Histogram interval is bin. Error if array empty. If call is a string, return histogram of that property.

In examples below, histogram returns [3, 1, 1, 0, 0, 1]

array = [1, 3, 4, 1, 1, 10]
histogram = histogram array, 2, (i) -> i

hash = ({id:i} for i in array)
histogram = histogram hash, 2, (o) -> o.id
histogram = histogram hash, 2, "id"

~ (void) sort(array, call = null)

Mutator. Sort array of objects in place by the function f. If f is string, f returns property of object.

Returns array.

Clone first if you want to preserve the original array.

array = [{i: 1}, {i: 5}, {i: -1}, {i: 2}, {i: 2}]
sortBy array, "i"
# array now is [{i: -1}, {i: 1}, {i: 2}, {i: 2}, {i:5}]

~ (void) uniq(array)

Mutator. Removes dups, by reference, in place from array. Note "by reference" means literally same object, not copy. Returns array. Clone first if you want to preserve the original array.

ids = ({id: i} for i in [0..10])
array = (ids[i] for i in [1, 3, 4, 1, 1, 10])
# array is [{id: 1}, {id: 3}, {id: 4}, {id: 1}, {id: 1}, {id: 10}]

arrayB = clone array
sortBy arrayB, "id"
# arrayB is [{id:1}, {id: 1}, {id: 1}, {id: 3}, {id: 4}, {id: 10}]

uniq arrayB
# arrayB now is [{id:1}, {id: 3}, {id: 4}, {id: 10}]

~ (void) flatten(array)

Return a new array composed of the rows of a matrix.

array = [[1, 2, 3], [4, 5, 6]]
array.flatten()
# returns [1, 2, 3, 4, 5, 6]

~ (void) concat(array, addArray)

Returns a new array that has addArray appended.

Concat checks [[ClassName]], and this does not work for things inheriting from Array.

~ (void) normalize(array, low = 0, high = 1)

Return an array with values in [low, high], defaults to [0, 1]. Note: to have a half-open interval, [low, high), try high = high - .00009

~ (void) normalizeInt(array, low, high)

~ (void) ask(array, call)

Useful in console. Also see CoffeeConsole Chrome extension.

Similar to NetLogo ask & with operators. Use:

array.with((object) -> object.x < 5)
  .ask((object) -> object.x = object.x + 1)
myModel.agents.with((object) -> object.id < 100)
  .ask(object.color = u.color.red)

~ (void) with(array, functionString)

~ (void) getProperty(array, property)

Property access, also useful for debugging.

Return an array of a property of the BreedSet.

array.getProperty("x") # [1, 8, 6, 2, 2]
array.getProperty("x") # [2, 8, 6, 3, 3]

~ (void) setProperty(array, property, value)

Set the property of the agents to a given value. If value is an array, its values will be used, indexed by agentSet's index. This is generally used via: getProperty, modify results, setProperty.

set.setProperty "x", 2
# {id: 4, x: 2, y: 3}, {id: 5, x: 2, y: 1}

~ (void) other(array, given)

Return an array without given object.

as = AS.clone().other(AS[0])
as.getProperty "id" # [1, 2, 3, 4]

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: