Skip to content

Map operators

Operators on hash maps.

Every program can use these operators. No import is necessary.

Operator Arguments Gives
def varname: [K:T] void
for hash: [string:T], itemName: string, repeat_block: block void
for hash: [string:T], item: T, itemName: string, repeat_block: block void
get map: [K:boolean], key: K <optional>boolean
get map: [K:double], key: K <optional>double
get map: [K:int], key: K <optional>int
get map: [K:T], key: K <optional>T
has map: [K:T], key: K boolean
keys map: [string:T] [string]
map_clear map: [K:T] void
set map: [K:T], key: K, value: T void

Target support marks

  • Own template
  • Default template
  • No template

The operator works for a target with an own template and for a target with the default template. The default template is the * entry of the definition: it writes the same code for every target that has no template of its own. A target with no template writes no code, and a program that uses the operator does not compile for that target.

def

(def varname) → void
def(varname: [K:T])

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4367

for

(for hash itemName repeat_block) → void
for(hash: [string:T], itemName: string, repeat_block: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5913

for

(for hash item itemName repeat_block) → void
for(hash: [string:T], item: T, itemName: string, repeat_block: block)

Walking a hash map used to be an es6 only operator: the two `for` shapes below carried one template each, so every other target answered `Could not match argument types for for` and a program that iterated a map compiled on JavaScript alone.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5886

get

(get map key) → <optional>boolean
get(map: [K:boolean], key: K)

`bool` and `null` have no common type, so the generic overload's conditional does not compile for a map of booleans -- the int and the double overloads above carry a cast for exactly this reason. Only C# has an entry here, so every other target keeps reaching the generic overload below.

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: no template
  • Rust: own template
  • Python: no template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: own template
  • C++: no template
  • PHP: no template
  • Scala: no template

Definition: compiler/Lang.rgr, line 9168

get

(get map key) → <optional>double
get(map: [K:double], key: K)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: default template
  • Rust: own template
  • Python: own template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 9107

get

(get map key) → <optional>int
get(map: [K:int], key: K)

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: no template
  • Rust: own template
  • Python: own template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: own template
  • C++: own template
  • PHP: no template
  • Scala: no template

Write and read a hash map

docs/examples/core/map/map-basics.rgr
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def age (?? (get ages "ada") 0)
print ("ada " + age)
if (has ages "alan") {
print "alan is in the map"
}
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
}
__js_main();

Definition: compiler/Lang.rgr, line 9051

get

(get map key) → <optional>T
get(map: [K:T], key: K)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: own template

Definition: compiler/Lang.rgr, line 9174

has

(has map key) → boolean
has(map: [K:T], key: K)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Write and read a hash map

docs/examples/core/map/map-basics.rgr
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def age (?? (get ages "ada") 0)
print ("ada " + age)
if (has ages "alan") {
print "alan is in the map"
}
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
}
__js_main();

Definition: compiler/Lang.rgr, line 9011

keys

(keys map) → [string]
keys(map: [string:T])

https://stackoverflow.com/questions/41690156/how-to-get-the-keys-as-string-array-from-map-in-go-lang https://stackoverflow.com/questions/110157/how-to-retrieve-all-keys-or-values-from-a-stdmap-and-put-them-into-a-vector

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 7786

map_clear

(map_clear map) → void
map_clear(map: [K:T])

Remove every entry of a map IN PLACE. On C++ this keeps the rg_ordered_map's buffers (capacity survives a clear), which is what makes pooled scopes reusable without re-allocating their tables.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: default template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 10122

set

(set map key value) → void
set(map: [K:T], key: K, value: T)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: own template
  • Python: default template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: own template
  • PHP: own template
  • Scala: own template

Write and read a hash map

docs/examples/core/map/map-basics.rgr
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def age (?? (get ages "ada") 0)
print ("ada " + age)
if (has ages "alan") {
print "alan is in the map"
}
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const age = (( Object.prototype.hasOwnProperty.call(ages, "ada") ? ages["ada"] : undefined ) ?? 0);
console.log("ada " + age);
if ( ( typeof(ages["alan"] ) != "undefined" && Object.prototype.hasOwnProperty.call(ages, "alan") ) ) {
console.log("alan is in the map");
}
}
__js_main();

Definition: compiler/Lang.rgr, line 9298

Ranger 3.5.1 · commit f323fd4 · development build