core.memoize is a memoization library offering functionality above
Clojure's core memoize
function in the following ways:
Pluggable memoization
core.memoize allows for different back-end cache implmentations to
be used as appropriate without changing the memoization modus operandi.
See the memoizer
function.
Manipulable memoization
Because core.memoize allows you to access a function's memoization store, you do interesting things like clear it, modify it, and save it for later.
core.memoize is a memoization library offering functionality above Clojure's core `memoize` function in the following ways: **Pluggable memoization** core.memoize allows for different back-end cache implmentations to be used as appropriate without changing the memoization modus operandi. See the `memoizer` function. **Manipulable memoization** Because core.memoize allows you to access a function's memoization store, you do interesting things like clear it, modify it, and save it for later.
(build-memoizer cache-factory f & args)
Builds a function that, given a function, returns a pluggable memoized
version of it. build-memoizer
takes a cache factory function, and the
arguments to that factory function -- at least one of those arguments
should be the function to be memoized (it's usually the first argument).
memoizer
above is a simpler version of build-memoizer
that 'does the
right thing' with a cache and a seed hash map. build-memoizer
remains
for backward compatibility but should be considered deprecated.
Builds a function that, given a function, returns a pluggable memoized version of it. `build-memoizer` takes a cache factory function, and the arguments to that factory function -- at least one of those arguments should be the function to be memoized (it's usually the first argument). `memoizer` above is a simpler version of `build-memoizer` that 'does the right thing' with a cache and a seed hash map. `build-memoizer` remains for backward compatibility but should be considered deprecated.
(fifo f)
(fifo f base)
(fifo f tkey threshold)
(fifo f base key threshold)
Works the same as the basic memoization function (i.e. memo
and core.memoize
except when a given threshold is breached.
Observe the following:
(require '[clojure.core.memoize :as memo])
(def id (memo/fifo identity :fifo/threshold 2))
(id 42)
(id 43)
(snapshot id)
;=> {[42] 42, [43] 43}
As you see, the limit of 2
has not been breached yet, but
if you call again with another value, then it is:
(id 44)
(snapshot id)
;=> {[44] 44, [43] 43}
That is, the oldest entry 42
is pushed out of the
memoization cache. This is the standard First In
First Out behavior.
Works the same as the basic memoization function (i.e. `memo` and `core.memoize` except when a given threshold is breached. Observe the following: (require '[clojure.core.memoize :as memo]) (def id (memo/fifo identity :fifo/threshold 2)) (id 42) (id 43) (snapshot id) ;=> {[42] 42, [43] 43} As you see, the limit of `2` has not been breached yet, but if you call again with another value, then it is: (id 44) (snapshot id) ;=> {[44] 44, [43] 43} That is, the oldest entry `42` is pushed out of the memoization cache. This is the standard **F**irst **I**n **F**irst **O**ut behavior.
(lazy-snapshot memoized-fn)
Returns a lazy snapshot of a core.memo-placed memoization cache. By lazy snapshot you can infer that what you get is only the cache contents at a moment in time -- and, being lazy, the cache could change while you are realizing the snapshot elements.
Returns a sequence of key/value pairs.
Returns a lazy snapshot of a core.memo-placed memoization cache. By lazy snapshot you can infer that what you get is only the cache contents at a moment in time -- and, being lazy, the cache could change while you are realizing the snapshot elements. Returns a sequence of key/value pairs.
(lru f)
(lru f base)
(lru f tkey threshold)
(lru f base key threshold)
Works the same as the basic memoization function (i.e. memo
and core.memoize
except when a given threshold is breached.
Observe the following:
(require '[clojure.core.memoize :as memo])
(def id (memo/lru identity :lru/threshold 2))
(id 42)
(id 43)
(snapshot id)
;=> {[42] 42, [43] 43}
At this point the cache has not yet crossed the set threshold
of 2
, but if you execute yet another call the story will
change:
(id 44)
(snapshot id)
;=> {[44] 44, [43] 43}
At this point the operation of the LRU cache looks exactly the same at the FIFO cache. However, the difference becomes apparent on further use:
(id 43)
(id 0)
(snapshot id)
;=> {[0] 0, [43] 43}
As you see, once again calling id
with the argument 43
will expose the LRU nature of the underlying cache. That is,
when the threshold is passed, the cache will expel the
Least Recently Used element in favor of the new.
Works the same as the basic memoization function (i.e. `memo` and `core.memoize` except when a given threshold is breached. Observe the following: (require '[clojure.core.memoize :as memo]) (def id (memo/lru identity :lru/threshold 2)) (id 42) (id 43) (snapshot id) ;=> {[42] 42, [43] 43} At this point the cache has not yet crossed the set threshold of `2`, but if you execute yet another call the story will change: (id 44) (snapshot id) ;=> {[44] 44, [43] 43} At this point the operation of the LRU cache looks exactly the same at the FIFO cache. However, the difference becomes apparent on further use: (id 43) (id 0) (snapshot id) ;=> {[0] 0, [43] 43} As you see, once again calling `id` with the argument `43` will expose the LRU nature of the underlying cache. That is, when the threshold is passed, the cache will expel the **L**east **R**ecently **U**sed element in favor of the new.
(lu f)
(lu f base)
(lu f tkey threshold)
(lu f base key threshold)
Similar to the implementation of memo-lru, except that this function removes all cache values whose usage value is smallest:
(require '[clojure.core.memoize :as memo])
(def id (memo/lu identity :lu/threshold 3))
(id 42)
(id 42)
(id 43)
(id 44)
(snapshot id)
;=> {[44] 44, [42] 42}
The Least Used values are cleared on cache misses.
Similar to the implementation of memo-lru, except that this function removes all cache values whose usage value is smallest: (require '[clojure.core.memoize :as memo]) (def id (memo/lu identity :lu/threshold 3)) (id 42) (id 42) (id 43) (id 44) (snapshot id) ;=> {[44] 44, [42] 42} The **L**east **U**sed values are cleared on cache misses.
(memo f)
(memo f seed)
Used as a more flexible alternative to Clojure's core memoization
function. Memoized functions built using memo
will respond to
the core.memo manipulable memoization utilities. As a nice bonus,
you can use memo
in place of memoize
without any additional
changes, with the added guarantee that the memoized function will
only be called once for a given sequence of arguments (memoize
can call the function multiple times when concurrent calls are
made with the same sequence of arguments).
The default way to use this function is to simply supply a function
that will be memoized. Additionally, you may also supply a map
of the form '{[42] 42, [108] 108}
where keys are a vector
mapping expected argument values to arity positions. The map values
are the return values of the memoized function.
If the supplied function has metadata containing an
:clojure.core.memoize/args-fn
key, the value is assumed to be a
function that should be applied to the arguments to produce a
subset or transformed sequence of arguments that are used for the
key in the cache (the full, original arguments will still be used
to call the function). This allows you to memoize functions where
one or more arguments are irrelevant for memoization, such as the
clojure.java.jdbc
functions, whose first argument may include
a (mutable) JDBC Connection
object:
(memo/memo (with-meta jdbc/execute! {::memo/args-fn rest}))
You can access the memoization cache directly via the :clojure.core.memoize/cache
key
on the memoized function's metadata. However, it is advised to
use the core.memo primitives instead as implementation details may
change over time.
Used as a more flexible alternative to Clojure's core `memoization` function. Memoized functions built using `memo` will respond to the core.memo manipulable memoization utilities. As a nice bonus, you can use `memo` in place of `memoize` without any additional changes, with the added guarantee that the memoized function will only be called once for a given sequence of arguments (`memoize` can call the function multiple times when concurrent calls are made with the same sequence of arguments). The default way to use this function is to simply supply a function that will be memoized. Additionally, you may also supply a map of the form `'{[42] 42, [108] 108}` where keys are a vector mapping expected argument values to arity positions. The map values are the return values of the memoized function. If the supplied function has metadata containing an `:clojure.core.memoize/args-fn` key, the value is assumed to be a function that should be applied to the arguments to produce a subset or transformed sequence of arguments that are used for the key in the cache (the full, original arguments will still be used to call the function). This allows you to memoize functions where one or more arguments are irrelevant for memoization, such as the `clojure.java.jdbc` functions, whose first argument may include a (mutable) JDBC `Connection` object: (memo/memo (with-meta jdbc/execute! {::memo/args-fn rest})) You can access the memoization cache directly via the `:clojure.core.memoize/cache` key on the memoized function's metadata. However, it is advised to use the core.memo primitives instead as implementation details may change over time.
(memo-clear! f)
(memo-clear! f args)
Reaches into an core.memo-memoized function and clears the cache. This is a destructive operation and should be used with care.
When the second argument is a vector of input arguments, clears cache only for argument vector.
Keep in mind that depending on what other threads or doing, an
immediate call to snapshot
may not yield an empty cache. That's
cool though, we've learned to deal with that stuff in Clojure by
now.
Reaches into an core.memo-memoized function and clears the cache. This is a destructive operation and should be used with care. When the second argument is a vector of input arguments, clears cache only for argument vector. Keep in mind that depending on what other threads or doing, an immediate call to `snapshot` may not yield an empty cache. That's cool though, we've learned to deal with that stuff in Clojure by now.
(memo-fifo f)
(memo-fifo f limit)
(memo-fifo f limit base)
DEPRECATED: Please use clojure.core.memoize/fifo instead.
DEPRECATED: Please use clojure.core.memoize/fifo instead.
(memo-lru f)
(memo-lru f limit)
(memo-lru f limit base)
DEPRECATED: Please use clojure.core.memoize/lru instead.
DEPRECATED: Please use clojure.core.memoize/lru instead.
(memo-lu f)
(memo-lu f limit)
(memo-lu f limit base)
DEPRECATED: Please use clojure.core.memoize/lu instead.
DEPRECATED: Please use clojure.core.memoize/lu instead.
(memo-reset! f base)
Takes a core.memo-populated function and a map and replaces the memoization cache with the supplied map. This is potentially some serious voodoo, since you can effectively change the semantics of a function on the fly.
(def id (memo identity))
(memo-swap! id '{[13] :omg})
(id 13)
;=> :omg
With great power comes ... yadda yadda yadda.
Takes a core.memo-populated function and a map and replaces the memoization cache with the supplied map. This is potentially some serious voodoo, since you can effectively change the semantics of a function on the fly. (def id (memo identity)) (memo-swap! id '{[13] :omg}) (id 13) ;=> :omg With great power comes ... yadda yadda yadda.
(memo-swap! f base)
(memo-swap! f swap-fn args & results)
The 2-arity version takes a core.memo-populated function and a map and
replaces the memoization cache with the supplied map. Use memo-reset!
instead for replacing the cache as this 2-arity version of memo-swap!
should be considered deprecated.
The 3+-arity version takes a core.memo-populated function and arguments
similar to what you would pass to clojure.core/swap!
and performs a
swap!
on the underlying cache. In order to satisfy core.memoize's
world view, the assumption is that you will generally be calling it like:
(def id (memo identity))
(memo-swap! id clojure.core.cache/miss [13] :omg)
(id 13)
;=> :omg
You'll nearly always use clojure.core.cache/miss
for this operation but
you could pass any function that would work on an immutable cache, such
as evict
or assoc
etc.
Be aware that memo-swap!
assumes it can wrap each of the results
values
in a delay
so that items conform to clojure.core.memoize
's world view.
The 2-arity version takes a core.memo-populated function and a map and replaces the memoization cache with the supplied map. Use `memo-reset!` instead for replacing the cache as this 2-arity version of `memo-swap!` should be considered deprecated. The 3+-arity version takes a core.memo-populated function and arguments similar to what you would pass to `clojure.core/swap!` and performs a `swap!` on the underlying cache. In order to satisfy core.memoize's world view, the assumption is that you will generally be calling it like: (def id (memo identity)) (memo-swap! id clojure.core.cache/miss [13] :omg) (id 13) ;=> :omg You'll nearly always use `clojure.core.cache/miss` for this operation but you could pass any function that would work on an immutable cache, such as `evict` or `assoc` etc. Be aware that `memo-swap!` assumes it can wrap each of the `results` values in a `delay` so that items conform to `clojure.core.memoize`'s world view.
(memo-ttl f)
(memo-ttl f limit)
(memo-ttl f limit base)
DEPRECATED: Please use clojure.core.memoize/ttl instead.
DEPRECATED: Please use clojure.core.memoize/ttl instead.
(memoized? f)
Returns true if a function has an core.memo-placed cache, false otherwise.
Returns true if a function has an core.memo-placed cache, false otherwise.
(memoizer f cache)
(memoizer f cache seed)
Build a pluggable memoized version of a function. Given a function and a (pluggable memoized) cache, and an optional seed (hash map of arguments to return values), return a cached version of that function.
If you want to build your own cached function, perhaps with combined caches or customized caches, this is the preferred way to do so now.
Build a pluggable memoized version of a function. Given a function and a (pluggable memoized) cache, and an optional seed (hash map of arguments to return values), return a cached version of that function. If you want to build your own cached function, perhaps with combined caches or customized caches, this is the preferred way to do so now.
(snapshot memoized-fn)
Returns a snapshot of a core.memo-placed memoization cache. By snapshot you can infer that what you get is only the cache contents at a moment in time.
Returns a snapshot of a core.memo-placed memoization cache. By snapshot you can infer that what you get is only the cache contents at a moment in time.
(ttl f)
(ttl f base)
(ttl f tkey threshold)
(ttl f base key threshold)
Unlike many of the other core.memo memoization functions,
memo-ttl
's cache policy is time-based rather than algorithmic
or explicit. When memoizing a function using memo-ttl
you
should provide a Time To Live parameter in
milliseconds.
(require '[clojure.core.memoize :as memo])
(def id (memo/ttl identity :ttl/threshold 5000))
(id 42)
(snapshot id)
;=> {[42] 42}
... wait 5 seconds ...
(id 43)
(snapshot id)
;=> {[43] 43}
The expired cache entries will be removed on each cache miss.
Unlike many of the other core.memo memoization functions, `memo-ttl`'s cache policy is time-based rather than algorithmic or explicit. When memoizing a function using `memo-ttl` you should provide a **T**ime **T**o **L**ive parameter in milliseconds. (require '[clojure.core.memoize :as memo]) (def id (memo/ttl identity :ttl/threshold 5000)) (id 42) (snapshot id) ;=> {[42] 42} ... wait 5 seconds ... (id 43) (snapshot id) ;=> {[43] 43} The expired cache entries will be removed on each cache **miss**.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close