Jump to content

Create an object/Native demonstration: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: Fix link: Perl 6 --> Raku)
(Added Wren)
Line 1,233:
trying: unset dic
dic:1 one 2 two
 
=={{header|Wren}}==
<lang ecmascript>class FixedSizeMap {
construct new(map) {
// copy the map so it cannot be mutated from the original reference
_map = {}
for (me in map.toList) _map[me.key] = me.value
}
 
containsKey(key) { _map[key] != null }
 
count { _map.count }
 
keys { _map.keys }
 
values { _map.values }
 
[key] { _map[key] }
[key] =(value) { _map[key] = value }
 
reset(key) {
var t = _map[key].type
_map[key] = (t == Num) ? 0 :
(t == String) ? "":
(t == Bool) ? false :
(t == List) ? [] :
(t == Map) ? {} : Fiber.abort("No suitable default value for type %(t)")
}
 
iterate(iterator) { _map.iterate(iterator) }
iteratorValue(iterator) { _map.iteratorValue(iterator) }
 
toString { _map.toString }
}
 
var map = { "a": 1, "b": 2 }
var fsm = FixedSizeMap.new(map)
System.print(fsm)
System.print(fsm.count)
fsm["a"] = 3
fsm["b"] = 4
System.print(fsm)
System.print(fsm.containsKey("c"))
fsm.reset("a")
System.print(fsm)
System.print(fsm.keys.toList)
System.print(fsm.values.toList)
for (me in fsm) System.print([me.key, me.value])</lang>
 
{{out}}
<pre>
{b: 2, a: 1}
2
{b: 4, a: 3}
false
{b: 4, a: 0}
[b, a]
[4, 0]
[b, 4]
[a, 0]
</pre>
 
=={{header|zkl}}==
9,483

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.