ListUtil

From MiniScript Wiki
Revision as of 21:59, 26 August 2021 by JoeStrout (talk | contribs) (Created page with "In Mini Micro, <c>listUtil</c> is an import module in the <c>/sys/lib</c> directory. It provides various additional list-related functions, many of them directly...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In Mini Micro, listUtil is an import module in the /sys/lib directory. It provides various additional list-related functions, many of them directly extending the list datatype.

Like all the modules in /sys/lib, the best documentation for listUtil is the source code (/sys/lib/listUtil.ms) itself. But this page summarizes the content in more concise form.

Added list methods

The following methods are added to the list type, and so are accessed using dot syntax after any list, for example: [1, 2, 3].reverse

Some of these methods mutate (modify) the list you call it on; others return a new list. See below, or the header comments in the source code, for details. Note that this is different from stringUtil, where all methods return a new string (since strings are immutable).

Method Returns
contains(item) true if this list contains the given item
deepCopy returns a clone of this list, recursively cloning any sub-lists as well
add(addend) mutates this list by adding elements of another list of the same size, or by adding addend to all elements
plus(addend) same as add, but returns a new list, leaving this list unchanged
multiplyBy(factor) mutates this list by multiplying elements of another list of the same size, or by multiplying all elements by factor
times(addend) same as multiplyBy, but returns a new list, leaving this list unchanged
dot(other) returns sum of pairwise products of elements in this list and other
reverse mutates this list by reversing the order of its elements
reversed returns a new list containing the elements of this one in reverse order, leaving this list unchanged
apply(func) mutates this list by applying a function to every element, in place
apply1(func, arg) same as apply, but takes one extra argument
mean returns the average (sum divided by number of values) of this list
counts returns a map that maps each unique element of this list, to the number of times that value occurs
distinct returns a list of the unique values in this list (in arbitrary order)
product returns the result of multiplying together all the elements in this list
min returns the minimum value in this list
max returns the maximum value in this list
reduce(func) reduce the list to a single value by applying func to two values at a time
filter(func) mutates this list by removing all elements x where func(x) is not true;

or where x.func is not true (if func is actually a string)

filtered(func) same as filter, but returns a new list, leaving this list unchanged
valuesOf(indexToExtract) returns a list containing elem[indexToExtract] for every element elem
removeVal(val, removeAll=false) mutates this list, removing the first (or all) occurrence of val
any returns a random element of this list

Factory methods

The following methods are also added to the list type, but are meant to be called as list.init, list.init2d</>, and <c>list.init3d rather than being called on an existing list.

Method Returns
init(size, initialValue) list containing size elements with value initialValue
init2d(rows, columns, initialValue) 2D array containing rows rows, each with columns elements with value initialValue
init3d(sizeA, sizeB, sizeC, initialValue) 3D array containing sizeA 2D arrays

See also: matrixUtil

Example

import "listUtil"
print [1, 2, 3].reversed  // prints: [3, 2, 1]