diff --git a/std.spl b/std.spl index 2755c2d..e61fa49 100644 --- a/std.spl +++ b/std.spl @@ -318,6 +318,21 @@ func nconcat { str | with amt ; _str } +def cached-results MicroMap:new =cached-results +func cache { ... | with arg-amt id body ; + def args arg-amt anew =args + def i arg-amt -- =i + while { i 0 lt not } { + "from stack"; i args:set; + i -- =i + } + def result [ args id ] cached-results:get =result + result null eq if { + args:to-stack body call dup =result [ args id ] swap cached-results:set; + } + result +} + func handle-panic { | with msg trace ; program-name dup if { program-name print " panicked at:" println diff --git a/test.spl b/test.spl index 5448e31..22e66cf 100644 --- a/test.spl +++ b/test.spl @@ -2,6 +2,8 @@ "#stream.spl" import "#http.spl" import +"SPL tester" =program-name + func main { int | with args ; def thing @@ -124,11 +126,23 @@ func main { int | with args ; } "" println + "testing cache" println + 2 cached-test _str println + 3 cached-test _str println + 2 cached-test _str println + 3 cached-test _str println + "" println + catch { "heya" throw } with { with e ; - e:object println + e:message println } 100 } + +func cached-test { mega | 1 "cached-test" cache <{ { mega | with i ; + i 2 * + "calculated " i _str concat println +} } }