Public Vars

Back

update-in (clj)

(source)

function

(update-in m ks f & args)
'Updates' a value in a nested associative structure, where ks is a sequence of keys and f is a function that will take the old value and any supplied args and return the new value, and returns a new nested structure. If any levels do not exist, hash-maps will be created.

Examples

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)])
juxt/jig
(ns jig.mqtt
  (:require
   jig
   [clojurewerkz.machine-head.client :as mh]
   [clojure.core.async :refer (chan >!! close!)]
   [clojure.tools.logging :refer :all])
  (:import (jig Lifecycle)))

(deftype MqttSubscriber [config]
  Lifecycle
  (init [_ system]
    (let [ch (chan (or (:channel-size config) 100))]
      (assoc-in system [:jig/channels (:channel config)] ch)))
  (start [_ system]
    (let [ch (get-in system [:jig/channels (:channel config)])]
      (infof "MQTT, client is %s, topics are %s" (::machine-head-client system) (:topics config))
      (mh/subscribe
       (::machine-head-client system)
       (:topics config)
       (fn [topic meta payload]
         (infof "Received message on topic %s: %s" topic (String. payload))
         (>!! ch {:topic topic :meta meta :payload payload}))))
    system)
  (stop [_ system]
    (let [client (::machine-head-client system)]
      (mh/unsubscribe client (:topics config)))
    (close! (get-in system [:jig/channels (:channel config)]))
    (update-in system [:jig/channels] dissoc (:channel config))))
bsless/clj-fast
(ns clj-fast.clojure.core-test
  (:require [clj-fast.clojure.core :as sut]
            [clojure.test :as t]))

(t/deftest test-update
  (t/are [result expr] (= result expr)
    {:a [1 2]}   (update {:a [1]} :a conj 2)
    [1]          (update [0] 0 inc)
    ;; higher-order usage
    {:a {:b 2}}  (sut/update-in {:a {:b 1}} [:a] update :b inc)
    ;; missing field = nil
    {:a 1 :b nil} (update {:a 1} :b identity)
    ;; 4 hard-coded arities
    {:a 1} (update {:a 1} :a +)
    {:a 2} (update {:a 1} :a + 1)
    {:a 3} (update {:a 1} :a + 1 1)
    {:a 4} (update {:a 1} :a + 1 1 1)
    ;; rest arity
    {:a 5} (update {:a 1} :a + 1 1 1 1)
    {:a 6} (update {:a 1} :a + 1 1 1 1 1)))

(t/deftest test-update-in
  (let [m {:a {:b 1}}]
    (t/is (= {:a {:b 2}} (sut/update-in m [:a :b] + 1)))
    (t/is (= {:a {:b 1}
              :c {:d true}} (sut/update-in m [:c :d] not))))
  (t/testing "Variadic arity"
    (let [m {:a 1}
          ks [:a]]
      (t/are [result expr] (= result expr)
        {:a 1} (sut/update-in m ks +)
        {:a 2} (sut/update-in m ks + 1)
        {:a 3} (sut/update-in m ks + 1 1)
        {:a 4} (sut/update-in m ks + 1 1 1)
        {:a 5} (sut/update-in m ks + 1 1 1 1)))))
Flexiana/framework
(ns xiana.interceptor.kebab-camel
  (:require
    [camel-snake-kebab.core :as csk]
    [camel-snake-kebab.extras :as cske]
    [clojure.core.memoize :as mem]))

(def interceptor
  "The purpose is to make Js request compatible with clojure, and response compatible with Javascript.
  :request - {:params { "
  {:name  ::camel-to-kebab-case
   :enter (fn [state]
            (reduce
              (fn [state type-param]
                (update-in state [:request type-param] camel-to-kebab))
              state
              request-type-params))
   :leave (fn [state]
            (update-in state [:response :body] kebab-to-camel))})
re-path/studio
(ns renderer.panel.events
  (:require
   [clojure.core.matrix :as mat]
   [re-frame.core :as rf]
   [renderer.utils.local-storage :as local-storage]))

(rf/reg-event-db
 :panel/toggle
 [local-storage/persist
  (rf/path :panel)]
 (fn [db [_ key]]
   (update-in db [key :visible?] not)))