Public Vars

Back

defmethod (clj)

(source)

macro

(defmethod multifn dispatch-val & fn-tail)
Creates and installs a new method of multimethod associated with dispatch-value.

Examples

replikativ/datahike
(ns datahike.http.writer
  "Remote writer implementation for datahike.http.server through datahike.http.client."
  (:require [datahike.writer :refer [PWriter create-writer create-database delete-database]]
            [datahike.http.client :refer [request-json] :as client]
            [datahike.json :as json]
            [datahike.tools :as dt :refer [throwable-promise]]
            [taoensso.timbre :as log]
            [clojure.core.async :refer [promise-chan put!]]))

(defmethod create-writer :datahike-server
  [config connection]
  (log/debug "Creating datahike-server writer for " connection config)
  (->DatahikeServerWriter config connection))

(defmethod create-database :datahike-server
  [& args]
  (let [p (throwable-promise)
        {:keys [writer] :as config} (first args)]
    ;; redirect call to remote-peer as writer config
    (deliver p (try (->
                     (request-json :post
                                   "create-database-writer"
                                   writer
                                   (vec (concat [(-> config
                                                     (assoc :remote-peer writer)
                                                     (dissoc :writer))]
                                                (rest args))))
                     (dissoc :remote-peer))
                    (catch Exception e
                      e)))
    p))

(defmethod delete-database :datahike-server
  [& args]
  (let [p (throwable-promise)
        {:keys [writer] :as config} (first args)]
    ;; redirect call to remote-peer as writer config
    (deliver p (try
                 (-> (request-json :post
                                   "delete-database-writer"
                                   writer
                                   (vec (concat [(-> config
                                                     (assoc  :remote-peer writer)
                                                     (dissoc :writer))]
                                                (rest args))))
                     (dissoc :remote-peer))
                 (catch Exception e
                   e)))
    p))
noprompt/meander
(ns multimethods
  (:refer-clojure :exclude [defmethod defmulti])
  (:require
   #?(:clj [clojure.core :as clj] :cljs [cljs.core :as cljs])
   [meander.epsilon :as m]))

(defmacro defmethod
  [mf [& lhr] & body]
  (swap! cache_ assoc-in [mf lhr] body)
  (let [ptrns (get @cache_ mf)]
    `(-set-fn ~(with-meta mf {:tag `MultiMeanderFn})
              (fn [& ~'argsv]
                (m/match ~'argsv
                  ~@(loop [[[l r] & more] ptrns xs []]
                      (if l
                        (recur more (conj xs l (cons `do r)))
                        xs)))))))

(defmethod test-fn
  [:add ?x ?y]
  (println ?x ?y)
  (+ ?x ?y))

(defmethod test-fn
  [:sub ?x ?y]
  (- ?x ?y))

(defmethod test-fn
  [:sum . [!xs ...]]
  (reduce + !xs))
typedclojure/typedclojure
(ns ^:no-doc typed.clj.ext.clojure.core__defmethod
  "Typing rules for clojure.core/defmethod"
  (:require [clojure.core.typed.internal :as internal]
            [typed.clj.checker.check :as chk]
            [typed.cljc.analyzer :as ana2]
            [typed.cljc.checker.check.unanalyzed :refer [defuspecial]]))

;;==================
;; clojure.core/defmethod

(defuspecial defuspecial__defmethod
  "defuspecial implementation for clojure.core/defmethod"
  [{:keys [form] :as expr} expected]
  (-> expr
      (update :form internal/add-defmethod-destructure-blame-form)
      ana2/analyze-outer
      (chk/check-expr expected)))
bsless/more.async
(ns more.async.dataflow.node
  (:require
   [clojure.spec.alpha :as s]
   [clojure.core.async :as a]
   [more.async :as ma]
   [clojure.data]))

(s/def ::pipeline (s/keys :req [::to ::from ::size ::xf]))
(defmethod -type ::pipeline-blocking [_] (s/keys :req [::name ::pipeline]))
(defmethod -type ::pipeline-async    [_] (s/keys :req [::name ::pipeline]))
(defmethod -type ::pipeline          [_] (s/keys :req [::name ::pipeline]))

(defmethod -compile ::pipeline
  [{{to ::to from ::from size ::size xf ::xf} ::pipeline} env]
  (a/pipeline size (env to) xf (env from)))

(defmethod -compile ::pipeline-blocking
  [{{to ::to from ::from size ::size xf ::xf} ::pipeline} env]
  (a/pipeline-blocking size (env to) xf (env from)))

(defmethod -compile ::pipeline-async
  [{{to ::to from ::from size ::size af ::xf} ::pipeline} env]
  (a/pipeline-async size (env to) af (env from)))

(doseq [t [::pipeline ::pipeline-blocking ::pipeline-async]]
  (defmethod ports t
    [{{to ::to from ::from} ::pipeline}]
    #{{::name from ::direction ::in}
      {::name to ::direction ::out}}))


(defmethod -type ::batch [_]
  (s/keys :req [::name ::batch]))

(defmethod -compile ::batch
  [{{from ::from
     to ::to
     size ::size
     timeout ::timeout
     rf ::rf
     init ::init-fn
     finally ::finally-fn
     async? ::async?
     :or {rf conj init (constantly []) finally identity}}
    ::batch} env]
  (let [from (env from)
        to (env to)]
    (if async?
      (ma/batch! from to size timeout rf init finally)
      (a/thread (ma/batch!! from to size timeout rf init finally)))))

(defmethod ports ::batch
  [{{to ::to from ::from} ::batch}]
  #{{::name from ::direction ::in}
    {::name to ::direction ::out}})


(s/def ::mult (s/keys :req [::from] :opt [::to*]))
(defmethod -type ::mult [_] (s/keys :req [::name ::mult]))

(defmethod -compile ::mult
  [{{from ::from to :to*} ::mult} env]
  (let [mult (a/mult (env from))]
    (doseq [ch to] (a/tap mult (env ch)))
    mult))

(defmethod ports ::mult
  [{{to ::to from ::from} ::mult}]
  (into
   #{{::name from ::direction ::in}}
   (map (fn [to] {::name to ::direction ::out}))
   to))

(defmethod -type ::pubsub [_] (s/keys :req [::name ::pubsub]))

(defmethod -compile ::pubsub
  [{{pub ::pub sub ::sub tf ::topic-fn} ::pubsub} env]
  (let [p (a/pub (env pub) tf)]
    (doseq [{:keys [:sub/topic :sub/chan]} sub]
      (a/sub p topic (env chan)))
    p))

(defmethod ports ::pubsub
  [{{to ::sub from ::pub} ::pubsub}]
  (into
   #{{::name from
      ::direction ::in}}
   (map (fn [to] {::name to ::direction ::out}))
   to))

(defmethod -type ::produce [_] (s/keys :req [::name ::produce]))

(defmethod -compile ::produce
  [{{ch ::to f ::fn async? ::async?} ::produce} env]
  (let [ch (env ch)]
    (if async?
      (ma/produce-call! ch f)
      (a/thread (ma/produce-call!! ch f)))))

(defmethod ports ::produce
  [{{to ::to} ::produce}]
  #{{::name to ::direction ::out}})

(defmethod -type ::consume [_] (s/keys :req [::name ::consume]))

(defmethod -compile ::consume
  [{{ch ::from f ::fn async? ::async? checked? ::checked?} ::consume} env]
  (let [ch (env ch)]
    (if async?
      ((if checked?
         ma/consume-checked-call!
         ma/consume-call!) ch f)
      (a/thread ((if checked?
                   ma/consume-checked-call!!
                   ma/consume-call!!) ch f)))))

(defmethod ports ::consume
  [{{from ::from} ::consume}]
  #{{::name from ::direction ::in}})

(defmethod -type ::split [_] (s/keys :req [::name ::split]))

(defmethod -compile ::split
  [{{from ::from to ::to-map f ::fn
     dropping? ::dropping?} ::split} env]
  ((if dropping? ma/split?! ma/split!) f (env from) (env to)))

(defmethod ports ::split
  [{{to ::to-map from ::from} ::split}]
  (into
   #{{::name from ::direction ::in}}
   (map (fn [to] {::name to ::direction ::out}))
   (vals to)))

(defmethod -type ::reductions [_]
  (s/keys :req [::name ::type ::reductions]))

(defmethod -compile ::reductions
  [{{from ::from
     to ::to
     rf ::rf
     init ::rf
     async? ::async?} ::reductions} env]
  (let [from (env from)
        to (env to)]
    (if async?
      (ma/reductions! rf init from to)
      (a/thread
        (ma/reductions!! rf init from to)))))

(defmethod ports ::reductions
  [{{to ::to from ::from} ::reductions}]
  #{{::name from ::direction ::in}
    {::name to ::direction ::out}})
re-path/studio
(ns renderer.tools.edit
  (:require
   [clojure.core.matrix :as mat]
   [renderer.element.handlers :as element.h]
   [renderer.handlers :as handlers]
   [renderer.history.handlers :as history]
   [renderer.tools.base :as tools]
   [renderer.utils.pointer :as pointer]))

(defmethod tools/properties :edit
  []
  {:icon "edit"})

(defmethod tools/activate :edit
  [db]
  (-> db
      (handlers/set-state :default)
      (handlers/set-message
       [:div
        [:div "Drag a handler to modify your shape, or click on an element 
              to change selection."]
        [:div "Hold " [:strong "Ctrl"] " to restrict direction."]])))

(defmethod tools/pointer-down :edit
  [db _ el]
  (assoc db :clicked-element el))

(defmethod tools/pointer-move :edit
  [db _ el]
  (-> db
      element.h/clear-hovered
      (element.h/hover (:key el))))

(defmethod tools/drag-start :edit
  [db]
  (handlers/set-state db :edit))

(defmethod tools/drag :edit
  [{:keys [adjusted-pointer-offset adjusted-pointer-pos clicked-element] :as db} e]
  (let [pointer-offset (mat/sub adjusted-pointer-pos adjusted-pointer-offset)
        db (history/swap db)
        element-key (:element clicked-element)
        pointer-offset (if (contains? (:modifiers e) :ctrl)
                         (pointer/lock-direction pointer-offset)
                         pointer-offset)]
    (if element-key
      (assoc-in db
                (conj (element.h/path db) element-key)
                (tools/edit (element.h/element db element-key)
                            pointer-offset
                            (:key clicked-element)))
      db)))

(defmethod tools/drag-end :edit
  [db]
  (-> db
      (handlers/set-state :default)
      (dissoc :clicked-element)
      (history/finalize "Edit " (-> db :clicked-element :key name))))