Combined interface to everything in Methodical you'd normally want to use.
Combined interface to everything in Methodical you'd normally want to use.
(+-method-combination)
Executes all applicable primary methods, returnings the sum of the values returned by each method. Same constraints as other CLOS operator-style method combinations.
Executes *all* applicable primary methods, returnings the sum of the values returned by each method. Same constraints as other CLOS operator-style method combinations.
(add-aux-method method-table qualifier dispatch-value f)
Add an auxiliary method implementation for qualifier
(e.g. :before
) and dispatch-value
. Unlike primary
methods, auxiliary methods are not limited to one method per dispatch value; thus this method does not remove
existing methods for this dispatch value.
Add an auxiliary method implementation for `qualifier` (e.g. `:before`) and `dispatch-value`. Unlike primary methods, auxiliary methods are not limited to one method per dispatch value; thus this method does not remove existing methods for this dispatch value.
(add-aux-method! multifn-var qualifier dispatch-val f)
Destructive version of [[methodical.interface/add-aux-method]]. Operates on a var defining a Methodical multifn.
Destructive version of [[methodical.interface/add-aux-method]]. Operates on a var defining a Methodical multifn.
(add-aux-method-with-unique-key multifn qualifier dispatch-val f unique-key)
Adds an auxiliary method with a unique-key
stored in its metadata. This unique key can later be used to remove the
auxiliary method with remove-aux-method-with-unique-key
. If a method with this key already exists for this
qualifier and dispatch value, replaces the original.
Adds an auxiliary method with a `unique-key` stored in its metadata. This unique key can later be used to remove the auxiliary method with [[remove-aux-method-with-unique-key]]. If a method with this key already exists for this qualifier and dispatch value, replaces the original.
(add-aux-method-with-unique-key! multifn-var
qualifier
dispatch-val
f
unique-key)
Destructive version of add-aux-method-with-unique-key
. Operates on a var defining a Methodical multifn.
Destructive version of [[add-aux-method-with-unique-key]]. Operates on a var defining a Methodical multifn.
(add-primary-method method-table dispatch-value f)
Set the primary method implementation for dispatch-value
, replacing it if it already exists.
Set the primary method implementation for `dispatch-value`, replacing it if it already exists.
(add-primary-method! multifn-var dispatch-val f)
Destructive version of add-primary-method
. Operates on a var defining a Methodical multifn.
Destructive version of [[add-primary-method]]. Operates on a var defining a Methodical multifn.
(allowed-qualifiers method-combination)
The set containing all qualifiers supported by this method combination. nil
in the set means the method
combination supports primary methods (because primary methods have no qualifier); all other values refer to auxiliary
methods with that qualifier, e.g. :before
, :after
, or :around
.
(allowed-qualifiers (clojure-method-combination)) ;-> #{nil}
(allowed-qualifiers (clos-method-combination)) ;-> #{nil :before :after :around}
(allowed-qualifiers (doseq-method-combination)) ;-> #{:doseq}
The set containing all qualifiers supported by this method combination. `nil` in the set means the method combination supports primary methods (because primary methods have no qualifier); all other values refer to auxiliary methods with that qualifier, e.g. `:before`, `:after`, or `:around`. ```clj (allowed-qualifiers (clojure-method-combination)) ;-> #{nil} (allowed-qualifiers (clos-method-combination)) ;-> #{nil :before :after :around} (allowed-qualifiers (doseq-method-combination)) ;-> #{:doseq} ```
(and-method-combination)
Invoke all applicable primary methods, from most-specific to least-specific; reducing the results as if by and
.
Like and
, this method invocation short-circuits if any implementation returns a falsey value. Otherwise, this
method returns the value returned by the last method invoked.
Invoke *all* applicable primary methods, from most-specific to least-specific; reducing the results as if by `and`. Like `and`, this method invocation short-circuits if any implementation returns a falsey value. Otherwise, this method returns the value returned by the last method invoked.
(applicable-primary-method multifn dispatch-val)
Return the primary method that would be use for dispatch-value
, including ones from ancestor dispatch values or the
default dipsatch value. Method includes ^:dispatch-value
metadata indicating the actual dispatch value for which
the applicable method was defined.
Like primary-method
, the method returned will not have any implicit args (such as next-method
) bound.
Return the primary method that would be use for `dispatch-value`, including ones from ancestor dispatch values or the default dipsatch value. Method includes `^:dispatch-value` metadata indicating the actual dispatch value for which the applicable method was defined. Like [[primary-method]], the method returned will not have any implicit args (such as `next-method`) bound.
(aux-methods multifn)
(aux-methods multifn dispatch-val)
(aux-methods multifn qualifier dispatch-val)
Get all auxiliary methods explicitly specified for dispatch-value
. This function does not include methods that
would otherwise still be applicable (e.g., methods for ancestor dispatch values) -- the methods explicitly defined
for this exact match.
qualifier
-> dispatch value
-> [method]
.qualifier
-> [method]
.methods
.Get all auxiliary methods *explicitly specified* for `dispatch-value`. This function does not include methods that would otherwise still be applicable (e.g., methods for ancestor dispatch values) -- the methods explicitly defined for this exact match. * With 1 arg: methods come back as a map of `qualifier` -> `dispatch value` -> `[method]`. * With 2 args: methods come back as a map of `qualifier` -> `[method]`. * With 3 args: methods come back as sequence of `methods`.
(cached-multifn-impl impl)
(cached-multifn-impl impl cache)
Wrap a MultiFnImpl
in a CachedMultiFnImpl
, which adds caching to calculated effective methods. The cache itself
is swappable with other caches that implement different strategies.
Wrap a `MultiFnImpl` in a `CachedMultiFnImpl`, which adds caching to calculated effective methods. The cache itself is swappable with other caches that implement different strategies.
(clojure-method-combination)
Simple method combination strategy that mimics the way vanilla Clojure multimethods combine methods; that is, to say, not at all. Like vanilla Clojure multimethods, this method combination only supports primary methods.
Simple method combination strategy that mimics the way vanilla Clojure multimethods combine methods; that is, to say, not at all. Like vanilla Clojure multimethods, this method combination only supports primary methods.
(clojure-method-table)
(clojure-method-table m)
Create a new Clojure-style method table. Clojure-style method tables only support primary methods.
Create a new Clojure-style method table. Clojure-style method tables only support primary methods.
(clojure-multifn-impl dispatch-fn
&
{:keys [hierarchy default-value prefers method-table]})
Create a mulitfn impl that largely behaves the same way as a vanilla Clojure multimethod.
Create a mulitfn impl that largely behaves the same way as a vanilla Clojure multimethod.
(clos-method-combination)
Method combination strategy that mimics the standard method combination in the Common Lisp Object System (CLOS).
Supports :before
, :after
, and :around
auxiliary methods. The values returned by :before
and :after
methods
are ignored. Primary methods and around methods get an implicit next-method
arg (see Methodical dox for more on
what this means).
Method combination strategy that mimics the standard method combination in the Common Lisp Object System (CLOS). Supports `:before`, `:after`, and `:around` auxiliary methods. The values returned by `:before` and `:after` methods are ignored. Primary methods and around methods get an implicit `next-method` arg (see Methodical dox for more on what this means).
(clos-multifn-impl dispatch-fn
&
{:keys [hierarchy default-value prefers primary-method-table
aux-method-table]})
Convenience for creating a new multifn instances that for the most part mimics the behavior of CLOS generic functions
using the standard method combination. Supports :before
, :after
, and :around
auxiliary methods, but values of
:before
and :after
methods are ignored, rather than threaded. Primary and :around
methods each get an implicit
next-method
arg.
Convenience for creating a new multifn instances that for the most part mimics the behavior of CLOS generic functions using the standard method combination. Supports `:before`, `:after`, and `:around` auxiliary methods, but values of `:before` and `:after` methods are ignored, rather than threaded. Primary and `:around` methods each get an implicit `next-method` arg.
(combine-methods method-combination primary-methods aux-methods)
Combine a sequence of matching primary-methods
with aux-methods
(a map of qualifier -> sequence of methods)
into a single effective method. Method includes effective ^:dispatch-value
metadata.
Combine a sequence of matching `primary-methods` with `aux-methods` (a map of qualifier -> sequence of methods) into a single effective method. Method includes effective `^:dispatch-value` metadata.
(concat-method-combination)
Like the seq-method-combination
, but concatenates all the results together.
seq-method-combination : map :: concat-method-combination : mapcat
Like the `seq-method-combination`, but concatenates all the results together. seq-method-combination : map :: concat-method-combination : mapcat
(default-aux-methods multifn)
Get a map of aux qualifier -> methods for the default dispatch value, if any exist.
Get a map of aux qualifier -> methods for the default dispatch value, if any exist.
(default-dispatch-value dispatcher)
Default dispatch value to use if no other dispatch value matches.
Default dispatch value to use if no other dispatch value matches.
(default-effective-method multifn)
Return the effective (combined) method for the default dispatch value, if one can be computed.
Return the effective (combined) method for the default dispatch value, if one can be computed.
(default-multifn dispatch-fn & {:keys [hierarchy default-value prefers]})
Create a new Methodical multifn using the default impl.
Create a new Methodical multifn using the default impl.
(default-multifn-impl dispatch-fn & {:keys [hierarchy default-value prefers]})
Create a basic multifn impl using default choices for method combination, dispatcher, and method table.
Create a basic multifn impl using default choices for method combination, dispatcher, and method table.
(default-primary-method multifn)
Get the default primary method associated with this mutlifn
, if one exists.
Get the default primary method associated with this `mutlifn`, if one exists.
(defmethod multifn-symb dispatch-val docstring? & fn-tail)
(defmethod multifn-symb
aux-qualifier
dispatch-val
unique-key?
docstring?
&
fn-tail)
Define a new multimethod method implementation. Syntax is the same as for vanilla Clojure defmethod
, but you may
also define auxiliary methods by passing an optional auxiliary method qualifier before the dispatch value:
;; define a new primary method
(defmethod some-multifn Bird
[_]
...)
;; define a new *auxiliary* method
(defmethod some-multifn :before Toucan
[_]
...)
Define a new multimethod method implementation. Syntax is the same as for vanilla Clojure [[defmethod]], but you may also define auxiliary methods by passing an optional auxiliary method qualifier before the dispatch value: ```clj ;; define a new primary method (defmethod some-multifn Bird [_] ...) ;; define a new *auxiliary* method (defmethod some-multifn :before Toucan [_] ...) ```
(defmulti name-symb
docstring?
attr-map?
&
{:keys [dispatcher combo method-table cache]})
(defmulti name-symb
docstring?
attr-map?
dispatch-fn?
&
{:keys [hierarchy default-value prefers combo method-table cache]})
Creates a new Methodical multimethod named by a Var. Usage of this macro mimics usage of [[clojure.core/defmulti]],
and it can be used as a drop-in replacement; it does, however, support a larger set of options. Note the dispatch-fn
is optional (if omitted, then identity will be used). In addition to the usual :default
and :hierarchy
options,
you many specify:
:combo
- The method combination to use for this multimethods. Method combinations define how multiple applicable
methods are combined; which auxiliary methods, e.g. :before
or :after
methods, are supported; and whether other
advanced facilities, such as next-method
, are available. There are over a dozen method combinations that ship as
part of Methodical; many are inspired by their equivalents in the Common Lisp Object System. The default method
combination is the thread-last method combination.
:dispatcher
- The dispatcher handles dispatch values when invoking a multimethod, and whether one dispatch value
(and thus, whether its corresponding method) is considered to be more-specific or otherwise preferred over another
dispatch value. The default dispatcher largely mimics the behavior of the Clojure dispatcher, using a single
hierarchy augmented by a prefers
table to control dispatch, with one big improvement: when dispatching on
multiple values, it supports default methods that specialize on some args and use the default for others.
(e.g. [String :default]
)
Note that the :hierarchy
, :default-value
and the positional dispatch-fn
are provided as conveniences for
creating a default dispatcher; if you pass a :dispatcher
arg instead, those arguments are not required and will
be ignored.
:cache
- controls caching behavior for effective methods. The default simple cache mimics the behavior of vanilla
Clojure multimethods.
:method-table
- maintains tables of dispatch value -> primary method and auxiliary method qualifier -> dispatch
value -> methods. The default implementation is a pair of simple maps.
The above options comprise the main constituent parts of a Methodical multimethod, and the majority of those parts
have several alternative implementations available in methodical.impl
. Defining additional implementations is
straightforward as well: see methodical.interface
for more details.
Other improvements over [[clojure.core/defmulti]]:
Attribute map options:
defmulti
supports a few additional options in its attributes map that will be used to validate defmethod
forms
during macroexpansion time. These are meant to help the users of your multimethods use them correctly by catching
mistakes right away rather than waiting for them to pull their hair out later wondering why a method they added isn't
getting called.
:dispatch-value-spec
-- a spec for the defmethod
dispatch value:
(m/defmulti mf
{:arglists '([x y]), :dispatch-value-spec (s/cat :x keyword?, :y int?)}
(fn [x y] [x y]))
(m/defmethod mf [:x 1]
[x y]
{:x x, :y y})
;; => ok
(m/defmethod mf [:x]
[x y]
{:x x, :y y})
;; failed: Insufficient input in: [0] at: [:args-for-method-type :primary :dispatch-value :y] [:x]
Note that this spec is applied to the unevaluated arguments at macroexpansion time, not the actual evaluated values.
Note also that if you want to allow a :default
method your spec will have to support it.
:defmethod-arities
-- a set of allowed/required arities that defmethod
forms are allowed to have. defmethod
forms must have arities that match all of the specified :defmethod-arities
, and all of its arities must be
allowed by :defmethod-arities
:
(m/defmulti ^:private mf
{:arglists '([x]), :defmethod-arities #{1}}
keyword)
(m/defmethod mf :x [x] x)
;; => ok
(m/defmethod mf :x ([x] x) ([x y] x y))
;; => error: {:arities {:disallowed #{2}}}
(m/defmethod mf :x [x y] x y)
;; => error: {:arities {:required #{1}}}
(m/defmethod mf :x [x y] x)
;; => error: {:arities {:required #{1 [:>= 3]}, :disallowed #{2}}}
:defmethod-arities
must be a set of either integers or [:> n]
forms to represent arities with &
rest
arguments, e.g. [:>= 3]
to mean an arity of three or-more arguments:
;; methods must both a 1-arity and a 3+-arity
(m/defmulti ^:private mf
{:arglists '([x] [x y z & more]), :defmethod-arities #{1 [:>= 3]}}
keyword)
(m/defmethod mf :x ([x] x) ([x y z & more] x))
;; => ok
When rest-argument arities are used, Methodical is smart enough to allow them when appropriate even if they do not
specifically match an arity specified in :defmethod-arities
:
(m/defmulti ^:private mf
{:arglists '([x y z & more]), :defmethod-arities #{[:>= 3]}}
keyword)
(m/defmethod mf :x
([a b c] x)
([a b c d] x)
([a b c d & more] x))
;; => ok, because everything required by [:>= 3] is covered, and everything present is allowed by [:>= 3]
Creates a new Methodical multimethod named by a Var. Usage of this macro mimics usage of [[clojure.core/defmulti]], and it can be used as a drop-in replacement; it does, however, support a larger set of options. Note the dispatch-fn is optional (if omitted, then identity will be used). In addition to the usual `:default` and `:hierarchy` options, you many specify: * `:combo` - The method combination to use for this multimethods. Method combinations define how multiple applicable methods are combined; which auxiliary methods, e.g. `:before` or `:after` methods, are supported; and whether other advanced facilities, such as `next-method`, are available. There are over a dozen method combinations that ship as part of Methodical; many are inspired by their equivalents in the Common Lisp Object System. The default method combination is the thread-last method combination. * `:dispatcher` - The dispatcher handles dispatch values when invoking a multimethod, and whether one dispatch value (and thus, whether its corresponding method) is considered to be more-specific or otherwise preferred over another dispatch value. The default dispatcher largely mimics the behavior of the Clojure dispatcher, using a single hierarchy augmented by a `prefers` table to control dispatch, with one big improvement: when dispatching on multiple values, it supports default methods that specialize on some args and use the default for others. (e.g. `[String :default]`) Note that the `:hierarchy`, `:default-value` and the positional `dispatch-fn` are provided as conveniences for creating a default dispatcher; if you pass a `:dispatcher` arg instead, those arguments are not required and will be ignored. * `:cache` - controls caching behavior for effective methods. The default simple cache mimics the behavior of vanilla Clojure multimethods. * `:method-table` - maintains tables of dispatch value -> primary method and auxiliary method qualifier -> dispatch value -> methods. The default implementation is a pair of simple maps. The above options comprise the main constituent parts of a Methodical multimethod, and the majority of those parts have several alternative implementations available in [[methodical.impl]]. Defining additional implementations is straightforward as well: see [[methodical.interface]] for more details. Other improvements over [[clojure.core/defmulti]]: * Evaluating the form a second time (e.g., when reloading a namespace) will *not* redefine the multimethod, unless you have modified its form -- unlike vanilla Clojure multimethods, which need to be unmapped from the namespace to make such minor tweaks as changing the dispatch function. Attribute map options: `defmulti` supports a few additional options in its attributes map that will be used to validate `defmethod` forms during macroexpansion time. These are meant to help the users of your multimethods use them correctly by catching mistakes right away rather than waiting for them to pull their hair out later wondering why a method they added isn't getting called. * `:dispatch-value-spec` -- a spec for the `defmethod` dispatch value: ```clj (m/defmulti mf {:arglists '([x y]), :dispatch-value-spec (s/cat :x keyword?, :y int?)} (fn [x y] [x y])) (m/defmethod mf [:x 1] [x y] {:x x, :y y}) ;; => ok (m/defmethod mf [:x] [x y] {:x x, :y y}) ;; failed: Insufficient input in: [0] at: [:args-for-method-type :primary :dispatch-value :y] [:x] ``` Note that this spec is applied to the unevaluated arguments at macroexpansion time, not the actual evaluated values. Note also that if you want to allow a `:default` method your spec will have to support it. * `:defmethod-arities` -- a set of allowed/required arities that `defmethod` forms are allowed to have. `defmethod` forms must have arities that match *all* of the specified `:defmethod-arities`, and all of its arities must be allowed by `:defmethod-arities`: ```clj (m/defmulti ^:private mf {:arglists '([x]), :defmethod-arities #{1}} keyword) (m/defmethod mf :x [x] x) ;; => ok (m/defmethod mf :x ([x] x) ([x y] x y)) ;; => error: {:arities {:disallowed #{2}}} (m/defmethod mf :x [x y] x y) ;; => error: {:arities {:required #{1}}} (m/defmethod mf :x [x y] x) ;; => error: {:arities {:required #{1 [:>= 3]}, :disallowed #{2}}} ``` `:defmethod-arities` must be a set of either integers or `[:> n]` forms to represent arities with `&` rest arguments, e.g. `[:>= 3]` to mean an arity of three *or-more* arguments: ```clj ;; methods must both a 1-arity and a 3+-arity (m/defmulti ^:private mf {:arglists '([x] [x y z & more]), :defmethod-arities #{1 [:>= 3]}} keyword) (m/defmethod mf :x ([x] x) ([x y z & more] x)) ;; => ok ``` When rest-argument arities are used, Methodical is smart enough to allow them when appropriate even if they do not specifically match an arity specified in `:defmethod-arities`: ```clj (m/defmulti ^:private mf {:arglists '([x y z & more]), :defmethod-arities #{[:>= 3]}} keyword) (m/defmethod mf :x ([a b c] x) ([a b c d] x) ([a b c d & more] x)) ;; => ok, because everything required by [:>= 3] is covered, and everything present is allowed by [:>= 3] ```
(describe this)
Return a Markdown-formatted string description of a Methodical object, such as a multifn.
Return a Markdown-formatted string description of a Methodical object, such as a multifn.
(dispatch-fn multifn)
Return a function that can be used to calculate dispatch values of given arg(s).
Return a function that can be used to calculate dispatch values of given arg(s).
(dispatch-on-first-arg dispatch-fn)
Create a dispatch function this will dispatch on the value of
(dispatch-fn <first-arg>)
and ignore all other args.
Create a dispatch function this will dispatch on the value of ```clj (dispatch-fn <first-arg>) ``` and ignore all other args.
(dispatch-on-first-four-args dispatch-fn)
(dispatch-on-first-four-args dispatch-fn-a
dispatch-fn-b
dispatch-fn-c
dispatch-fn-d)
Create a dispatch function this will dispatch on the value of
[(dispatch-fn <first-arg>) (dispatch-fn <second-arg>) (dispatch-fn <third-arg>) (dispatch-fn <fourth-arg>)]
and ignore all other args.
Create a dispatch function this will dispatch on the value of ```clj [(dispatch-fn <first-arg>) (dispatch-fn <second-arg>) (dispatch-fn <third-arg>) (dispatch-fn <fourth-arg>)] ``` and ignore all other args.
(dispatch-on-first-three-args dispatch-fn)
(dispatch-on-first-three-args dispatch-fn-a dispatch-fn-b dispatch-fn-c)
Create a dispatch function this will dispatch on the value of
[(dispatch-fn <first-arg>) (dispatch-fn <second-arg>) (dispatch-fn <third-arg>)]
and ignore all other args.
Create a dispatch function this will dispatch on the value of ```clj [(dispatch-fn <first-arg>) (dispatch-fn <second-arg>) (dispatch-fn <third-arg>)] ``` and ignore all other args.
(dispatch-on-first-two-args dispatch-fn)
(dispatch-on-first-two-args dispatch-fn-a dispatch-fn-b)
Create a dispatch function this will dispatch on the value of
[(dispatch-fn <first-arg>) (dispatch-fn <second-arg>)]
and ignore all other args.
Create a dispatch function this will dispatch on the value of ```clj [(dispatch-fn <first-arg>) (dispatch-fn <second-arg>)] ``` and ignore all other args.
(dispatch-value multifn a)
(dispatch-value multifn a b)
(dispatch-value multifn a b c)
(dispatch-value multifn a b c d)
(dispatch-value multifn a b c d e)
(dispatch-value multifn a b c d e f)
(dispatch-value multifn a b c d e f g)
(dispatch-value multifn a b c d e f g & more)
Calculate the dispatch value that multifn
will use given args
.
Calculate the dispatch value that `multifn` will use given `args`.
(dispatcher multifn)
Get the dispatcher associated with this multifn.
Get the dispatcher associated with this multifn.
(do-method-combination)
Based on the CLOS progn
method combination. Sequentially executes all applicable primary methods, presumably for
side-effects, in order from most-specific to least-specific; returns the value returned by the least-specific
method. do
method combinations support :around
auxiliary methods, but not :before
or :after
methods.
Based on the CLOS `progn` method combination. Sequentially executes *all* applicable primary methods, presumably for side-effects, in order from most-specific to least-specific; returns the value returned by the least-specific method. `do` method combinations support `:around` auxiliary methods, but not `:before` or `:after` methods.
(dominates? dispatcher dispatch-val-x dispatch-val-y)
Is dispatch-val-x
considered more specific than dispatch-val-y
?
Is `dispatch-val-x` considered more specific than `dispatch-val-y`?
(effective-dispatch-value multifn dispatch-val)
Return the least-specific dispatch value that would return the same effective method as dispatch-value
. e.g. if
dispatch-value
is Integer
and the effective method is a result of combining a Object
primary method and a
Number
aux method, the effective dispatch value is Number
, since Number
is the most specific thing out of the
primary and aux methods and would get the same effective method as Integer
.
Return the least-specific dispatch value that would return the same effective method as `dispatch-value`. e.g. if `dispatch-value` is `Integer` and the effective method is a result of combining a `Object` primary method and a `Number` aux method, the effective dispatch value is `Number`, since `Number` is the most specific thing out of the primary and aux methods and would get the same effective method as `Integer`.
(effective-method multifn dispatch-value)
Return the effective method for dispatch-value
. The effective method is a combined primary method and applicable
auxiliary methods that can be called like a normal function. effective-method
is similar in purpose
to [[clojure.core/get-method]] in vanilla Clojure multimethods; a different name is used here because I felt
get-method
would be ambiguous with regards to whether it returns only a primary method or a combined effective
method.
Return the effective method for `dispatch-value`. The effective method is a combined primary method and applicable auxiliary methods that can be called like a normal function. [[effective-method]] is similar in purpose to [[clojure.core/get-method]] in vanilla Clojure multimethods; a different name is used here because I felt `get-method` would be ambiguous with regards to whether it returns only a primary method or a combined effective method.
(effective-primary-method multifn dispatch-val)
Build and effective method equivalent that would be used for this dispatch-value
if it had no applicable auxiliary
methods. Implicit args (such as next-method
) will be bound appropriately. Method has ^:dispatch-value
metadata
for the dispatch value with which the most-specific primary method was defined.
Build and effective method equivalent that would be used for this `dispatch-value` if it had no applicable auxiliary methods. Implicit args (such as `next-method`) will be bound appropriately. Method has `^:dispatch-value` metadata for the dispatch value with which the most-specific primary method was defined.
(everything-dispatcher &
{:keys [hierarchy prefers]
:or {hierarchy (var clojure.core/global-hierarchy)
prefers {}}})
A Dispatcher that always considers all primary and auxiliary methods to be matches; does not calculate dispatch
values for arguments when invoking. Dispatch values are still used to sort methods from most- to least- specific,
using hierarchy
and map of prefers
.
A Dispatcher that always considers *all* primary and auxiliary methods to be matches; does not calculate dispatch values for arguments when invoking. Dispatch values are still used to sort methods from most- to least- specific, using `hierarchy` and map of `prefers`.
(is-default-effective-method? multifn dispatch-val)
When multifn
is invoked with args that have dispatch-val
, will we end up using the default effective
method (assuming one exists)?
When `multifn` is invoked with args that have `dispatch-val`, will we end up using the default effective method (assuming one exists)?
(is-default-primary-method? multifn dispatch-val)
When multifn
is invoked with args that have dispatch-val
, will we end up using the default primary method (assuming
one exists)?
When `multifn` is invoked with args that have `dispatch-val`, will we end up using the default primary method (assuming one exists)?
(matching-aux-methods multifn dispatch-val)
(matching-aux-methods dispatcher method-table dispatch-val)
Return a map of aux method qualifier -> sequence of applicable methods for dispatch-value
, sorted from
most-specific to least-specific. Methods should have the ^:dispatch-value
with which they were defined as
metadata.
Return a map of aux method qualifier -> sequence of applicable methods for `dispatch-value`, sorted from most-specific to least-specific. Methods should have the `^:dispatch-value` with which they were defined as metadata.
(matching-primary-methods multifn dispatch-val)
(matching-primary-methods dispatcher method-table dispatch-val)
Return a sequence of applicable primary methods for dispatch-value
, sorted from most-specific to least-specific.
Methods include the ^:dispatch-value
with which they were defined as metadata. The standard dispatcher also checks
to make sure methods in the sequence are not ambiguously specific, replacing ambiguous methods with ones that will
throw an Exception when invoked.
Return a sequence of applicable primary methods for `dispatch-value`, sorted from most-specific to least-specific. Methods include the `^:dispatch-value` with which they were defined as metadata. The standard dispatcher also checks to make sure methods in the sequence are not ambiguously specific, replacing ambiguous methods with ones that will throw an Exception when invoked.
(max-method-combination)
Executes all applicable primary methods, and returns the maximum value returned by any one implementation. Same constraints as other CLOS operator-style method combinations.
Executes *all* applicable primary methods, and returns the maximum value returned by any one implementation. Same constraints as other CLOS operator-style method combinations.
(method-combination multifn)
Get the method combination associated with this multifn.
Get the method combination associated with this multifn.
(method-table multifn)
Get the method table associated with this multifn.
Get the method table associated with this multifn.
(min-method-combination)
Based on the CLOS method combination of the same name. Executes all applicable primary methods, returning the
minimum value returned by any implementation. Like do
method combinations, min
supports :around
auxiliary
methods, but not :before
or :after
.
Based on the CLOS method combination of the same name. Executes *all* applicable primary methods, returning the minimum value returned by any implementation. Like `do` method combinations, `min` supports `:around` auxiliary methods, but not `:before` or `:after`.
(multi-default-dispatcher dispatch-fn
&
{:keys [hierarchy default-value prefers]
:or {hierarchy (var clojure.core/global-hierarchy)
default-value :default
prefers {}}})
Like the standard dispatcher, with one big improvement: when dispatching on multiple values, it supports default
methods that specialize on some args and use the default for others. (e.g. [String :default]
)
Like the standard dispatcher, with one big improvement: when dispatching on multiple values, it supports default methods that specialize on some args and use the default for others. (e.g. `[String :default]`)
(multifn impl)
(multifn impl mta)
(multifn impl mta cache)
Create a new cached Methodical multifn using impl
as the multifn implementation.
Create a new *cached* Methodical multifn using `impl` as the multifn implementation.
(or-method-combination)
Like the and
combination, but combines result as if by or
; short-circuits after the first matching primary method
returns a truthy value.
Like the `and` combination, but combines result as if by `or`; short-circuits after the first matching primary method returns a truthy value.
(prefer-method multifn dispatch-val-x dispatch-val-y)
Prefer dispatch-val-x
over dispatch-val-y
for dispatch and method combinations. You can undo this preference
with unprefer-method
.
Prefer `dispatch-val-x` over `dispatch-val-y` for dispatch and method combinations. You can undo this preference with [[unprefer-method]].
(prefer-method! multifn-var dispatch-val-x dispatch-val-y)
Destructive version of prefer-method
. Operates on a var defining a Methodical multifn.
Note that vanilla Clojure [[clojure.core/prefer-method]] is actually itself destructive, so this function is
actually the Methodical equivalent of that function. prefer-method!
is used by Methodical to differentiate the
operation from our nondestructive prefer-method
, which returns a copy of the multifn with an altered dispatch
table.
Destructive version of [[prefer-method]]. Operates on a var defining a Methodical multifn. Note that vanilla Clojure [[clojure.core/prefer-method]] is actually itself destructive, so this function is actually the Methodical equivalent of that function. `prefer-method!` is used by Methodical to differentiate the operation from our nondestructive [[prefer-method]], which returns a copy of the multifn with an altered dispatch table.
(prefers dispatcher)
Return a map of preferred dispatch value -> set of other dispatch values.
Return a map of preferred dispatch value -> set of other dispatch values.
(primary-method multifn dispatch-val)
Get the primary method explicitly specified for dispatch-value
. This function does not return methods that would
otherwise still be applicable (e.g., methods for ancestor dispatch values) -- just the methods explicitly defined
for this exact match. (If you want methods that will be used, including those of ancestors dispatch values, you can
use applicable-primary-method
or effective-primary-method
instead.)
Note that the primary method will not have any implicit args (e.g. next-method
) bound the way it normally would
when combined into an effective method; you will need to supply this yourself (or pass nil
for no next-method
).
Get the primary method *explicitly specified* for `dispatch-value`. This function does not return methods that would otherwise still be applicable (e.g., methods for ancestor dispatch values) -- just the methods explicitly defined for this exact match. (If you want methods that will be used, including those of ancestors dispatch values, you can use [[applicable-primary-method]] or [[effective-primary-method]] instead.) Note that the primary method will not have any implicit args (e.g. `next-method`) bound the way it normally would when combined into an effective method; you will need to supply this yourself (or pass `nil` for no `next-method`).
(primary-methods method-table)
Get a dispatch-value -> fn
map of all primary methods associated with this method table.
Get a `dispatch-value -> fn` map of all primary methods associated with this method table.
(remove-all-aux-methods multifn)
(remove-all-aux-methods multifn qualifier)
(remove-all-aux-methods multifn qualifier dispatch-val)
With one arg, remove all auxiliary methods for a multifn
. With two args, remove all auxiliary methods for the
given qualifier
(e.g. :before
). With three args, remove all auxiliary methods for a given qualifier
and
dispatch-value
.
With one arg, remove *all* auxiliary methods for a `multifn`. With two args, remove all auxiliary methods for the given `qualifier` (e.g. `:before`). With three args, remove all auxiliary methods for a given `qualifier` and `dispatch-value`.
(remove-all-aux-methods! multifn-var)
(remove-all-aux-methods! multifn-var qualifier)
(remove-all-aux-methods! multifn-var qualifier dispatch-val)
Destructive version of remove-all-aux-methods
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-all-aux-methods]]. Operates on a var defining a Methodical multifn.
(remove-all-aux-methods-for-dispatch-val multifn dispatch-val)
Remove all auxiliary methods for dispatch-value
for all qualifiers.
Remove all auxiliary methods for `dispatch-value` for *all* qualifiers.
(remove-all-aux-methods-for-dispatch-val! multifn-var dispatch-val)
Destructive version of remove-all-aux-methods-for-dispatch-val
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-all-aux-methods-for-dispatch-val]]. Operates on a var defining a Methodical multifn.
(remove-all-methods multifn)
Remove all primary and auxiliary methods, including default implementations.
Remove all primary and auxiliary methods, including default implementations.
(remove-all-methods! multifn-var)
Destructive version of remove-all-methods
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-all-methods]]. Operates on a var defining a Methodical multifn.
(remove-all-preferences multifn)
Return a copy of multifn
with all of its preferences for all dispatch values removed.
To destructively remove all preferences, use remove-all-preferences!
.
Return a copy of `multifn` with all of its preferences for all dispatch values removed. To destructively remove all preferences, use [[remove-all-preferences!]].
(remove-all-preferences! multifn-var)
Destructive version of remove-all-preferences
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-all-preferences]]. Operates on a var defining a Methodical multifn.
(remove-all-primary-methods multifn)
Remove all primary methods, for all dispatch values (including the default value), for this multifn
or method
table.
Remove all primary methods, for all dispatch values (including the default value), for this `multifn` or method table.
(remove-all-primary-methods! multifn-var)
Destructive version of remove-all-primary-methods
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-all-primary-methods]]. Operates on a var defining a Methodical multifn.
(remove-aux-method method-table qualifier dispatch-val method)
Remove an auxiliary method from a method table. Because multiple auxiliary methods are allowed for the same
dispatch value, existing implementations of MethodTable
are currently only able to remove exact matches -- for
functions, this usually means identical objects.
In the future, I hope to fix this by storing unique identifiers in the metadata of methods in the map.
Remove an auxiliary method from a method table. Because multiple auxiliary methods are allowed for the same dispatch value, existing implementations of `MethodTable` are currently only able to remove exact matches -- for functions, this usually means identical objects. In the future, I hope to fix this by storing unique identifiers in the metadata of methods in the map.
(remove-aux-method! multifn-var qualifier dispatch-val f)
Destructive version of [[methodical.interface/remove-aux-method]]. Operates on a var defining a Methodical multifn.
Destructive version of [[methodical.interface/remove-aux-method]]. Operates on a var defining a Methodical multifn.
(remove-aux-method-with-unique-key multifn qualifier dispatch-val unique-key)
Remove an auxiliary method that was added by add-aux-method-with-unique-key
, if one exists. Returns multifn
.
Remove an auxiliary method that was added by [[add-aux-method-with-unique-key]], if one exists. Returns `multifn`.
(remove-aux-method-with-unique-key! multifn-var
qualifier
dispatch-val
unique-key)
Destructive version of remove-aux-method-with-unique-key
. Operates on a var defining a Methodical multifn.
Destructive version of [[remove-aux-method-with-unique-key]]. Operates on a var defining a Methodical multifn.
(remove-primary-method method-table dispatch-value)
Remove the primary method for dispatch-value
.
Remove the primary method for `dispatch-value`.
(remove-primary-method! multifn-var dispatch-val)
Destructive version of [[methodical.interface/remove-primary-method]]. Operates on a var defining a Methodical multifn.
Destructive version of [[methodical.interface/remove-primary-method]]. Operates on a var defining a Methodical multifn.
(seq-method-combination)
Executes all applicable primary methods, from most-specific to least-specific; returns a sequence of results from
the method invocations. Inspired by CLOS nconc
and append
method combinations, but unlike those, this
combination returns a completely lazy sequence. Like other CLOS-operator-inspired method combinations, this
combination currently supports :around
methods, but not :before
or :after
methods.
Executes *all* applicable primary methods, from most-specific to least-specific; returns a sequence of results from the method invocations. Inspired by CLOS `nconc` and `append` method combinations, but unlike those, this combination returns a completely lazy sequence. Like other CLOS-operator-inspired method combinations, this combination currently supports `:around` methods, but not `:before` or `:after` methods.
(simple-cache)
(simple-cache m)
Create a basic dumb cache. The simple cache stores
Create a basic dumb cache. The simple cache stores
(standard-dispatcher dispatch-fn
&
{:keys [hierarchy default-value prefers]
:or {hierarchy (var clojure.core/global-hierarchy)
default-value :default
prefers {}}})
Create a stanadrd Methodical multifn dispatcher. The standard dispatcher replicates the way vanilla Clojure
multimethods handle multimethod dispatch, with support for a custom hierarchy
, default-value
and map of
prefers
.
Create a stanadrd Methodical multifn dispatcher. The standard dispatcher replicates the way vanilla Clojure multimethods handle multimethod dispatch, with support for a custom `hierarchy`, `default-value` and map of `prefers`.
(standard-method-table)
(standard-method-table primary aux)
Create a new standard method table that supports both primary and auxiliary methods.
Create a new standard method table that supports both primary and auxiliary methods.
(standard-multifn-impl combo dispatcher method-table)
Create a basic multifn impl using method combination combo
, dispatcher dispatcher
, and method-table
.
See default-multifn-impl
for the defaults that are normally used if you don't specify otherwise.
Create a basic multifn impl using method combination `combo`, dispatcher `dispatcher`, and `method-table`. See [[default-multifn-impl]] for the defaults that are normally used if you don't specify otherwise.
(thread-first-method-combination)
Similar the the standard CLOS-style method combination, but threads the result of each :before
and :after
auxiliary methods, as well as the primary method, as the first arg of subsequent method invocations.
Similar the the standard CLOS-style method combination, but threads the result of each `:before` and `:after` auxiliary methods, as well as the primary method, as the *first* arg of subsequent method invocations.
(thread-last-method-combination)
Similar the the standard CLOS-style method combination, but threads the result of each :before
and :after
auxiliary methods, as well as the primary method, as the last arg of subsequent method invocations.
Similar the the standard CLOS-style method combination, but threads the result of each `:before` and `:after` auxiliary methods, as well as the primary method, as the *last* arg of subsequent method invocations.
(trace multifn & args)
Instrument a multimethod multifn
, then invoke it; calls to its primary and aux methods and their results are
printed to out. Returns same result as untraced version would have returned. Prints trace in color by default, but you can disable this by binding [[*color*]] to
false`.
Method calls are printed with n:
, where n
is the current depth of the trace; the result of each method call is
printed with a corresponding n>
:
(trace/trace my-fn 1 {})
;; ->
0: (my-fn 1 {})
1: (#primary-method<:default> nil 1 {})
1> {:x 1}
1: (#aux-method<:after [java.lang.Object :default]> 1 {:x 1})
1> {:object? true, :x 1}
0> {:object? true, :x 1}
Instrument a multimethod `multifn`, then invoke it; calls to its primary and aux methods and their results are printed to *out*`. Returns same result as untraced version would have returned. Prints trace in color by default, but you can disable this by binding [[*color*]] to `false`. Method calls are printed with `n:`, where `n` is the current depth of the trace; the result of each method call is printed with a corresponding `n>`: ```clj (trace/trace my-fn 1 {}) ;; -> 0: (my-fn 1 {}) 1: (#primary-method<:default> nil 1 {}) 1> {:x 1} 1: (#aux-method<:after [java.lang.Object :default]> 1 {:x 1}) 1> {:object? true, :x 1} 0> {:object? true, :x 1} ```
(transform-fn-tail method-combination qualifier fn-tail)
Make appropriate transformations to the fn-tail
of a methodical.macros/defmethod
macro expansion for a
primary method (qualifier will be nil
) or an auxiliary method. You can use this method to add implicit args like
next-method
to the body of a defmethod
macro. (Because this method is invoked during macroexpansion, it should
return a Clojure form.)
Make appropriate transformations to the `fn-tail` of a [[methodical.macros/defmethod]] macro expansion for a primary method (qualifier will be `nil`) or an auxiliary method. You can use this method to add implicit args like `next-method` to the body of a `defmethod` macro. (Because this method is invoked during macroexpansion, it should return a Clojure form.)
(uncached-multifn impl)
(uncached-multifn impl mta)
Create a new Methodical multifn using impl
as the multifn implementation; impl
itself should implement
MultiFnImpl
. DOES NOT CACHE EFFECTIVE METHODS -- use multifn
instead, unless you like slow dispatch times.
Create a new Methodical multifn using `impl` as the multifn implementation; `impl` itself should implement `MultiFnImpl`. DOES NOT CACHE EFFECTIVE METHODS -- use `multifn` instead, unless you like slow dispatch times.
(unprefer-method multifn dispatch-val-x dispatch-val-y)
Return a copy of multifn
with any preferences of dispatch-val-x
over dispatch-val-y
removed. If no such
preference exists, this returns multifn
as-is. Opposite of prefer-method
.
To destructively remove a dispatch value preference, use unprefer-method!
.
Return a copy of `multifn` with any preferences of `dispatch-val-x` over `dispatch-val-y` removed. If no such preference exists, this returns `multifn` as-is. Opposite of [[prefer-method]]. To destructively remove a dispatch value preference, use [[unprefer-method!]].
(unprefer-method! multifn-var dispatch-val-x dispatch-val-y)
Destructive version of unprefer-method
. Operates on a var defining a Methodical multifn.
Destructive version of [[unprefer-method]]. Operates on a var defining a Methodical multifn.
(watching-cache cache references)
Wrap cache
in a WatchingCache
, which clears the cache whenever one of the watched references
(such as vars or
atoms) changes. Intended primarily for use with 'permanent' MultiFns, such as those created with defmulti
; this is
rarely needed or wanted for transient multifns.
Wrap `cache` in a `WatchingCache`, which clears the cache whenever one of the watched `references` (such as vars or atoms) changes. Intended primarily for use with 'permanent' MultiFns, such as those created with `defmulti`; this is rarely needed or wanted for transient multifns.
(with-dispatcher multifn new-dispatcher)
Return a copy of this multifn using new-dispatcher
as its dispatcher.
Return a copy of this multifn using `new-dispatcher` as its dispatcher.
(with-method-table multifn new-method-table)
Return a copy of this multifn using new-method-table
as its method table.
Return a copy of this multifn using `new-method-table` as its method table.
(with-prefers dispatcher new-prefs)
Return a copy of dispatcher
with its preferences map replaced with new-prefs
.
Return a copy of `dispatcher` with its preferences map replaced with `new-prefs`.
(with-prefers! multifn-var new-prefs)
Destructive version of [[methodical.interface/with-prefers]]. Operates on a var defining a Methodical multifn.
Destructive version of [[methodical.interface/with-prefers]]. Operates on a var defining a Methodical multifn.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close