Skip to content

Assoc

Name - Description Default Type
<input> Input sequence that defines which element in the target sequence or table needs to be updated and with what value. Should have even number of elements. [Any]
<output> Modified array or table. Has the same type as the array or table on which Assoc was applied. [Any]
Name The name of the sequence or table to be updated. "" String&Any
Key Table key for the value that is to be updated. Parameter applicable if target is table. None String&String
Global If the variable is or should be available to all the wires in the same mesh. The default value (false) makes the variable local to the wire. false Bool

Updates a sequence (array) or a table (associative array/ dictionary) on the basis of an input sequence.

Details

This shard can be used to update specific member elements within a sequence or a table with new values.

The input sequence identifies which elements are to be updated and their new/ updated values. To achieve this, the member elements of this input sequence are parsed in pairs. The 1st element of each pair gives the index of the target element to update, and the 2nd element of that pair gives the new value for the target element. Due to this, the input sequence must always contain an even number of elements.

Examples

1
2
3
4
5
6
;; Update a sequence (needs to be mutable!)
[10 20] >= .sequence
(Log) ;; prints original target sequence => [10 20]
[0 2 1 3] (Assoc .sequence) ;; input sequence updates target sequence [index-0 ele => 2, index-1 ele => 3]
.sequence (Log) ;; prints updated target sequence => [2 3] 
.sequence (Assert.Is [2 3] true)
[info] [shards/General/Assoc/1.edn] [10 20]
[info] [shards/General/Assoc/1.edn] [2 3]

 

1
2
3
4
5
6
;; Update a global-var table (Global = true; table available to all the wires in the same mesh)
{:key1 [10 20] :key2 [30 40]} (Set .tableG :Global true)
(Log) ;; prints original table => {:key1 [10 20] :key2 [30 40]}
[0 2 1 3] (Assoc .tableG "key1") ;; input sequence updates value of key "key1" in table
.tableG (Log) ;; prints updated table => {:key1 [2 3] :key2 [30 40]}
.tableG (Assert.Is {:key1 [2 3] :key2 [30 40]} true)
[info] [shards/General/Assoc/2.edn] {key1: [10 20] key2: [30 40]}
[info] [shards/General/Assoc/2.edn] {key1: [2 3] key2: [30 40]}

 

1
2
3
4
5
6
;; Update a local-variable table (:Global = false, table available to only this wire in the mesh)
{:key1 [10 20] :key2 [30 40]} >= .table ; (needs to be mutable!)
(Log) ;; prints original table => {:key1 [10 20] :key2 [30 40]}
[0 2 1 3] (Assoc .table "key2") ;; input sequence updates value of key "key2" in table
.table (Log) ;; prints updated table => {:key1 [10 20] :key2 [2 3]}
.table (Assert.Is {:key1 [10 20] :key2 [2 3]} true)
[info] [shards/General/Assoc/3.edn] {key1: [10 20] key2: [30 40]}
[info] [shards/General/Assoc/3.edn] {key1: [10 20] key2: [2 3]}