You're viewing version 1.2.0 of assoc. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

assoc

clojure.core

  • (assoc map key val)
  • (assoc map key val & kvs)
assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector).

2 Examples top

  • user=> (assoc {} :key1 "value" :key2 "another value")
    {:key2 "another value", :key1 "value"}
    
    user=> (assoc {:key1 "old value1" :key2 "value2"} :key1 "value1" :key3 "value3")
    {:key3 "value3", :key2 "value2", :key1 "value1"}
    
    user=> (assoc nil :key1 4)
    {:key1 4}
    
    user=> (assoc [1 2 3] 0 10)
    [10 2 3]
    
    user=> (assoc [1 2 3] 3 10)
    [1 2 3 10]
    
    user=> (assoc [1 2 3] 4 10)
    java.lang.IndexOutOfBoundsException (NO_SOURCE_FILE:0)
    
    
    
    
    ;; From http://clojure-examples.appspot.com/clojure.core/assoc
  • (def test-map {:account-no 12345678 :lname "Jones" :fnam "Fred"})
    (assoc test-map :fnam "Sue")
    {:account-no 12345678, :lname "Jones", :fnam "Sue"}
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:161 top

(def
 ^{:arglists '([map key val] [map key val & kvs])
   :doc "assoc[iate]. When applied to a map, returns a new map of the
    same (hashed/sorted) type, that contains the mapping of key(s) to
    val(s). When applied to a vector, returns a new vector that
    contains val at index. Note - index must be <= (count vector)."
   :added "1.0"}
 assoc
 (fn assoc
   ([map key val] (. clojure.lang.RT (assoc map key val)))
   ([map key val & kvs]
    (let [ret (assoc map key val)]
      (if kvs
        (recur ret (first kvs) (second kvs) (nnext kvs))
        ret)))))
Vars in clojure.core/assoc: first fn key let map nnext second val
Used in 140 other vars (expand)
zipmap proxy-call-with-super update-in assoc-in definline defn- replace bean memoize declare proxy add-remote-javadoc rename-keys map-invert index deftest- deftest inc-report-counter set-test with-test suite-attrs xml-zip defmacro- defvar- defvar make-map defunbound- defhinted defunbound defonce- defalias defstruct- dissoc-in replace-relation merge-relations add-index add-relation remove-tuple add-tuple build-seed-bindings delta-literal magic-literal negated-literal seed-relation seed-predicate-for-insertion deferror fnmap add-remote-javadoc add-break-thread! assoc headers with-system-properties add-components returns times has-args calls monad-transformer summarize prof with-system-properties repl-info frequencies group-by frequencies group-by deftype- deftest- deftest inc-report-counter set-test with-test mapcat-chain auto run-httpcore status wrap-file-info wrap-flash assoc-param wrap-session time-series-plot box-plot add-box-plot add-categories stacked-bar-chart bar-chart heat-map xy-plot pie-chart scatter-plot line-chart function-plot $group-by add-points histogram add-function add-lines stacked-area-chart area-chart add-histogram defop $rollup query-dataset som-batch-train deriv-fn* deps compile defproject returns times has-args calls fail success-result base-result pending-result with-timing annotate set-attr remove-attr append transform-content cacheable content prepend html-content remove-class xml-zip defslimefn swank-toggle-trace register-dispatch get new-sandbox-compiler tree-map button group-container-args calls has-args returns times

Comments top

1 comment(s) for assoc.

the API is blurry When applied to a vector

;; should indicate following
(assoc vector index val) 
Log in to add a comment.