Back
update (clj)
(source)function
(update m k f)
(update m k f x)
(update m k f x y)
(update m k f x y z)
(update m k f x y z & more)
'Updates' a value in an associative structure, where k is a
key and f is a function that will take the old value
and any supplied args and return the new value, and returns a new
structure. If the key does not exist, nil is passed as the old value.
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 transform-var
(comp clerk/mark-presented
(clerk/update-val (fn [v] (viewer/->ViewerEval (list 'resolve (list 'quote (symbol v))))))))
(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))))}]))})
nextjournal/clerk
(ns nextjournal.clerk.atom
"Demo of Clerk's two-way bindings."
{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(:require [clojure.core :as core]
[nextjournal.clerk :as clerk]))
(def transform-var
(comp (clerk/update-val symbol)
clerk/mark-presented))
(def counter-viewer
{:transform-fn transform-var
:render-fn '(fn [var-name]
(if-let [var (resolve var-name)]
(let [atom @var]
[:div
[:h2 "Counter Example"]
[:button.px-2.py-1.bg-blue-200.mr-1 {:on-click #(swap! atom update :counter inc)} "+"]
[:button.px-2.py-1.bg-blue-200.mr-1 {:on-click #(swap! atom update :counter dec)} "-"]
[:button.px-2.py-1.bg-blue-200.mr-1 {:on-click #(swap! atom (fn [_] {:counter 0}))} "reset"]
[nextjournal.clerk.render/inspect @atom]])
[:div "could not resolve" var-name]))})
;; changing my-state on the JVM and running clerk/show! will update the slider
;; and counter accordingly:
(comment
(do
(swap! my-state update :counter #(mod (+ % 33) 100))
(clerk/recompute!)))
mhuebert/maria
(ns cells.cell
(:refer-clojure :exclude [bound-fn get])
(:require [clojure.core :as core]
[cells.util :as util]
[applied-science.js-interop :as j]))
(defmacro defcell
"Defines a named cell."
[the-name & body]
(let [[docstring body] (if (string? (first body))
[(first body) (rest body)]
[nil body])
[options body] (if (and (map? (first body)) (> (count body) 1))
[(first body) (rest body)]
[nil body])
f `(fn [~'self] ~@body)]
`(do
;; support re-evaluation without breaking links
(declare ~the-name)
(let [prev-cell# ~the-name]
(def ~(with-meta the-name options)
~@(when docstring (list docstring))
(if (some? prev-cell#)
(~'cells.cell/update-cell* prev-cell# ~f)
(~'cells.cell/cell* ~f)))))))
typedclojure/typedclojure
(ns ^:no-doc typed.clj.ext.clojure.core__reify
"Typing rules clojure.core/reify"
(:require [clojure.core.typed.internal :as internal]
[typed.cljc.checker.check :as chk]
[typed.cljc.analyzer :as ana2]
[typed.cljc.checker.check.unanalyzed :refer [defuspecial]]))
(defuspecial defuspecial__reify
"defuspecial implementation for clojure.core/reify"
[{original-reify-form :form :as original-expr} expected]
(-> original-expr
ana2/analyze-outer
((fn [expr]
{:pre [(= :unanalyzed (:op expr))]
:post [(= :unanalyzed (:op %))]}
(update expr :form (fn [form]
{:pre [(= 'reify* (first form))]}
(-> (list* (vary-meta (first form) assoc ::original-reify-expr original-expr)
(rest form))
(with-meta (meta form)))))))
(chk/check-expr expected)))
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/update (t/All [[m :< (t/Option (t/Associative t/Any t/Any))]
k v c :..]
[m k [(t/Get m k) c :.. c :-> v] c :.. c :-> (t/Assoc m k v)])
;;TODO
;cc/update-in (t/All [m k :.. v c :..] [m (t/HSequential [k :.. k]) [(t/GetIn m (t/HSequential [k :.. k])) c :.. c :-> v] c :.. c :-> (t/AssocIn m (t/HSequential [k :.. k]) v)])
cc/method-sig [java.lang.reflect.Method :-> '[t/Any t/AnyNilableNonEmptySeq t/Any]]
cc/proxy-name [Class (t/Seqable Class) :-> t/Str]
cc/get-proxy-class [Class :* :-> Class]
cc/construct-proxy [Class t/Any :* :-> t/Any]
cc/init-proxy [t/Proxy (t/Map t/Str t/Any) :-> t/Proxy]
cc/update-proxy [t/Proxy (t/Map t/Str t/Any) :-> t/Proxy]
cc/proxy-mappings [t/Proxy :-> (t/Map t/Str t/Any)]
cc/proxy-call-with-super (t/All [x] [[:-> x] t/Proxy t/Str :-> x])
cc/bean [Object :-> (t/Map t/Any t/Any)]
])
cc/update-keys (t/All [k k' v] [(t/Nilable (t/Associative k v)) [k :-> k'] :-> (t/Map k' v)])
cc/update-vals (t/All [k v v'] [(t/Nilable (t/Associative k v)) [v :-> v'] :-> (t/Map k v')])