Skip to content

Update

Name - Description Default Type
<input> Input is the new value of the variable being updated. Any
<output> The input to this shard is passed through as its output. Any
Name The name of the variable. "" String&Any
Key The key of the value to read from the table (parameter applicable only if the target variable is a table). None String&String
Global If the variable is available to all of the wires in the same mesh. false Bool

Modifies the value of an existing mutable variable.

Details

Update modifies the value of an existing mutable variable.

The name of the variable comes from the :Name parameter and the update value comes from the input.

Update overwrites string, numeric, and sequence variables with the new value (coming from input). However, for sequences, it cannot update a sequence at the level of elements (i.e., add elements, remove elements, change element order, etc.), so it overwrites the whole sequence with whatever you've passed in the input field.

Also, for an existing table, Update can only change the existing keys' values. It cannot add new key-value pairs to the table (do that with Set). To update existing key-values in a table you need to pass the key in the :Key parameter.

Since variables may be locally scoped (created with (:Global false); exists only for current wire) or globally scoped (created with (:Global true); exists for all wires of that mesh), both parameters :Global and :Name are used in combination to identify the correct variable to update.

The input to this shard is the update value to be applied to the mutable variables and is also passed through as this shard's output.

Note

Update has an alias >. Its an alias for Update with the defaults: (Update ...). See the code examples at the end to understand how this alias is used.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;; update a mutable string variable (by default available only to current wire)
"Hello" (Set :Name .svar)
.svar (Log ".svar")                 ;; => .svar: Hello
"World" (Update .svar)              ;; modify string variable
.svar (Log "modified .svar")        ;; => .svar: World

;; update a mutable numeric variable (available to all wires because `:Global true`)
100 (Set :Name .nvar :Global true)
.nvar (Log ".nvar")                 ;; => .nvar: 100
200 (Update 
    :Name .nvar
    :Global true)                   ;; modify numeric variable
.nvar (Log "modified .nvar")        ;; => modified .nvar: 200

;; update a mutable sequence (will overwrite it completely)
[10 20 30] (Set :Name .sequence)
[100] (Update :Name .sequence)
.sequence (Log ".sequence")         ;; => .sequence: [100]

;; update a mutable table: update existing key-value pair
["a" "b"] (Set
            :Name "table1"
            :Key "key1")
.table1 (Log ".table1")             ;; => .table1: {key1: [a, b]}
["def"] (Update .table1 :Key "key1")
.table1 (Log ".table1")             ;; => .table1: {key1: [def]}

;; Using an `Update` alias

;; `>` is alias for `(Update ...)`: update an existing string variable
"Hello" (Set :Name .svarA)
.svarA (Log ".svarA")               ;; => .svar: Hello
"World" > .svarA                    ;; modify string variable
.svarA (Log "modified .svar")       ;; => .svar: World

;; create and update local/global table variables
["a" "b"] (Set
            :Name "table"
            :Key "key1")            ;; create local table
["c" "d"] (Set
            :Name "table"
            :Key "key1"
            :Global true)           ;; create same-name global table
(Get .table) (Log)                  ;; local table => {key1: [a, b]}
(Get .table :Global true) (Log)     ;; same-name global table => {key1: [c, d]}
["X"] (Update .table :Key "key1")   ;; update local table
["Y"] (Update .table
            :Key "key1"
            :Global true)           ;; update same-name global table
(Get .table) (Log)                  ;; updated local table => {key1: [X]}
(Get .table :Global true) (Log)     ;; updated same-name global table => {key1: [Y]}
[info] [shards/General/Update/Update.edn] .svar: Hello
[info] [shards/General/Update/Update.edn] modified .svar: World
[info] [shards/General/Update/Update.edn] .nvar: 100
[info] [shards/General/Update/Update.edn] modified .nvar: 200
[info] [shards/General/Update/Update.edn] .sequence: [100]
[info] [shards/General/Update/Update.edn] .table1: {key1: [a b]}
[info] [shards/General/Update/Update.edn] .table1: {key1: [def]}
[info] [shards/General/Update/Update.edn] .svarA: Hello
[info] [shards/General/Update/Update.edn] modified .svar: World
[info] [shards/General/Update/Update.edn] {key1: [a b]}
[info] [shards/General/Update/Update.edn] {key1: [c d]}
[info] [shards/General/Update/Update.edn] {key1: [X]}
[info] [shards/General/Update/Update.edn] {key1: [Y]}