From 8feb9819b8cb3736bb2e9db8b629ef69141b32b1 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sat, 27 Jun 2015 12:03:55 -0400 Subject: [PATCH] Fix my very very silly mistake of makeing keys and values the same type in map, also add a map.for_each function --- stdlib/map.krak | 6 +++++- tests/test_map.expected_results | 5 +++++ tests/test_map.krak | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/stdlib/map.krak b/stdlib/map.krak index 57af3de..164b169 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -12,7 +12,7 @@ fun map(key:T, value:U): map { obj map { var keys: vector::vector - var values: vector::vector + var values: vector::vector fun construct() { keys.construct() @@ -47,5 +47,9 @@ obj map { fun operator[](key: T): U { return get(key) } + fun for_each(func: fun(T, U):void) { + for (var i = 0; i < keys.size; i++;) + func(keys[i], values[i]) + } } diff --git a/tests/test_map.expected_results b/tests/test_map.expected_results index 970c96a..d7bb831 100644 --- a/tests/test_map.expected_results +++ b/tests/test_map.expected_results @@ -6,3 +6,8 @@ 2 30 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 diff --git a/tests/test_map.krak b/tests/test_map.krak index e0f78f7..1fc482a 100644 --- a/tests/test_map.krak +++ b/tests/test_map.krak @@ -16,5 +16,11 @@ fun main():int { println(m[2]) println(m[6]) 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 }