Fix my very very silly mistake of makeing keys and values the same type in map, also add a map.for_each function
This commit is contained in:
@@ -12,7 +12,7 @@ fun map<T,U>(key:T, value:U): map<T,U> {
|
|||||||
|
|
||||||
obj map<T,U> {
|
obj map<T,U> {
|
||||||
var keys: vector::vector<T>
|
var keys: vector::vector<T>
|
||||||
var values: vector::vector<T>
|
var values: vector::vector<U>
|
||||||
|
|
||||||
fun construct() {
|
fun construct() {
|
||||||
keys.construct()
|
keys.construct()
|
||||||
@@ -47,5 +47,9 @@ obj map<T,U> {
|
|||||||
fun operator[](key: T): U {
|
fun operator[](key: T): U {
|
||||||
return get(key)
|
return get(key)
|
||||||
}
|
}
|
||||||
|
fun for_each(func: fun(T, U):void) {
|
||||||
|
for (var i = 0; i < keys.size; i++;)
|
||||||
|
func(keys[i], values[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,3 +6,8 @@
|
|||||||
2
|
2
|
||||||
30
|
30
|
||||||
4
|
4
|
||||||
|
Lookie, a map!
|
||||||
|
What I get for not testing different types
|
||||||
|
key: 7, value: Lookie, a map!
|
||||||
|
key: 20, value: What I get for not testing different types
|
||||||
|
key: 30, value: we'll look for for_each too
|
||||||
|
|||||||
@@ -16,5 +16,11 @@ fun main():int {
|
|||||||
println(m[2])
|
println(m[2])
|
||||||
println(m[6])
|
println(m[6])
|
||||||
println(m[3])
|
println(m[3])
|
||||||
|
var mapEx = map(7, "Lookie, a map!")
|
||||||
|
mapEx[20] = "What I get for not testing different types"
|
||||||
|
mapEx[30] = "we'll look for for_each too"
|
||||||
|
println(mapEx[7])
|
||||||
|
println(mapEx[20])
|
||||||
|
mapEx.for_each(fun(key:int, value:char*) { print("key: "); print(key); print(", value: "); println(value); })
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user