Difference between revisions of "ListUtil"

From MiniScript Wiki
Jump to navigation Jump to search
m (fixed ending code tag)
(Added missing methods)
 
(2 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
|-
 
|-
 
| contains(''item'') || true if this list contains the given ''item''
 
| contains(''item'') || true if this list contains the given ''item''
 +
|-
 +
| lastIndexOf(''element'', ''beforeIdx''=null) || returns the *last* index of the given element, optionally before a given index.  Returns null if not found.
 +
|-
 +
| split(''delimiter'', ''maxCount''=null) || similar to string.split, this splits a list into sub-lists by where the given delimiter element is found.
 
|-
 
|-
 
| deepCopy || returns a clone of this list, recursively cloning any sub-lists as well
 
| deepCopy || returns a clone of this list, recursively cloning any sub-lists as well
Line 34: Line 38:
 
|-
 
|-
 
| apply1(''func'', ''arg'') || same as <c>apply</c>, but takes one extra argument
 
| apply1(''func'', ''arg'') || same as <c>apply</c>, but takes one extra argument
 +
|-
 +
| applied(''func'') || same as apply but returns a new list
 +
|-
 +
| applied1(''func'', ''arg'') || same as apply1 but returns a new list
 
|-
 
|-
 
| mean || returns the average (sum divided by number of values) of this list
 
| mean || returns the average (sum divided by number of values) of this list
Line 51: Line 59:
 
| filter(''func'') || mutates this list by removing all elements ''x'' where ''func(x)'' is not true;
 
| 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)
 
or where ''x.func'' is not true (if ''func'' is actually a string)
 +
|-
 +
| filter1(''func'', ''arg'') || same as ''filter'' but takes one extra argument.
 
|-
 
|-
 
| filtered(''func'') || same as ''filter'', but returns a new list, leaving this list unchanged
 
| filtered(''func'') || same as ''filter'', but returns a new list, leaving this list unchanged
 +
|-
 +
| filtered1(''func'', ''arg'') || same as ''filtered'' but takes one extra argument.
 +
|-
 +
| compress(''valueToCompress'') || replace each run of a given value with a single instance of that value (in place).
 +
|-
 +
| compressed(''valueToCompress'') || same as ''compress' but returns a new list.
 
|-
 
|-
 
| valuesOf(''indexToExtract'') || returns a list containing ''elem[indexToExtract]'' for every element ''elem''
 
| valuesOf(''indexToExtract'') || returns a list containing ''elem[indexToExtract]'' for every element ''elem''
Line 59: Line 75:
 
|-
 
|-
 
| any || returns a random element of this list
 
| any || returns a random element of this list
 +
|-
 +
| swap(''index1'', ''index2'') || exchange two values in place, by their indexes
 
|}
 
|}
  
Line 84: Line 102:
  
  
 +
[[Category:Sys Modules]]
 
[[Category:Mini Micro]]
 
[[Category:Mini Micro]]
 +
[[Category:Command-Line_MiniScript]]

Latest revision as of 02:27, 5 April 2025

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
lastIndexOf(element, beforeIdx=null) returns the *last* index of the given element, optionally before a given index. Returns null if not found.
split(delimiter, maxCount=null) similar to string.split, this splits a list into sub-lists by where the given delimiter element is found.
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
applied(func) same as apply but returns a new list
applied1(func, arg) same as apply1 but returns a new list
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)

filter1(func, arg) same as filter but takes one extra argument.
filtered(func) same as filter, but returns a new list, leaving this list unchanged
filtered1(func, arg) same as filtered but takes one extra argument.
compress(valueToCompress) replace each run of a given value with a single instance of that value (in place).
compressed(valueToCompress) same as compress' but returns a new list.
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
swap(index1, index2) exchange two values in place, by their indexes

Factory methods

The following methods are also added to the list type, but are meant to be called as list.init, list.init2d, and 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]