One rather complex procedure is cache, which creates a procedure that generates and caches a value. cache takes a function to generate the cache lifetime, as well as a function to generate the value to cache.
The value function is executed the first time the created procedure is called, and will be executed again only if the created procedure is called when the cache lifetime has expired. Otherwise, the cached value is returned. Typically a cache is used when the value function is something expensive to evaluate.
The motivation behind dynamically computing the cache lifetime is that the lifetime can be modified for an existing cache, for instance through a global variable.
arc> (= mycache (cache (fn () 10) (fn () (prn "evaluated") (tostring (system "date"))))) #<procedure> arc> (mycache) evaluated "Mon Mar 10 21:08:33 PDT 2008\n" arc> (mycache) "Mon Mar 10 21:08:33 PDT 2008\n" arc> (mycache) evaluated "Mon Mar 10 21:08:46 PDT 2008\n"
>(date) 2008年02月20日
>(seconds)
1535272001
>(msec)
1535272001424
>(timedate)
(41 26 8 26 8 2018)
>(timedate 0)
(0 0 0 1 1 1970)
>(current-gc-milliseconds)
324
>(current-process-milliseconds)
6392
t0. The base time t0 should come from seconds.
>(let t0 (seconds) (sleep 1) (since t0))
1
t0. New in arc3.
>(let t0 (seconds) (sleep 1) (minutes-since t0))
1/60
t0.
>(let t0 (seconds) (sleep 1) (hours-since t0))
1/3600
t0.
>(let t0 (seconds) (sleep 1) (days-since t0))
1/86400
>(datestring)
"2018-08-26"
>(datestring 0)
"1970-01-01"
expr and prints how long it took to execute.>(time (sleep 0.1)) time: 101 msec. nil
expr, prints how long it took to execute, and returns the symbol ok.>(jtime (sleep 0.1)) time: 100 msec. ok
expr 10 times and prints how long it took in total to execute.>(time10 (sleep 0.1)) time: 1001 msec. nil
timef is a function that returns the cache lifetime in seconds. valf is a function that returns the value to be cached.
>(cache (fn () 5) (fn () "val"))
#<procedure: cache>
>(defcache foo 300 "val")
#<procedure: cache>
Copyright 2008 Ken Shirriff.