Tables
def t @{:a 1 :b 2 :c 3})
(
# Can also make a table with:
1 :b 2 :c 3)
(table :a
#=> 3
(length t)
# Cannot have `nil` as a key or a value in a table.
#=> @[:c :a :b]
(keys t) #=> @[ 3 1 2]
(values t)
#=> 3
(get t :c) # same
(t :c) #=> nil
(get t :x)
7) # Mutates `t`, and also returns it.
(put t :x @{:c 3 :x 7 :a 1 :b 2}
set (t :a) 88) # @{:c 3 :x 7 :a 88 :b 2}
(
# Add a value to the table.
set (t :z) 99) # @{:a 88 :b 2 :c 3 :x 7 :z 99}
(
def t @{:a @[1 2 @{:x 1}] :b "yo"})
(# If you want to change that :x value to a 3 ... Note:
# `(t :a)` is an array.
# `((t :a) 2)` is that table `@{:x 1}`.
# `(((t :a) 2) :x)` is that 1.
# So, both of these work:
2) :x 3) # First arg is the ds `put` operates on.
(put ((t :a) # ^^^^^^^^^^
set (((t :a) 2) :x) 3) # First arg is the value to set.
(# ^^^^^^^^^^^^^^^
# `t` is now `@{:a @[1 2 @{:x 3}] :b "yo"})`
# Note, you can increment it like so:
++ (((t :a) 2) :x)) #=> 4, and
(# `t` is now `@{:a @[1 2 @{:x 4}] :b "yo"})`
# Returns an array of 2-element key-value tuples.
(pairs t)
* x 4))
(defn foo [x] (# @{:a 88 :b 2 :c 3 :x 7 :z 99}
# @{:a 88 :b 2 :c 12 :x 7 :z 99}
(update t :c foo)
1 2 3]) #=> @{:c 3 :a 1 :b 2}
(zipcoll [:a :b :c] [
# Merges and returns a new table.
(merge t1 t2) -into t1 t2 t3) # Merges into and modifies `t1`.
(merge
# Since tables are mutable:
= t1 t2) # Only checks if they point to the same table.
(
# But you also have:
= t1 t2) # Recurs down, checking for equality. (deep
Notes:
table/clone
doesn’t copy everything deeply
For more details, see: