Map::keys and Map::values can be used method-call style.
This commit is contained in:
parent
3b99b8f166
commit
fb05e811b7
@ -79,7 +79,7 @@ for x in range(0, 50, 3) { // step by 3
|
|||||||
Iterate Through Object Maps
|
Iterate Through Object Maps
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
Two functions, `keys` and `values`, return [arrays] containing cloned _copies_
|
Two methods, `keys` and `values`, return [arrays] containing cloned _copies_
|
||||||
of all property names and values of an [object map], respectively.
|
of all property names and values of an [object map], respectively.
|
||||||
|
|
||||||
These [arrays] can be iterated.
|
These [arrays] can be iterated.
|
||||||
@ -88,7 +88,7 @@ These [arrays] can be iterated.
|
|||||||
let map = #{a:1, b:3, c:5, d:7, e:9};
|
let map = #{a:1, b:3, c:5, d:7, e:9};
|
||||||
|
|
||||||
// Property names are returned in unsorted, random order
|
// Property names are returned in unsorted, random order
|
||||||
for x in keys(map) {
|
for x in map.keys() {
|
||||||
if x > 10 { continue; } // skip to the next iteration
|
if x > 10 { continue; } // skip to the next iteration
|
||||||
|
|
||||||
print(x);
|
print(x);
|
||||||
@ -97,7 +97,7 @@ for x in keys(map) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Property values are returned in unsorted, random order
|
// Property values are returned in unsorted, random order
|
||||||
for val in values(map) {
|
for val in map.values() {
|
||||||
print(val);
|
print(val);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -126,11 +126,11 @@ y.remove("a") == 1; // remove property
|
|||||||
y.len() == 2;
|
y.len() == 2;
|
||||||
y.has("a") == false;
|
y.has("a") == false;
|
||||||
|
|
||||||
for name in keys(y) { // get an array of all the property names via the 'keys' function
|
for name in y.keys() { // get an array of all the property names via 'keys'
|
||||||
print(name);
|
print(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
for val in values(y) { // get an array of all the property values via the 'values' function
|
for val in y.values() { // get an array of all the property values via 'values'
|
||||||
print(val);
|
print(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,3 +138,30 @@ y.clear(); // empty the object map
|
|||||||
|
|
||||||
y.len() == 0;
|
y.len() == 0;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
No Support for Property Getters
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
In order not to affect the speed of accessing properties in an object map, new property
|
||||||
|
[getters][getters/setters] cannot be registered because they conflict with the syntax of
|
||||||
|
property access.
|
||||||
|
|
||||||
|
A property [getter][getters/setters] function registered via `Engine::register_get`, for example,
|
||||||
|
for a `Map` will never be found - instead, the property will be looked up in the object map.
|
||||||
|
|
||||||
|
Therefore, _method-call_ notation must be used for built-in properties:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
map.len // access property 'len', returns '()' if not found
|
||||||
|
|
||||||
|
map.len() // returns the number of properties
|
||||||
|
|
||||||
|
map.keys // access property 'keys', returns '()' if not found
|
||||||
|
|
||||||
|
map.keys() // returns array of all property names
|
||||||
|
|
||||||
|
map.values // access property 'values', returns '()' if not found
|
||||||
|
|
||||||
|
map.values() // returns array of all property values
|
||||||
|
```
|
||||||
|
@ -16,7 +16,7 @@ mod inner {
|
|||||||
pub use core_error as error;
|
pub use core_error as error;
|
||||||
|
|
||||||
pub mod collections {
|
pub mod collections {
|
||||||
pub use hashbrown::{HashMap, HashSet};
|
pub use hashbrown::{hash_map, hash_set, HashMap, HashSet};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,10 +61,10 @@ fn test_for_object() -> Result<(), Box<EvalAltResult>> {
|
|||||||
let keys = "";
|
let keys = "";
|
||||||
let map = #{a: 1, b: 2, c: 3};
|
let map = #{a: 1, b: 2, c: 3};
|
||||||
|
|
||||||
for key in keys(map) {
|
for key in map.keys() {
|
||||||
keys += key;
|
keys += key;
|
||||||
}
|
}
|
||||||
for value in values(map) {
|
for value in map.values() {
|
||||||
sum += value;
|
sum += value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user