Back
methods (clj)
(source)function
(methods multifn)
Given a multimethod, returns a map of dispatch values -> dispatch fns
Examples
babashka/babashka
(ns babashka.impl.protocols
(:require [babashka.impl.protocols :as protocols]
[clojure.core.protocols :as p]
[clojure.datafy :as d]
;; ensure datafy is loaded, we're going to override its
;; clojure.lang.Namespace implementation for datafy
[clojure.reflect]
[sci.core :as sci :refer [copy-var]]
[sci.impl.types :as types]
[sci.impl.vars]))
(defmethod datafy :sci.impl.protocols/reified [x]
(let [methods (types/getMethods x)]
((get methods 'datafy) x)))
(defmethod nav :sci.impl.protocols/reified [coll k v]
(let [methods (types/getMethods coll)]
((get methods 'nav) coll k v)))
(def protocols-namespace
{;; Datafiable
'Datafiable (sci/new-var 'clojure.core.protocols/Datafiable {:methods #{'datafy}
:protocol p/Datafiable
:ns protocols-ns}
{:ns protocols-ns})
'datafy (copy-var datafy protocols-ns)
;; Navigable
'Navigable (sci/new-var 'clojure.core.protocols/Navigable {:methods #{'nav}
:protocol p/Navigable
:ns protocols-ns}
{:ns protocols-ns})
'nav (copy-var nav protocols-ns)
;; IKVReduce only added for satisies? check for now. We can implement
;; kv-reduce in the future, but this needs patching some functions like
;; update-vals, etc.
'IKVReduce (sci/new-var 'clojure.core.protocols/IKVReduce {:protocol p/IKVReduce
;; :methods #{'kv-reduce}
:ns protocols-ns}
{:ns protocols-ns})
;; 'kv-reduce (copy-var kv-reduce protocols-ns)
}
)
hraberg/deuce
(ns deuce.emacs.coding
(:use [deuce.emacs-lisp :only (defun defvar) :as el])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc]
[deuce.emacs.charset :as charset]
[deuce.emacs.fns :as fns]
[deuce.emacs-lisp.globals :as globals])
(:refer-clojure :exclude []))
(defvar translation-table-for-input nil
"Char table for translating self-inserting characters.
This is applied to the result of input methods, not their input.
See also `keyboard-translate-table'.
camsaul/methodical
(ns methodical.impl.cache.simple
"A basic, dumb cache. `SimpleCache` stores cached methods in a simple map of dispatch-value -> effective method; it
offers no facilities to deduplicate identical methods for the same dispatch value. This behaves similarly to the
caching mechanism in vanilla Clojure."
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.interface]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface Cache)))
describe/Describable
(describe [this]
(format "It caches methods using a `%s`." (.getCanonicalName (class this)))))
camsaul/methodical
(ns methodical.impl.combo.clojure
"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."
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.interface]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
MethodCombination
(allowed-qualifiers [_]
#{nil}) ; only primary methods
(combine-methods [_ [primary-method] aux-methods]
(when (seq aux-methods)
(throw (UnsupportedOperationException. "Clojure-style multimethods do not support auxiliary methods.")))
primary-method)
camsaul/methodical
(ns methodical.impl.multifn.cached
(:require
[clojure.core.protocols :as clojure.protocols]
[clojure.datafy :as datafy]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(clojure.lang Named)
(methodical.interface Cache MultiFnImpl)))
(effective-method [_ dispatch-value]
(or
(.cached-method cache dispatch-value)
;; just like vanilla multimethods, we will add a new entry for every unique dispatch value we encounter, so
;; there's an implicit assumption that dispatch values are bounded
;;
;; build the effective method for dispatch value. We may end up throwing this out, but we currently need to build
;; it to determine the effective dispatch value.
(let [method (i/effective-method impl dispatch-value)
effective-dispatch-value (:dispatch-value (meta method))
;; If a method with the same effective dispatch value is already cached, add the existing method to the
;; cache for dispatch value. This way we don't end up with a bunch of duplicate methods impls for various
;; dispatch values that have the same effective dispatch value
cached-effective-dv-method (.cached-method cache effective-dispatch-value)
method (or cached-effective-dv-method method)]
;; Make sure the method was cached for the effective dispatch value as well, that way if some less-specific
;; dispatch value comes along with the same effective dispatch value we can use the existing method
(when-not cached-effective-dv-method
(i/cache-method! cache effective-dispatch-value method))
(i/cache-method! cache dispatch-value method)
method)))