MapUtil

From MiniScript Wiki
Jump to navigation Jump to search

In Mini Micro, mapUtil is an import module in the /sys/lib directory. It provides various additional map-related functions, directly extending the map datatype.

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

Constants

The following values should be normally be prefixed with mapUtil, e.g., mapUtil.fromTo.

Name Value / Purpose
fromTo generate a map from one sequence (list or string) to another. The two sequences must be the same length.

Added map methods

The following methods are added to the map type, and so are accessed using dot syntax after any map, for example: {"Hello": "World"}.inverse

Method Returns
get(key, defaultValue=null) look up the value for an index in this map, but if the given index is not found, return a default value instead. Works with inherited values, too.
hasValue(v, includeInherited=false) return true if this map contains the given value as a value (compare with built-in function .hasIndex, which checks whether this map has a given value as a key). By default this

does not walk the inheritance chain (just like .hasIndex), but if you pass true as the second parameter, then it does.

sortedIndexes get the indexes of this map, but in sorted order (rather than undefined order as .indexes gives you).
inverse return a new map that has the keys and values swapped (keys of this map become values of the result map, and vice versa).
filterIndexes(func) remove any indexes for which the given function does not return true.
filterValues(func) remove any key/value pairs for which the function, applied to the value, does not return true.
applyToValues(func) apply the given function to all values.
pairs get all key/value pairs as a list (in arbitrary order) of little maps with "key" and "value".
swap(key1, key2) swap the values of two keys. Note that this may be applied to locals (or globals), enabling you to swap the values of two variables.

Example

import "mapUtil"
print {"Hello": "World"}.inverse //prints {"World": "Hello"}