Merge pull request #1842 from Kodiologist/rm-multi
Remove hy.contrib.multi
This commit is contained in:
commit
8c67cd0e2f
2
NEWS.rst
2
NEWS.rst
@ -10,6 +10,8 @@ Removals
|
|||||||
and `defn` instead.
|
and `defn` instead.
|
||||||
* Literal keywords are no longer parsed differently in calls to functions
|
* Literal keywords are no longer parsed differently in calls to functions
|
||||||
with certain names.
|
with certain names.
|
||||||
|
* `hy.contrib.multi` has been removed. Use ``cond`` or the PyPI package
|
||||||
|
``multipledispatch`` instead.
|
||||||
|
|
||||||
New Features
|
New Features
|
||||||
------------------------------
|
------------------------------
|
||||||
|
@ -12,7 +12,6 @@ Contents:
|
|||||||
:maxdepth: 3
|
:maxdepth: 3
|
||||||
|
|
||||||
loop
|
loop
|
||||||
multi
|
|
||||||
profile
|
profile
|
||||||
sequences
|
sequences
|
||||||
walk
|
walk
|
||||||
|
@ -1,110 +0,0 @@
|
|||||||
========
|
|
||||||
defmulti
|
|
||||||
========
|
|
||||||
|
|
||||||
defn
|
|
||||||
----
|
|
||||||
.. versionadded:: 0.10.0
|
|
||||||
|
|
||||||
``defn`` lets you arity-overload a function by the given number of
|
|
||||||
args and/or kwargs. This version of ``defn`` works with regular syntax and
|
|
||||||
with the arity overloaded one. Inspired by Clojures take on ``defn``.
|
|
||||||
|
|
||||||
.. code-block:: clj
|
|
||||||
|
|
||||||
=> (require [hy.contrib.multi [defn]])
|
|
||||||
=> (defn fun
|
|
||||||
... ([a] "a")
|
|
||||||
... ([a b] "a b")
|
|
||||||
... ([a b c] "a b c"))
|
|
||||||
|
|
||||||
=> (fun 1)
|
|
||||||
"a"
|
|
||||||
=> (fun 1 2)
|
|
||||||
"a b"
|
|
||||||
=> (fun 1 2 3)
|
|
||||||
"a b c"
|
|
||||||
|
|
||||||
=> (defn add [a b]
|
|
||||||
... (+ a b))
|
|
||||||
=> (add 1 2)
|
|
||||||
3
|
|
||||||
|
|
||||||
|
|
||||||
defmulti
|
|
||||||
--------
|
|
||||||
|
|
||||||
.. versionadded:: 0.12.0
|
|
||||||
|
|
||||||
``defmulti``, ``defmethod`` and ``default-method`` lets you define
|
|
||||||
multimethods where a dispatching function is used to select between different
|
|
||||||
implementations of the function. Inspired by Clojure's multimethod and based
|
|
||||||
on the code by `Adam Bard`_.
|
|
||||||
|
|
||||||
.. code-block:: clj
|
|
||||||
|
|
||||||
=> (require [hy.contrib.multi [defmulti defmethod default-method]])
|
|
||||||
=> (defmulti area [shape]
|
|
||||||
... "calculate area of a shape"
|
|
||||||
... (:type shape))
|
|
||||||
|
|
||||||
=> (defmethod area "square" [square]
|
|
||||||
... (* (:width square)
|
|
||||||
... (:height square)))
|
|
||||||
|
|
||||||
=> (defmethod area "circle" [circle]
|
|
||||||
... (* (** (:radius circle) 2)
|
|
||||||
... 3.14))
|
|
||||||
|
|
||||||
=> (default-method area [shape]
|
|
||||||
... 0)
|
|
||||||
|
|
||||||
=> (area {:type "circle" :radius 0.5})
|
|
||||||
0.785
|
|
||||||
|
|
||||||
=> (area {:type "square" :width 2 :height 2})
|
|
||||||
4
|
|
||||||
|
|
||||||
=> (area {:type "non-euclid rhomboid"})
|
|
||||||
0
|
|
||||||
|
|
||||||
``defmulti`` is used to define the initial multimethod with name, signature
|
|
||||||
and code that selects between different implementations. In the example,
|
|
||||||
multimethod expects a single input that is type of dictionary and contains
|
|
||||||
at least key :type. The value that corresponds to this key is returned and
|
|
||||||
is used to selected between different implementations.
|
|
||||||
|
|
||||||
``defmethod`` defines a possible implementation for multimethod. It works
|
|
||||||
otherwise in the same way as ``defn``, but has an extra parameters
|
|
||||||
for specifying multimethod and which calls are routed to this specific
|
|
||||||
implementation. In the example, shapes with "square" as :type are routed to
|
|
||||||
first function and shapes with "circle" as :type are routed to second
|
|
||||||
function.
|
|
||||||
|
|
||||||
``default-method`` specifies default implementation for multimethod that is
|
|
||||||
called when no other implementation matches.
|
|
||||||
|
|
||||||
Interfaces of multimethod and different implementation don't have to be
|
|
||||||
exactly identical, as long as they're compatible enough. In practice this
|
|
||||||
means that multimethod should accept the broadest range of parameters and
|
|
||||||
different implementations can narrow them down.
|
|
||||||
|
|
||||||
.. code-block:: clj
|
|
||||||
|
|
||||||
=> (require [hy.contrib.multi [defmulti defmethod]])
|
|
||||||
=> (defmulti fun [&rest args]
|
|
||||||
... (len args))
|
|
||||||
|
|
||||||
=> (defmethod fun 1 [a]
|
|
||||||
... a)
|
|
||||||
|
|
||||||
=> (defmethod fun 2 [a b]
|
|
||||||
... (+ a b))
|
|
||||||
|
|
||||||
=> (fun 1)
|
|
||||||
1
|
|
||||||
|
|
||||||
=> (fun 1 2)
|
|
||||||
3
|
|
||||||
|
|
||||||
.. _Adam Bard: https://adambard.com/blog/implementing-multimethods-in-python/
|
|
@ -1,89 +0,0 @@
|
|||||||
;; Hy Arity-overloading
|
|
||||||
;; Copyright 2019 the authors.
|
|
||||||
;; This file is part of Hy, which is free software licensed under the Expat
|
|
||||||
;; license. See the LICENSE.
|
|
||||||
|
|
||||||
(import [collections [defaultdict]]
|
|
||||||
[hy [HyExpression HyList HyString]])
|
|
||||||
|
|
||||||
(defclass MultiDispatch [object]
|
|
||||||
|
|
||||||
(setv _fns (defaultdict dict))
|
|
||||||
|
|
||||||
(defn __init__ [self f]
|
|
||||||
(setv self.f f)
|
|
||||||
(setv self.__doc__ f.__doc__)
|
|
||||||
(unless (in f.__name__ (.keys (get self._fns f.__module__)))
|
|
||||||
(setv (get self._fns f.__module__ f.__name__) {}))
|
|
||||||
(setv values f.__code__.co_varnames)
|
|
||||||
(setv (get self._fns f.__module__ f.__name__ values) f))
|
|
||||||
|
|
||||||
(defn fn? [self v args kwargs]
|
|
||||||
"Compare the given (checked fn) to the called fn"
|
|
||||||
(setv com (+ (list args) (list (.keys kwargs))))
|
|
||||||
(and
|
|
||||||
(= (len com) (len v))
|
|
||||||
(.issubset (frozenset (.keys kwargs)) com)))
|
|
||||||
|
|
||||||
(defn __call__ [self &rest args &kwargs kwargs]
|
|
||||||
(setv func None)
|
|
||||||
(for [[i f] (.items (get self._fns self.f.__module__ self.f.__name__))]
|
|
||||||
(when (.fn? self i args kwargs)
|
|
||||||
(setv func f)
|
|
||||||
(break)))
|
|
||||||
(if func
|
|
||||||
(func #* args #** kwargs)
|
|
||||||
(raise (TypeError "No matching functions with this signature")))))
|
|
||||||
|
|
||||||
(defn multi-decorator [dispatch-fn]
|
|
||||||
(setv inner (fn [&rest args &kwargs kwargs]
|
|
||||||
(setv dispatch-key (dispatch-fn #* args #** kwargs))
|
|
||||||
(if (in dispatch-key inner.--multi--)
|
|
||||||
((get inner.--multi-- dispatch-key) #* args #** kwargs)
|
|
||||||
(inner.--multi-default-- #* args #** kwargs))))
|
|
||||||
(setv inner.--multi-- {})
|
|
||||||
(setv inner.--doc-- dispatch-fn.--doc--)
|
|
||||||
(setv inner.--multi-default-- (fn [&rest args &kwargs kwargs] None))
|
|
||||||
inner)
|
|
||||||
|
|
||||||
(defn method-decorator [dispatch-fn &optional [dispatch-key None]]
|
|
||||||
(setv apply-decorator
|
|
||||||
(fn [func]
|
|
||||||
(if (is dispatch-key None)
|
|
||||||
(setv dispatch-fn.--multi-default-- func)
|
|
||||||
(assoc dispatch-fn.--multi-- dispatch-key func))
|
|
||||||
dispatch-fn))
|
|
||||||
apply-decorator)
|
|
||||||
|
|
||||||
(defmacro defmulti [name params &rest body]
|
|
||||||
`(do (import [hy.contrib.multi [multi-decorator]])
|
|
||||||
(with-decorator multi-decorator
|
|
||||||
(defn ~name ~params ~@body))))
|
|
||||||
|
|
||||||
(defmacro defmethod [name multi-key params &rest body]
|
|
||||||
`(do (import [hy.contrib.multi [method-decorator]])
|
|
||||||
(with-decorator (method-decorator ~name ~multi-key)
|
|
||||||
(defn ~name ~params ~@body))))
|
|
||||||
|
|
||||||
(defmacro default-method [name params &rest body]
|
|
||||||
`(do (import [hy.contrib.multi [method-decorator]])
|
|
||||||
(with-decorator (method-decorator ~name)
|
|
||||||
(defn ~name ~params ~@body))))
|
|
||||||
|
|
||||||
(defmacro defn [name &rest bodies]
|
|
||||||
(setv arity-overloaded? (fn [bodies]
|
|
||||||
(if (isinstance (first bodies) HyString)
|
|
||||||
(arity-overloaded? (rest bodies))
|
|
||||||
(isinstance (first bodies) HyExpression))))
|
|
||||||
|
|
||||||
(if (arity-overloaded? bodies)
|
|
||||||
(do
|
|
||||||
(setv comment (HyString))
|
|
||||||
(if (= (type (first bodies)) HyString)
|
|
||||||
(setv [comment #* bodies] bodies))
|
|
||||||
(+ '(do (import [hy.contrib.multi [MultiDispatch]])) (lfor
|
|
||||||
[let-binds #* body] bodies
|
|
||||||
`(with-decorator MultiDispatch (defn ~name ~let-binds ~comment ~@body)))))
|
|
||||||
(do
|
|
||||||
(setv [lambda-list #* body] bodies)
|
|
||||||
`(setv ~name (fn* ~lambda-list ~@body)))))
|
|
@ -1,123 +0,0 @@
|
|||||||
;; Copyright 2019 the authors.
|
|
||||||
;; This file is part of Hy, which is free software licensed under the Expat
|
|
||||||
;; license. See the LICENSE.
|
|
||||||
|
|
||||||
(require [hy.contrib.multi [defmulti defmethod default-method defn]])
|
|
||||||
(import pytest)
|
|
||||||
|
|
||||||
(defn test-different-signatures []
|
|
||||||
"NATIVE: Test multimethods with different signatures"
|
|
||||||
(defmulti fun [&rest args]
|
|
||||||
(len args))
|
|
||||||
|
|
||||||
(defmethod fun 0 []
|
|
||||||
"Hello!")
|
|
||||||
|
|
||||||
(defmethod fun 1 [a]
|
|
||||||
a)
|
|
||||||
|
|
||||||
(defmethod fun 2 [a b]
|
|
||||||
"a b")
|
|
||||||
|
|
||||||
(defmethod fun 3 [a b c]
|
|
||||||
"a b c")
|
|
||||||
|
|
||||||
(assert (= (fun) "Hello!"))
|
|
||||||
(assert (= (fun "a") "a"))
|
|
||||||
(assert (= (fun "a" "b") "a b"))
|
|
||||||
(assert (= (fun "a" "b" "c") "a b c")))
|
|
||||||
|
|
||||||
(defn test-different-signatures-defn []
|
|
||||||
"NATIVE: Test defn with different signatures"
|
|
||||||
(defn f1
|
|
||||||
([] "")
|
|
||||||
([a] "a")
|
|
||||||
([a b] "a b"))
|
|
||||||
|
|
||||||
(assert (= (f1) ""))
|
|
||||||
(assert (= (f1 "a") "a"))
|
|
||||||
(assert (= (f1 "a" "b") "a b"))
|
|
||||||
(with [(pytest.raises TypeError)]
|
|
||||||
(f1 "a" "b" "c")))
|
|
||||||
|
|
||||||
(defn test-basic-dispatch []
|
|
||||||
"NATIVE: Test basic dispatch"
|
|
||||||
(defmulti area [shape]
|
|
||||||
(:type shape))
|
|
||||||
|
|
||||||
(defmethod area "square" [square]
|
|
||||||
(* (:width square)
|
|
||||||
(:height square)))
|
|
||||||
|
|
||||||
(defmethod area "circle" [circle]
|
|
||||||
(* (** (:radius circle) 2)
|
|
||||||
3.14))
|
|
||||||
|
|
||||||
(default-method area [shape]
|
|
||||||
0)
|
|
||||||
|
|
||||||
(assert (< 0.784 (area {:type "circle" :radius 0.5}) 0.786))
|
|
||||||
(assert (= (area {:type "square" :width 2 :height 2})) 4)
|
|
||||||
(assert (= (area {:type "non-euclid rhomboid"}) 0)))
|
|
||||||
|
|
||||||
(defn test-docs []
|
|
||||||
"NATIVE: Test if docs are properly handled"
|
|
||||||
(defmulti fun [a b]
|
|
||||||
"docs"
|
|
||||||
a)
|
|
||||||
|
|
||||||
(defmethod fun "foo" [a b]
|
|
||||||
"foo was called")
|
|
||||||
|
|
||||||
(defmethod fun "bar" [a b]
|
|
||||||
"bar was called")
|
|
||||||
|
|
||||||
(assert (= fun.--doc-- "docs")))
|
|
||||||
|
|
||||||
(defn test-kwargs-handling []
|
|
||||||
"NATIVE: Test handling of kwargs with multimethods"
|
|
||||||
(defmulti fun [&kwargs kwargs]
|
|
||||||
(get kwargs "type"))
|
|
||||||
|
|
||||||
(defmethod fun "foo" [&kwargs kwargs]
|
|
||||||
"foo was called")
|
|
||||||
|
|
||||||
(defmethod fun "bar" [&kwargs kwargs]
|
|
||||||
"bar was called")
|
|
||||||
|
|
||||||
(assert (= (fun :type "foo" :extra "extra") "foo was called")))
|
|
||||||
|
|
||||||
(defn test-basic-multi []
|
|
||||||
"NATIVE: Test a basic arity overloaded defn"
|
|
||||||
(defn f2
|
|
||||||
([] "Hello!")
|
|
||||||
([a] a)
|
|
||||||
([a b] "a b")
|
|
||||||
([a b c] "a b c"))
|
|
||||||
|
|
||||||
(assert (= (f2) "Hello!"))
|
|
||||||
(assert (= (f2 "a") "a"))
|
|
||||||
(assert (= (f2 "a" "b") "a b"))
|
|
||||||
(assert (= (f2 "a" "b" "c") "a b c")))
|
|
||||||
|
|
||||||
|
|
||||||
(defn test-kw-args []
|
|
||||||
"NATIVE: Test if kwargs are handled correctly for arity overloading"
|
|
||||||
(defn f3
|
|
||||||
([a] a)
|
|
||||||
([&optional [a "nop"] [b "p"]] (+ a b)))
|
|
||||||
|
|
||||||
(assert (= (f3 1) 1))
|
|
||||||
(assert (= (f3 :a "t") "t"))
|
|
||||||
(assert (= (f3 "hello " :b "world") "hello world"))
|
|
||||||
(assert (= (f3 :a "hello " :b "world") "hello world")))
|
|
||||||
|
|
||||||
|
|
||||||
(defn test-docs []
|
|
||||||
"NATIVE: Test if docs are properly handled for arity overloading"
|
|
||||||
(defn f4
|
|
||||||
"docs"
|
|
||||||
([a] (print a))
|
|
||||||
([a b] (print b)))
|
|
||||||
|
|
||||||
(assert (= f4.--doc-- "docs")))
|
|
Loading…
Reference in New Issue
Block a user