Category talk:Wren-maputil
Source code
<lang ecmascript>/* Module "maputil.wren" */
import "./check" for Check
/* MapUtil supplements the Map class with some other operations on maps. */ class MapUtil {
// Creates a new Map from lists of keys and values // which must be of the same length. static create(keys, values) { Check.list("keys", keys) var count = keys.count Check.list("values", values, count, count) var m = {} for (i in 0...count) m[keys[i]] = values[i] return m }
// Creates a new Map from a list of keys and assigns // the same value to all of them. static createSame(keys, value) { Check.list("keys", keys) var m = {} for (key in keys) m[key] = value return m }
// Adds entries to an existing Map from lists of keys and values // which must be of the same length. Returns the Map after the additions. static addAll(map, keys, values) { Check.map("map", map) Check.list("keys", keys) var count = keys.count Check.list("values", values, count, count) for (i in 0...count) map[keys[i]] = values[i] return map }
// Adds entries to an existing Map from a list of keys and assigns // the same value to all of them. Returns the Map after the additions. static addAllSame(map, keys, value) { Check.map("map", map) Check.list("keys", keys) for (key in keys) map[key] = value return map }
// Returns true if all keys in the 'keys' list are keys of an existing map // or false otherwise. static containsAll(map, keys) { Check.map("map", map) Check.list("keys", keys) for (key in keys) { if (!map[key]) return false } return true }
// Returns true if any key in the 'keys' list is a key of an existing map // or false otherwise. static containsAny(map, keys) { Check.map("map", map) Check.list("keys", keys) for (key in keys) { if (map[key]) return true } return false }
// Returns true if no key in the 'keys' list is a key of an existing map // or false otherwise. static containsNone(map, keys) { !containsAny(map, keys) }
// Removes all keys in the 'keys' list from an existing Map // and returns a list of the associated values removed. static removeAll(map, keys) { Check.map("map", map) if (map.isEmpty) return [] Check.list("keys", keys) var removals = [] for (key in keys) { if (map.containsKey(key)) removals.add(map.remove(key)) } return removals }
// Removes all entries from 'map' whose key satisfies the predicate // function 'fn' and returns a list of the associated values removed. static removeBy(map, fn) { Check.map("map", map) if (map.isEmpty) return [] var removals = [] for (key in map.keys) { if (fn.call(key)) removals.add(map.remove(key)) } return removals }
// If 'key' exists in 'map' increases its value by 'inc'. // Otherwise creates a new entry with that key and a value of 1. // Map values must be numeric. Returns 'map' after the change. static increase(map, key, inc) { Check.map("map", map) Check.nonNegNum("inc", inc) if (map.containsKey(key)) { map[key] = map[key] + inc } else { map[key] = 1 } return map }
// If 'key' exists in 'map' decreases its value by 'dec'. // If the resulting value is <= 0, the entry is removed from the map. // Map values must be numeric. Returns 'map' after the change. static decrease(map, key, dec) { Check.map("map", map) Check.nonNegNum("dec", dec) if (map.containsKey(key)) { if (map[key] <= dec) { map.remove(key) } else { map[key] = map[key] - dec } } return map }
// Convenience versions of the above methods where inc/dec is always 1. static increase(map, key) { increase(map, key, 1) } static decrease(map, key) { decrease(map, key, 1) }
// Sorts copies of the MapEntries of 'map' into key order using the default comparer // function: {|a, b| a < b } and returns a sequence (not list) of them. // Doesn't affect 'map' itself but enables it to be iterated in sorted order. static sort(map) { Check.map("map", map) return map.keys.toList.sort().map { |k| MapEntry.new(k, map[k]) } }
// Sorts copies of the MapEntries of 'map' into key order using a // comparison function 'comparer' and returns a sequence (not list) of them. // Doesn't affect 'map' itself but enables it to be iterated in sorted order. static sort(map, comparer) { Check.map("map", map) return map.keys.toList.sort(comparer).map { |k| MapEntry.new(k, map[k]) } }
}</lang>