Stream: beginners

Topic: ✔ Caching Functions


view this post on Zulip Fletch Canny (Dec 13 2023 at 10:16):

One of the benefits of pure FP is the ability to cache, but I can't seem to find anything in the documentation about it. Is there a way to explicitly tell Roc to cache one of or all functions in an app?
I tried implementing caching with a Cache value cache monad, but (with the problem I was solving, at least- aoc day12) it crashed with a stack overflow.

view this post on Zulip Brendan Hansknecht (Dec 13 2023 at 16:04):

Roc does not have this currently

view this post on Zulip Brendan Hansknecht (Dec 13 2023 at 16:04):

Would need to be done manually with a dictionary

view this post on Zulip Brendan Hansknecht (Dec 13 2023 at 16:06):

Recently we realized that we have a bug in code generation related to the stack that makes stack overflows more common. So you may have it that even if your function was tail recursive

view this post on Zulip Fletch Canny (Dec 14 2023 at 01:26):

Oh okay, a compiler bug makes more sense. Thanks for the reply!
I'd like to make an interface like so

interface Cache
  exposes []
  imports []
Cache a cache := (cache -> (a, cache))
map : Cache a cache, (a -> b) -> Cache b cache
updateCache : Cache r cache, (cache -> cache) -> Cache r cache
andThen : Cache a cache, (a -> Cache b cache) -> Cache b cache
sequence : List ({} -> Cache a cache) -> Cache (List a) cache
store : cache -> Cache {} cache
ignore : a -> Cache a cache
get : Cache cache cache
run : Cache r cache, cache -> r

Where the elements of sequence are thunked to optimize recursive functions, though I suppose that won't be necessary when function caching is implemented.
I'll mark this thread as solved.

view this post on Zulip Notification Bot (Dec 14 2023 at 01:26):

Fletch Canny has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC