summaryrefslogtreecommitdiff
path: root/core.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-11-02 09:17:03 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-11-02 09:17:03 +0300
commite216b9be95e2c52f62e27294714ba6bb8fd58d1f (patch)
treecf590436b7f0cbfe70e4d15739004c2487bde2ab /core.fnl
parent3b9aa01b8b82f8710eb19fc829b937af42d9dbcb (diff)
feature(core): more multimethod related functions
Diffstat (limited to 'core.fnl')
-rw-r--r--core.fnl25
1 files changed, 25 insertions, 0 deletions
diff --git a/core.fnl b/core.fnl
index 26cedcb..2218706 100644
--- a/core.fnl
+++ b/core.fnl
@@ -423,4 +423,29 @@ found in the table."
(set res not-found)))
res))
+(fn* core.remove-method
+ [multifn dispatch-val]
+ (tset (. (getmetatable multifn) :multimethods) dispatch-val nil)
+ multifn)
+
+(fn* core.remove-all-methods
+ "Removes all of the methods of multimethod"
+ [multifn]
+ (let [mtable (. (getmetatable multifn) :multimethods)]
+ (each [k _ (pairs mtable)]
+ (tset mtable k nil))
+ multifn))
+
+(fn* core.methods
+ "Given a multimethod, returns a map of dispatch values -> dispatch fns"
+ [multifn]
+ (. (getmetatable multifn) :multimethods))
+
+(fn* core.get-method
+ "Given a multimethod and a dispatch value, returns the dispatch `fn'
+that would apply to that value, or `nil' if none apply and no default."
+ [multifn dispatch-val]
+ (or (. (getmetatable multifn) :multimethods dispatch-val)
+ (. (getmetatable multifn) :multimethods :default)))
+
core