Public Vars

Back

swap! (clj)

(source)

function

(swap! atom f) (swap! atom f x) (swap! atom f x y) (swap! atom f x y & args)
Atomically swaps the value of atom to be: (apply f current-value-of-atom args). Note that f may be called multiple times, and thus should be free of side effects. Returns the value that was swapped in.

Examples

nextjournal/clerk
(ns viewers.controls
  "Demo of Clerk's two-way bindings."
  {:nextjournal.clerk/visibility {:code :show :result :show}}
  (:require [clojure.core :as core]
            [nextjournal.clerk :as clerk]
            [nextjournal.clerk.viewer :as viewer]))

(def render-slider
  '(fn [state-atom]
     [:input {:type :range :value @state-atom :on-change #(swap! state-atom (constantly (int (.. % -target -value))))}]))

(def render-text-input
  '(fn [state-atom]
     [:input {:type :text :value @state-atom :on-change #(swap! state-atom (constantly (.. % -target -value)))
              :class "px-3 py-3 placeholder-blueGray-300 text-blueGray-600 relative bg-white bg-white rounded text-sm border border-blueGray-300 outline-none focus:outline-none focus:ring w-full"}]))

(def convenient-slider
  {:transform-fn (comp transform-var (clerk/update-val #(cond-> % (viewer/get-safe % ::clerk/var-from-def) ::clerk/var-from-def)))
   :render-fn '(fn [x] (let [state-atom (cond-> x (var? x) deref)]
                         [:input {:type :range :value @state-atom :on-change #(swap! state-atom (constantly (int (.. % -target -value))))}]))})
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 defmulti [name]
  (swap! cache_ dissoc name)
  `(def ~name (MultiMeanderFn. nil)))

(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)))))))
hraberg/deuce
(ns deuce.emacs.marker
  (:use [deuce.emacs-lisp :only (defun defvar) :as el])
  (:require [clojure.core :as c]
            [deuce.emacs.alloc :as alloc]
            [deuce.emacs.buffer :as buffer]
            [deuce.emacs.data :as data])
  (:import [deuce.emacs.data Buffer BufferText Marker])
  (:refer-clojure :exclude []))

(defun set-marker (marker position &optional buffer)
  "Position MARKER before character number POSITION in BUFFER.
  BUFFER defaults to the current buffer.
  If POSITION is nil, makes marker point nowhere.
  Then it no longer slows down editing in any buffer.
  Returns MARKER."
  (let [^Buffer buffer (el/check-type 'bufferp (or buffer (buffer/current-buffer)))
        ^Marker marker marker]
    (when-let [^Buffer old-buffer @(.buffer marker)]
      (swap! (.markers ^BufferText (.text old-buffer)) #(seq (remove #{marker} %))))
    (reset! (.buffer marker) buffer)
    (reset! (.charpos marker) position)
    (swap! (.markers ^BufferText (.text buffer)) conj marker)
    marker))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

cc/reset! (t/All [x] [(t/Atom x) x :-> x])
cc/swap! (t/All [x b :..] [(t/Atom x) [x b :.. b :-> x] b :.. b :-> x])
#?@(:cljs [] :default [
cc/reset-vals! (t/All [x] [(t/Atom x) x :-> '[x x]])
cc/swap-vals! (t/All [x b :..] [(t/Atom x) [x b :.. b :-> x] b :.. b :-> '[x x]])
])
cc/vreset! (t/All [x] [(t/Volatile x) x :-> x])
cc/volatile? (t/Pred t/AnyVolatile)
cc/compare-and-set! (t/All [x] [(t/Atom x) t/Any x :-> t/Bool])
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)))

  (cache-method! [_ dispatch-value method]
    (swap! atomm assoc dispatch-value method))